导出excel报错System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏

xiaoyanwei2000 2011-01-04 03:13:52
导出wps表格时报错

2010-12-27 12/27/2010 09:44:24 userid:1222 loginid:bd username:测试供电公司
Button.RaisePostBackEvent => Button.OnClick => Student.btnExcel_Click
message:System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
在 ET.Range.get_Cells()
在 OSTS.Web.Exam.Student.btnExcel_Click(Object sender, EventArgs e)

代码如下

object missing = Type.Missing;
string temp = Request.PhysicalApplicationPath + "ExcelModel" + "\\" + ViewState["FILE"].ToString();//文档模版
ET.Application etApp = new ET.ApplicationClass();
ET._Workbook etWork = etApp.Workbooks.Open(temp, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
ET.Worksheet etSheet = (ET.Worksheet)etWork.Sheets.get_Item(1);
ET.Range ra;
string filename = "";
string name = "";
string savePath = "";
DataSet ds;
DataTable dtMain;
DataTable dtSub;
DataView dv;

#region 全部试卷
if (ViewState["FILE"].ToString() == "paper.xls" && ViewState["DATA"].GetType() == typeof(DataTable))
{
dtMain = (DataTable)ViewState["DATA"];
int index = 4;
int sum = 0;
etSheet.Cells[2, 1] = ViewState["BATCHNAME"];
for (int i = 0; i < dtMain.Rows.Count; i++)
{
etSheet.Cells[index + i, 1] = i + 1;
etSheet.Cells[index + i, 2] = "'" + dtMain.Rows[i]["jobcode"];
etSheet.Cells[index + i, 3] = dtMain.Rows[i]["jobname"];
etSheet.Cells[index + i, 4] = dtMain.Rows[i]["itemname"];
etSheet.Cells[index + i, 5] = dtMain.Rows[i]["num"];
sum += int.Parse(dtMain.Rows[i]["num"].ToString());
}
etSheet.Cells[index + dtMain.Rows.Count, 1] = "合计";
etSheet.Cells[index + dtMain.Rows.Count, 5] = sum;
string cell = "g" + (dtMain.Rows.Count + 3);
ra = etSheet.get_Range("a3", cell);
ra.Cells.Borders.LineStyle = ET.ETLineStyle.etContinuous;
ra.Borders.Weight = ET.ETBorderWeight.etThin;
}
#endregion
savePath = Request.PhysicalApplicationPath + "ExcelModel" + "\\" + ViewState["FILENAME"].ToString();
etWork.SaveAs(savePath, missing, missing, missing, missing, missing, ET.ETSaveAsAccessMode.etExclusive, missing, missing, missing, missing);
etWork.Close(false, missing, missing);
etApp.Quit();

filename = Convert.ToString(savePath);
name = HttpUtility.UrlEncode(System.Text.UTF8Encoding.UTF8.GetBytes(ViewState["FILENAME"].ToString()));

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();

请教各位大侠,问题该如何解决
...全文
908 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lengfeng8866 2012-01-06
  • 打赏
  • 举报
回复
我们一个同事在开发中也遇到了此类问题,一直没有很好的解决,不知道楼主解决了没有?
xiaoyanwei2000 2012-01-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lengfeng8866 的回复:]

我们一个同事在开发中也遇到了此类问题,一直没有很好的解决,不知道楼主解决了没有?
[/Quote]
用了更高的版本就解决了,我本机是2010,服务器是2007,升级服务器的版本就解决了
wuyq11 2011-01-04
  • 打赏
  • 举报
回复
EXCEL版本是否一致
程序本身的问题。
使得内存资源没有及时释放导致此错误
tianliang1 2011-01-04
  • 打赏
  • 举报
回复
不懂,前几天我也做了一个把GV导出到Excel表,自己测试通过了。不过你说的这种部署到服务器上会出现问题。。。我还没有试过。。。先在此留个脚印,帮你顶顶。。。
提示:其实这类方法网上有很多种,你可以试者换换其他的实现方法,或许不会出现这个问题。。。
ruiruiy 2011-01-04
  • 打赏
  • 举报
回复
是导出到个sheet吗
我现在也正在做这个
希望能借鉴点你的经验
xiaoyanwei2000 2011-01-04
  • 打赏
  • 举报
回复
没人呀...
xiaoyanwei2000 2011-01-04
  • 打赏
  • 举报
回复
顺便说一局,程序部署在我的机器上没事,但是部署到客户的服务器上导出Excel(客户要求导出wps表格)就有问题了。

62,269

社区成员

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

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

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

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