我正在尝试创建一个可以打开xlsx
文件进行阅读的应用,而不是阅读它并用它做一些事情。当我运行我的应用程序,然后单击按钮加载文件时,我收到此错误:
索引无效。来自HRESULT的异常:0x8002000B(DISP_E_BADINDEX)
在这行代码中:
Excel.Workbook a
= excelApp.Workbooks.Open("C:\\test.xlsx", 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t", false, false, 0, true, 1, 0);
你能说出这里可能出现的问题吗?
编辑:这是完整的代码,所以我希望更容易分辨导致错误的原因
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form, IDisposable
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook a = excelApp.Workbooks.Open("C:/test.xlsx");
// This selectes the used range of the excel workbook and enters it in
// a two dimentional array
try
{
// Get a reference to the first sheet of the workbook.
Excel.Sheets excelSheets = a.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
// write out to console for debugging
textBox1.Text = "excelWorksheet is " + excelWorksheet;
// Get a range of data.
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A3", Missing.Value);
// write out to console for debugging
textBox1.Text = "excelCell is " + excelCell;
// write out to console for debugging
textBox1.Text = "Creating string[,] array. . . ";
// Retrieve the data from the range.
Object[,] dataArray;
// write out to console for debugging
textBox1.Text = "String[,] array created. . . ";
dataArray = (System.Object[,])excelCell.get_Value(Missing.Value);
// write out to console for debugging
textBox1.Text = "Counting rows and columns. . . ";
// Determine the dimensions of the array.
int iRows;
int iCols;
iRows = dataArray.GetUpperBound(0);
iCols = dataArray.GetUpperBound(1);
// write out to console for debugging
textBox1.Text = "Printing array. . . ";
// Print the data of the array.
for (int rowCounter = 1; rowCounter <= iRows; rowCounter++)
{
// write out to console for debugging
textBox1.Text = ("row " + rowCounter);
for (int colCounter = 1; colCounter <= iCols; colCounter++)
{
// Write the next value to the console.
richTextBox1.Text = "col " + colCounter + "= " + dataArray[rowCounter, colCounter].ToString() + ", ";
}
// Write in a new line.
richTextBox1.Text = "\n";
}
}
catch (Exception theException)
{
// Create error message
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
// Display error message
MessageBox.Show(errorMessage, "Error");
}
}
}
}
答案 0 :(得分:0)
我不知道您是否找到了解决方案,但这是一种解决方法。希望能帮助到你 我面临着完全相同的问题。 问题出在这条线上。在我的情况下,工作簿有多个工作表,因此我想循环遍历每个工作簿并检索数据。
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
您正在尝试打开包含索引/名称的工作表&#34; Sheet1&#34;如果您已经知道您期望的工作表名称,请使用这些名称,否则最好使用工作表索引。 例子
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(1);
这使得表格在索引1
这就是我如何解决我的问题。我首先检查工作表名称是否存在
var sheetExists = xlWorkBook.Worksheets.Cast<Excel.Worksheet>().FirstOrDefault(worksheet => worksheet.Name == "SomeSheetName"); // this line returns null if the sheet name or index you intend to open does not exist
if (sheetExists != null) // this line thus handles the invalid index error.
{ /**you can now open the sheet**/
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
}
答案 1 :(得分:0)
我相信 sheetExists 变量已经有了工作表,所以不需要再次获取它,所以如果 sheetExists 不为空,那么就可以使用了