我正在尝试创建一个可以打开 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");
}
}
}
}
1 回答
我不知道你是否找到了解决方案,但这是一种解决方法 . 希望它有助于我面对完全相同的问题 . 问题出在这条线上 . 在我的情况下,工作簿有多个工作表,因此我想循环遍历每个工作簿并检索数据 .
您正在尝试打开一个索引/名称为“Sheet1”但不存在的工作表..如果您已经知道您期望的工作表名称,请使用这些名称,否则最好使用工作表索引 . 例
这使得表格在索引1处
这就是我如何解决我的问题 . 我首先检查工作表名称是否存在