我有一个代码应该从excel文档中读取并告诉我里面是什么,我得到了一个错误。因此,当我使用关键字“excel”时,程序应该打开一个文件对话框,让我选择一个excel文档,之后应该告诉我里面是什么。这是代码
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;
答案 0 :(得分:0)
由于文件名末尾有黑色标记,文件被视为文件夹。
删除这些黑色墨水并重试
string fileName = @"C:\Users\N.Horatiu\Desktop\a.xlsx";