C#中如何检查excel文件是否被写保护?
我正在开发一个示例应用程序,其中我必须打开一个 Excel 文件并检查该文件是否受到写保护。代码是
using System.Windows.Forms;
using Microsoft.Office.Core;
private void button1_Click(object sender, EventArgs e)
{
string fileNameAndPath = @"D:\Sample\Sample1.xls";
// the above excel file is a write protected.
Microsoft.Office.Interop.Excel.Application a =
new Microsoft.Office.Interop.Excel.Application();
if (System.IO.File.Exists(fileNameAndPath))
{
Microsoft.Office.Interop.Excel.ApplicationClass app =
new Microsoft.Office.Interop.Excel.ApplicationClass();
// create the workbook object by opening the excel file.
app.Workbooks.Open(fileNameAndPath,0,false,5,"","",true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t",false, true, 0,false,true,0);
Microsoft.Office.Interop.Excel._Workbook w =
app.Workbooks.Application.ActiveWorkbook;
if (w.ReadOnly)
MessageBox.Show("HI");
// the above condition is true.
}
}
我想知道该文件是否被写保护。
I'm developing a sample application in which I have to open an excel file and check whether the file is write protected or not. The code is
using System.Windows.Forms;
using Microsoft.Office.Core;
private void button1_Click(object sender, EventArgs e)
{
string fileNameAndPath = @"D:\Sample\Sample1.xls";
// the above excel file is a write protected.
Microsoft.Office.Interop.Excel.Application a =
new Microsoft.Office.Interop.Excel.Application();
if (System.IO.File.Exists(fileNameAndPath))
{
Microsoft.Office.Interop.Excel.ApplicationClass app =
new Microsoft.Office.Interop.Excel.ApplicationClass();
// create the workbook object by opening the excel file.
app.Workbooks.Open(fileNameAndPath,0,false,5,"","",true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t",false, true, 0,false,true,0);
Microsoft.Office.Interop.Excel._Workbook w =
app.Workbooks.Application.ActiveWorkbook;
if (w.ReadOnly)
MessageBox.Show("HI");
// the above condition is true.
}
}
I would like know whether the file is write protected or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以像这样获取 FileAttributes:
请参阅文档: http ://msdn.microsoft.com/en-us/library/system.io.fileattributes.aspx
You can get the FileAttributes an like this:
See for documentation: http://msdn.microsoft.com/en-us/library/system.io.fileattributes.aspx
如果您想检查文件是否为只读,则可以使用
File.GetAttributes()
,如下所示:If you want to check if the file is readonly, then you can check using
File.GetAttributes()
, like this:我认为您想查看
WorkBook
类的HasPassword
属性。更多信息请访问:http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.haspassword%28VS.80%29.aspx
编辑:在下面留下我的旧答案
吗意思是文件或工作簿是否是只读的?
要检查工作簿是否为只读,
WorkBook
类具有一个ReadOnly
属性。否则,要检查文件,请查看使用框架中的 IO.FileInfo 类来获取文件属性,如以下代码所示:
I think you want to look at the
HasPassword
property of theWorkBook
class.More info at: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.haspassword%28VS.80%29.aspx
Edit: Left my old answer below
Do you mean if the file or the workbook is readonly?
To check if the workbook is readonly the
WorkBook
class has aReadOnly
property.Otherwise, to check the file, look at using the
IO.FileInfo
class in the framework to get out the file attributes, like in the following code:您可以检查 File.GetAttributes
You can check File.GetAttributes
本质上,只读和写保护是同一件事。但是,您可能会遇到无法访问文件的情况,因为该文件正在被另一个进程使用。在这种情况下,您尝试使用 FileShare 打开它,如下所示:
Essentially, ReadOnly and Write-protected are the same thing. However, you might be encountering the situation where you are unable to access a file because it is being used by another process. In this case, you try to open it with a FileShare as below:
您可以检查保护
You can check protection with