如何使用c#以只读方式创建excel文件

问题描述:

在下面的代码中,我能够生成一个.xls文件,但是我无法将其作为只读文件。

我在'var fileName ...'行收到错误,说明'你是缺少using指令或汇编引用'。



In the following code I am able to generate an .xls file but am unable to make it as readonly.
I get an error at line 'var fileName..' stating that 'are you missing an using directive or assembly reference' .

ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\RAJENDRAN\\Desktop\\DailySoresOutwardIndent.xls");
      ExcelApp.ActiveWorkbook.Saved = true;
      var fileInfo = new fileInfo("C:\\Users\\RAJENDRAN\\Desktop\\DailySoresOutwardIndent.xls");
      fileInfo.IsReadOnly = true;

试试这个



try this

System.IO.FileInfo objFileInfo = new System.IO.FileInfo(szFilePath);
            objFileInfo.IsReadOnly = true;