Exception from HRESULT: 0x800A03EC

花如玉 2009-09-03 09:37:04
我在把数据导到excel时出现Exception from HRESULT: 0x800A03EC的异常。
但是我导出的数据较少时就不会出现这样的异常,请各位帮忙。

下面为我的C#代码

{
//dv为要输出到Excel的数据
Excel.Application excel;// = new Application();

int rowIndex = 1;

int colIndex = 1;

Excel._Workbook xBk;

Excel._Worksheet xSt;

excel = new Excel.ApplicationClass();

xBk = excel.Workbooks.Add(true);

xSt = (Excel._Worksheet)xBk.ActiveSheet;

xSt.Name = "Summary";


//取得列标题
foreach (DataColumn col in dv.Table.Columns)
{//好像在这个循环里抛出的异常

excel.Cells[rowIndex, colIndex] = col.ColumnName;

xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Bold = true;
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Name = "Arial";
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Size = "8";

colIndex++;
}

//取得表格中的数据

foreach (DataRowView row in dv)
{
colIndex = 1;

rowIndex++;

foreach (DataColumn col in dv.Table.Columns)
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();

xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;//设置字符型的字段格式为左对齐
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Name = "Arial";
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Size = "8";
colIndex++;

}

}

int rowSum = rowIndex;
colIndex--;


//设置报表表格为最适应宽度

xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowSum, colIndex]).Select();

xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowSum, colIndex]).Columns.AutoFit();


//冻结
excel.ActiveWindow.SplitColumn = 1;
excel.ActiveWindow.SplitRow = 1;
excel.ActiveWindow.Split = true;
excel.ActiveWindow.FreezePanes = true;

//显示效果
HttpContext curContext = HttpContext.Current;

xBk.SaveCopyAs(curContext.Server.MapPath(".") + "\\" + fileName + ".xls");

//ds = null;

xBk.Close(false, null, null);

excel.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);

System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);

xBk = null;

excel = null;

xSt = null;

string path = curContext.Server.MapPath(fileName + ".xls");

FileInfo file = new FileInfo(path);

curContext.Response.Clear();

curContext.Response.Charset = "GB2312";

curContext.Response.ContentEncoding = Encoding.UTF8;

// 添加头信息,为"文件下载/另存为"对话框指定默认文件名

curContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

// 添加头信息,指定文件大小,让浏览器能够显示下载进度

curContext.Response.AddHeader("Content-Length", file.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载

curContext.Response.ContentType = "application/ms-excel";

// 把文件流发送到客户端

curContext.Response.WriteFile(file.FullName);

curContext.Response.Flush();

File.Delete(file.FullName);
}
catch (Exception ex)
{
Loger.writeExceptionLog(ex.Message);
throw ex;
}






...全文
3026 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
vssvss 2010-04-07
  • 打赏
  • 举报
回复
非常感谢楼上的提醒 终于找到我的bug出在哪里了
如果单元格内容过长了 那么如何解决呢
如果dataArray有一行的内容很长, 那么这个函数就不能用了 能有其他的解决方案吗

mySheet.get_Range(mySheet.Cells[beginRow, 1], mySheet.Cells[(beginRow + row-1), content.Length]).Value2 = dataArray;

花如玉 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 findcaiyzh 的回复:]
google到这个,你看看你的是不是这个原因
大意是内容中不能有 "=".

I'm using Microsoft.Office.Interop.Excel.Application object and .Excel.Range.value2 property in my C# applications. I'm listing some datas on gridview and also exporting this datas to excel. On this export operation my application threw an exception like "HRESULT  0x800A03EC". I searched the error line and I realised that If a cell includes equal char('='), excel probably conjectured this char as a excel formula and throw an exception. In conclusion, If you replace equal char('=') with another one, problem solved easily.
[/Quote]

多谢回复。但是我导出的cell中的内容没有‘=’。好像列数太多的缘故!
宝_爸 2009-09-03
  • 打赏
  • 举报
回复
google到这个,你看看你的是不是这个原因
大意是内容中不能有 "=".

I'm using Microsoft.Office.Interop.Excel.Application object and .Excel.Range.value2 property in my C# applications. I'm listing some datas on gridview and also exporting this datas to excel. On this export operation my application threw an exception like "HRESULT 0x800A03EC". I searched the error line and I realised that If a cell includes equal char('='), excel probably conjectured this char as a excel formula and throw an exception. In conclusion, If you replace equal char('=') with another one, problem solved easily.
花如玉 2009-09-03
  • 打赏
  • 举报
回复
列数太多的缘故么?
xycit 2009-09-03
  • 打赏
  • 举报
回复
up~看着像内存错误`!
泡面之夏 2009-09-03
  • 打赏
  • 举报
回复
有可能是某个单元格的内容太长的原因;

参考:

http://www.cnblogs.com/huadd/archive/2009/05/08/1452788.html

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧