我正在使用.net 4.0。我使用OpenFileDialog
从Excel工作表中读取数据。第一次OpenFileDialog
工作得很好,我得到了文件,但在关闭它并重新打开它以获取另一个文件后, AccessViolationException 会显示出来。
代码:
private void excelFileOpenBt_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
String filename = openFileDialog1.FileName;
if (filename == String.Empty)
return;
fileNameLb.Text = filename;
FileStream stream = new FileStream(filename, FileMode.Open);
//Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader2003 = ExcelReaderFactory.CreateBinaryReader(stream);
DataSet result = excelReader2003.AsDataSet();
String txt = "";
foreach (DataTable table in result.Tables)
{
MessageBox.Show("Table");
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Columns.Count; j++)
txt += "\"" + table.Rows[i].ItemArray[j] + "\";";
txt += "----";
}
}
MessageBox.Show(txt);
excelReader2003.Close();
stream.Close();
}
}