C# 在c中读取excel时出错#
我有一个代码,应该从excel文档中读取并告诉我里面是什么,但我得到了一个错误。因此,当我使用关键字“excel”时,程序应该打开一个文件对话框,让我选择一个excel文档,然后告诉我里面是什么。这是密码C# 在c中读取excel时出错#,c#,excel,C#,Excel,我有一个代码,应该从excel文档中读取并告诉我里面是什么,但我得到了一个错误。因此,当我使用关键字“excel”时,程序应该打开一个文件对话框,让我选择一个excel文档,然后告诉我里面是什么。这是密码 case "excel": if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
case "excel":
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application _excelApp = new Microsoft.Office.Interop.Excel.Application();
_excelApp.Visible = true;
string fileName = @"C:\Users\N.Horatiu\Desktop\a.docx\\";
Workbook workbook = _excelApp.Workbooks.Open(fileName,
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);
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range excelRange = worksheet.UsedRange;
object[,] valueArray = (object[,])excelRange.get_Value(
XlRangeValueDataType.xlRangeValueDefault);
for (int row = 1; row <= worksheet.UsedRange.Rows.Count; ++row)
{
for (int col = 1; col <= worksheet.UsedRange.Columns.Count; ++col)
{
ss.SpeakAsync(valueArray[row, col].ToString());
}
}
workbook.Close(false, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(workbook);
_excelApp.Quit();
Marshal.FinalReleaseComObject(_excelApp);
}
break;
case“excel”:
if(openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application_excelApp=新的Microsoft.Office.Interop.Excel.Application();
_excelApp.Visible=true;
字符串文件名=@“C:\Users\N.Horatiu\Desktop\a.docx\\”;
工作簿=_excelApp.Workbooks.Open(文件名,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,
类型。缺失,类型。缺失);
工作表=(工作表)工作簿。工作表[1];
Microsoft.Office.Interop.Excel.Range excelRange=worksheet.UsedRange;
对象[,]值数组=(对象[,])excelRange.get_值(
XlRangeValueDataType.xlRangeValueDefault);
对于(int row=1;row,由于文件名末尾的黑色斜杠,文件被视为文件夹
删除这些黑色斜杠,然后重试
string fileName = @"C:\Users\N.Horatiu\Desktop\a.xlsx";
很明显,如果你看到这个异常,你会觉得它是一个有效的文件路径吗?这是一个普通的“找不到文件”错误。您希望如何将Word文档作为Excel工作表打开?在任何情况下,如果您想要打开Excel文件,请使用open XML SDK或类似EPPlus的库,不要使用interop来打开整个应用程序。为您节省了数GB的RAMA数…GB?我怀疑Excel是否有那么大的一头猪。您没有否决,但是否检查了extension?