使用 C# windowsforms 应用程序导出 Microsoft Excel 中的数据
当我尝试将数据导出到 Excel 时出现异常...异常是
COMException:HRESULT 异常:0x800A03EC。
我该如何解决这个错误?我该如何得到解决方案?告诉我这个问题的解决方案...
提前致谢...
我的代码是:
{
oxl = new Excel.Application();
oxl.Visible = true;
oxl.DisplayAlerts = false;
wbook = oxl.Workbooks.Add(Missing.Value);
wsheet = (Excel.Worksheet)wbook.ActiveSheet;
wsheet.Name = "Customers";
DataTable dt = InstituteTypeDetail();
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
wsheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
wsheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
range = wsheet.get_Range(wsheet.Cells[1, 1],
wsheet.Cells[rowCount, dt.Columns.Count]);//In this place i got the exception
range.EntireColumn.AutoFit();
wsheet = null;
range = null;
}
我做错了什么?解决这个异常的方法是什么....有人请告诉我...
i got the exception when i am trying to export data to excel.... the exception is
COMException: Exception from HRESULT: 0x800A03EC.
How shall i solve this error? How shall i get solution? Tell me the solution of this problem...
Thanks in advance...
My Code is:
{
oxl = new Excel.Application();
oxl.Visible = true;
oxl.DisplayAlerts = false;
wbook = oxl.Workbooks.Add(Missing.Value);
wsheet = (Excel.Worksheet)wbook.ActiveSheet;
wsheet.Name = "Customers";
DataTable dt = InstituteTypeDetail();
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
wsheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
wsheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
range = wsheet.get_Range(wsheet.Cells[1, 1],
wsheet.Cells[rowCount, dt.Columns.Count]);//In this place i got the exception
range.EntireColumn.AutoFit();
wsheet = null;
range = null;
}
what i did wrong? what is the way to solve this exception.... Anyone plz tell me...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下示例代码对我来说效果很好。你能尝试一下吗?已修改您的示例。最后释放对所有 com 对象的引用始终是一个好习惯。
The following sample code works fine for me. Can you give it a try. Have modified your sample. Also it is always a good practice to release references to all com objects at the end.
当您收到错误时,您的
rowCount
实际上正确吗?您从 1 开始,但随后立即递增 - 因此,当您在foreach
循环中检查第 1 行时,rowCount
实际上是 2。我认为您的 < code>rowCount 应初始化为 0(零)。然后您的标题行检查应查找
if (rowCount == 1)
。Is your
rowCount
actually correct at the point you get the error? You're starting it from 1, but then incrementing it immediately - so when you're examining row 1 in yourforeach
loop,rowCount
is actually 2.I think your
rowCount
should be initialised to 0 (zero.) Then your header row check should look forif (rowCount == 1)
.get_Range
期望单元格名称为字符串,即“A1”、“B25”等。您可以尝试用以下代码替换该行:get_Range
expects the name of a cell as string, i.e. something like "A1", "B25" etc. You could try to replace the line with the following code:SpreadsheetGear for .NET 可让您通过 .NET 处理 Excel 工作簿,而不会出现与 Excel COM Interop 相关的问题。 SpreadsheetGear 也比 COM Interop 更快 - 特别是当像代码那样循环访问多个单元格时。
以下是一些与使用 SpreadsheetGear API 的代码执行相同操作的代码:
您可以在 此处查看实时示例 如果您想亲自尝试,请在此处下载免费试用版。
免责声明:我拥有 SpreadsheetGear LLC
SpreadsheetGear for .NET will let you work with Excel workbooks from .NET without the problems associated with Excel COM Interop. SpreadsheetGear is also faster than COM Interop - especially when looping through a number of cells as your code seems to do.
Here is some code which does the same thing as your code using the SpreadsheetGear API:
You can see live samples here and download the free trial here if you want to try it yourself.
Disclaimer: I own SpreadsheetGear LLC