c# - C# 无法从只读 Excel 文件中读取
问题描述
我的电脑上有一个文件夹,其中包含多个都标记为只读的 Excel 电子表格。该文件夹通过 OneDrive 同步到我公司的 Sharepoint。
当我尝试通过 Microsoft.Office.Interop.Excel 以编程方式从其中一张表中读取数据时,我不断收到You cannot use this command on a protected sheet
错误消息。
这是我用来打开文件的代码:
public ExcelReader(String filePath)
{
this.filePath = filePath;
FileName = filePath.Substring(filePath.LastIndexOf("\\")+1);
app = new Excel.Application();
app.DisplayAlerts = false;
workbook = app.Workbooks.Open(filePath, false, true); //open in read only
}
public void openSheet(String sheet)
{
SheetName = sheet;
worksheet = workbook.Sheets[sheet];
Excel.Range last = worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
ColumnsTotal = last.Column;
RowsTotal = last.Row;
}
引发异常的行是Excel.Range last = worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
.
我认为,由于我明确告诉工作簿以只读模式打开,并且由于我从不修改这些文件的内容,因此文件是只读的这一事实不应该成为问题。
我在这里做错了什么?如何在不取消保护的情况下读取这些文件的内容(出于安全原因,我不能这样做)?
解决方案
推荐阅读
- python - 提交未上传到 codecov
- typescript - 无法在数据中获取变量
- vb.net - 如何在文本框中输入最大长度的字符后添加空格
- java - Android Studio 更新到 3.4 后未在工具菜单中显示 SDK Manager 和 AVDManager
- python - 如何访问 Pandas 系列中的最后一个元素?
- node.js - 使用 Docker 运行单个 NodeJS 脚本并能够使用 Ctrl-C 终止它的最简单方法是什么
- javascript - 如何将 url 添加到客户端浏览器历史记录?
- r - 如何设置 UTF8 并通过 R 启动 H2O Flow UI?
- java - 在 android 中更新应用程序后,我的资产文件夹中的图像没有被替换
- javascript - 通过单击图像展开/折叠 Div