将数据导出到 Excel 文件 C#.NET
我想将access数据库中的数据导出到excel文件。 但我收到 HRESULT 异常:0x800A03EC 错误。
这是我编写的一些代码,
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
private void showBtn_Click(object sender, EventArgs e)
{
int cnt = -1;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\AyumiDB1.mdb");
thisConnection.Open();
OleDbCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT CodeNumber, Particular, LF, DebitCredit, Amount FROM JournalVoucher";
OleDbDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
cnt++;
MessageBox.Show(thisReader["CodeNumber"].GetType().ToString());
xlWorkSheet.Cells[cnt, 1] = thisReader["CodeNumber"].ToString();
xlWorkSheet.Cells[cnt, 2] = thisReader["Particular"].ToString();
xlWorkSheet.Cells[cnt, 3] = thisReader["LF"].ToString();
xlWorkSheet.Cells[cnt, 4] = thisReader["DebitCredit"].ToString();
xlWorkSheet.Cells[cnt, 5] = thisReader["Amount"].ToString();
}
thisReader.Close();
thisConnection.Close();
xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
我的问题是
MessageBox.Show(thisReader["CodeNumber"].GetType().ToString());
运行完美,但是当我尝试将相同的值插入 excel 单元格
xlWorkSheet.Cells[cnt, 1] = thisReader["CodeNumber"].ToString();
时,会引发异常。
我做错了什么吗?
谢谢
I want to export the data from a access database to excel file.
But i got Exception from HRESULT: 0x800A03EC error.
Here is a bit of code that i have written,
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
private void showBtn_Click(object sender, EventArgs e)
{
int cnt = -1;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\AyumiDB1.mdb");
thisConnection.Open();
OleDbCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT CodeNumber, Particular, LF, DebitCredit, Amount FROM JournalVoucher";
OleDbDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
cnt++;
MessageBox.Show(thisReader["CodeNumber"].GetType().ToString());
xlWorkSheet.Cells[cnt, 1] = thisReader["CodeNumber"].ToString();
xlWorkSheet.Cells[cnt, 2] = thisReader["Particular"].ToString();
xlWorkSheet.Cells[cnt, 3] = thisReader["LF"].ToString();
xlWorkSheet.Cells[cnt, 4] = thisReader["DebitCredit"].ToString();
xlWorkSheet.Cells[cnt, 5] = thisReader["Amount"].ToString();
}
thisReader.Close();
thisConnection.Close();
xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
My problem is the
MessageBox.Show(thisReader["CodeNumber"].GetType().ToString());
runs perfectly, but when i tried to insert the same value to the cell of excel with
xlWorkSheet.Cells[cnt, 1] = thisReader["CodeNumber"].ToString();
then the exception is thrown.
Am i doing something wrong??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这一行中避免您更改的异常
In this line avoid the exception you change
而不是
尝试这个
可能会起作用
Instead of
try this
Might work