尝试读取 .xlsm 文件时出错
现在我有扩展名为.xlsm的excel文件,我需要使用C#进行读写。 我尝试使用 Microsoft.Office.Interop.Excel,
Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
Workbook ObjWorkBook;
Worksheet ObjWorkSheet;
ObjWorkBook = ObjExcel.Workbooks.Open(excelFilename,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet) ObjWorkBook.Sheets[1];
if (ObjWorkSheet != null)
{
Range usedRange = ObjWorkSheet.UsedRange;
if (usedRange != null)
{
for (int rowIndex = 1; rowIndex < usedRange.Rows.Count; rowIndex++)
{
for (int colIndex = 1; colIndex < usedRange.Columns.Count; colIndex++)
{
string firstName = ((Microsoft.Office.Interop.Excel.Range)ObjWorkSheet.Cells[rowIndex, colIndex]).Value2.ToString();
}
}
}
}
但是,在读取单元格时,我发现错误“对象引用未设置为对象的实例”。尽管我替换了另一个新的 excel 文件.xlsm,并且它读取时没有错误... 另外,我试图读取的文件有宏...
有什么解决方案吗?
提前致谢 :)
Now I have excel file of extension .xlsm, and I need to read and write using C#.
I tried to use Microsoft.Office.Interop.Excel,
Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
Workbook ObjWorkBook;
Worksheet ObjWorkSheet;
ObjWorkBook = ObjExcel.Workbooks.Open(excelFilename,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet) ObjWorkBook.Sheets[1];
if (ObjWorkSheet != null)
{
Range usedRange = ObjWorkSheet.UsedRange;
if (usedRange != null)
{
for (int rowIndex = 1; rowIndex < usedRange.Rows.Count; rowIndex++)
{
for (int colIndex = 1; colIndex < usedRange.Columns.Count; colIndex++)
{
string firstName = ((Microsoft.Office.Interop.Excel.Range)ObjWorkSheet.Cells[rowIndex, colIndex]).Value2.ToString();
}
}
}
}
however, I found error Object reference not set to an instance of an object when reading cells..although I replaced another new excel file.xlsm and it read with no errors...
Also, The file I'm trying to read has macros...
Any Solutions ?!
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论