c#export to excell无法在某些计算机上运行

时间:2015-06-12 12:08:55

标签: c# excel

我有以下代码,以便导出excel

 System.IO.MemoryStream stream = new System.IO.MemoryStream();

using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
{
   WriteExcelFile(ds, document);
}

   stream.Flush();
   stream.Position = 0;

   Response.ClearContent();
   Response.Clear();
   Response.Buffer = true;
   Response.Charset = "";

   Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
   Response.AddHeader("content-disposition", "attachment; filename=" + filename);
   Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";


   byte[] data1 = new byte[stream.Length];
   stream.Read(data1, 0, data1.Length);
   stream.Close();
   Response.BinaryWrite(data1);
   Response.Flush();
   Response.End();

使用上述代码将数据从数据集导出到Excel时,几乎所有计算机都没有任何问题。但是在某些计算机中存在如下错误。

错误:

NullReferenceException: Object reference not set to an instance of an object

堆栈

[NullReferenceException: Object reference not set to an instance of an object.]

Project.Excel_Export.CreateDataSourceNew() in d:\Projects\Rapor\Excel_Export.aspx.cs:146

Project.Excel_Export.bagla() in d:\Projects\Rapor\Excel_Export.aspx.cs:129

Project.Excel_Export.Page_Load(Object sender, EventArgs e) in d:\Projects\Rapor\Excel_Export.aspx.cs:91




 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
 System.Web.UI.Control.OnLoad(EventArgs e) +99
 System.Web.UI.Control.LoadRecursive() +50
 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

如果我在本地运行项目,则没有任何错误。导出到Excel时,只有部分计算机出现此错误。

导出excel时出错的某些计算机有什么问题?

问题可能与计算机中的服务器或excel版本有关?

我想念的地方,我该如何解决这个问题。

任何帮助将不胜感激。

感谢。

0 个答案:

没有答案