导出到Excel在某些机器上失败

我们正在开发一个Web应用程序,我们的一些查询结果需要导出到Excel。 我们正在使用下面的C#代码来导出:

System.Web.HttpContext ctx = System.Web.HttpContext.Current; CurrentPackingListModel.Voyage.ShipmentDataContext = ShipmentDataContext; ctx.Response.Clear(); string filename = "ApprovalForm.xls"; ctx.Response.AddHeader("content-disposition", "attachment;filename=" + filename); ctx.Response.ContentType = "application/vnd.ms-excel"; ctx.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); ctx.Response.Charset = "UTF-8"; return View("../Packing/_ExportApprovalForm", CurrentPackingListModel); 

我返回结果的部分视图如下:

 <body id="body" onload="window.print();"> <table> <tbody> <tr> <td class="table-header" colspan="8"> <div style="width: 100%"> <div class="lleft"> @* <img id="imgLogo" src="~/Images/myLogo.png" />*@ </div> <div class="baslik">Approval Packing List Form</div> <div style="float: right;">@DateTime.Now.ToString("MM.dd.yyyy")</div> </div> </td> </tr> <tr> <td colspan="6"></td> </tr> <tr> <td></td> </tr> <tr> <td></td> </tr> <tr> <td class="line-header">@Html.DisplayNameFor(x => x.ID)</td> <td>: @Html.HiddenFor(x => x.ID)@Html.DisplayFor(x => x.ID)</td> <td class="line-header" style="width: 165px;">@Html.DisplayNameFor(x => x.Voyage.StartDate)</td> <td>: @Html.DisplayFor(x => x.Voyage.StartDate)</td> <td class="line-header">@Html.DisplayNameFor(x => x.Voyage.VesselID)</td> <td>: @Html.DisplayFor(x => x.Voyage.VesselText) </td> </tr> <tr> <td class="line-header">@Html.DisplayNameFor(x => x.Voyage.Id)</td> <td>: @Html.DisplayFor(x => x.Voyage.Id)</td> <td class="line-header" style="width: 165px;">@Html.DisplayNameFor(x => x.Voyage.EndDate)</td> <td>: @Html.DisplayFor(x => x.Voyage.EndDate)</td> <td></td> <td></td> </tr> <tr> <td colspan="6"> <hr /> </td> </tr> </tbody> </table> <table> <tr> <td class="line-header" style="width: 160px;">Approve Personel</td> <td style="border: solid 1px; width: 180px;"></td> <td class="line-header">Discharge Port</td> <td style="border: solid 1px; width: 180px;"></td> </tr> <tr> <td class="line-header">Approve Date</td> <td style="border: solid 1px;"></td> <td class="line-header">Terminal</td> <td style="border: solid 1px;"></td> </tr> <tr> <td class="line-header">Signiture</td> <td style="border: solid 1px;"></td> <td></td> <td></td> </tr> </table> @if (Request.QueryString["type"] == "HRC" && Model.HrcListPrint != null) { <table> <tr> <td colspan="10" style="height: 20px;"> <hr /> </td> </tr> <tr> <td style="text-align: center; width: 210mm; font-weight: bold;" colspan="11">HRC LIST </td> </tr> </table> <table class="display dataTable no-footer"> <thead> <tr> <th>Customer Name</th> <th>Customer PO No</th> <th>Ord. ITem No</th> <th>CM No</th> <th>Product</th> <th>Size (T x W inch)</th> <th>Thickness Tolerance</th> <th>Qty (tons)</th> <th>Coil Weight (Lbs)</th> <th>Destination Port</th> <th>Barcode</th> <th>Heat No</th> <th>Status</th> </tr> </thead> <tbody> @foreach (MedTrade.Apollo.Shared.Models.Shipment.PackingListDetailModel item in Model.HrcListPrint) { <tr> <td>@item.CustomerName</td> <td>@item.CustomerPurchaseOrderNumber</td> <td>@String.Format("'{0}'", item.OrderItemText)</td> <td>@item.CMNO</td> <td>@item.ProductStandartName</td> <td>@item.ProductProperty</td> <td>@item.ThicknessToleranceType</td> <td>@((item.Quantity / 1000).ToString("N3"))</td> <td>@item.CoilWeight.ToString("N0")</td> <td>@item.DischargePortTanim</td> <td>@item.BarcodeNo</td> <td>@item.HeatNo</td> <td>@item.StatusText</td> </tr> } </tbody> </table> } @if (Request.QueryString["type"] == "Rebar" && Model.RebarListPrint != null) { <table> <tr> <td colspan="10" style="height: 20px;"> <hr /> </td> </tr> <tr> <td style="text-align: center; width: 210mm; font-weight: bold;" colspan="10">REBAR LIST </td> </tr> </table> <table class="display dataTable no-footer"> <thead> <tr> <th>Customer Name</th> <th>Customer PO No</th> <th>Ord. ITem No</th> <th>CM No</th> <th>Product</th> <th>Size (D x L inch)</th> @if (Model.SearchCriteria.ViewType == ViewType.Group) { <th>Qty (tons) / # of bundles</th> } else { <th>Quantity (Tons)</th> } <th>Bundle Weight (Lbs)</th> <th>Destination Port</th> @if (Model.SearchCriteria.ViewType == ViewType.Detail) { <th>Barcode</th> } <th>Heat No</th> <th>Status</th> </tr> </thead> <tbody> @foreach (MedTrade.Apollo.Shared.Models.Shipment.PackingListDetailModel item in Model.RebarListPrint) { <tr> <td>@item.CustomerName</td> <td>@item.CustomerPurchaseOrderNumber</td> <td>@String.Format("'{0}'", item.OrderItemText)</td> <td>@item.CMNO</td> <td>@item.ProductStandartName</td> <td>@item.ProductProperty</td> @if (Model.SearchCriteria.ViewType == ViewType.Group) { <td>@((item.Quantity / 1000).ToString("N3")) / @item.Count</td> } else { <td>@((item.Quantity / 1000).ToString("N3"))</td> } <td>@item.BundleWeight.ToString("N0")</td> <td>@item.DischargePortTanim</td> @if (Model.SearchCriteria.ViewType == ViewType.Detail) { <td>@item.BarcodeNo</td> } <td>@item.HeatNo</td> <td>@item.StatusText</td> </tr> } </tbody> </table> } </body> 

但是导出到Excel可以在一些机器上运行,但是不能用于其他机器 这只是最近才开始发生的。

有没有可能的解决scheme来解决这个问题,而不重写整个导出function?

道歉这不是一个答案,但我无法评论,因为我有低代表。

是不是在工作的机器上安装了Excel excel? 从我的记忆中,必须安装excel才能导出。 过去我用Epplus来解决这个问题。

你能够添加一些exception处理和logging这个发布,所以你可以从错误中获得更多的细节? 即使您只是将其写入机器上的txt文件。

对不起,我没有任何实际的答案。

下面的代码可能会对你有所帮助:

 public void ExportToExcel() { DataGrid dgGrid = new DataGrid(); dgGrid.DataSource = /*Give your data source here*/; dgGrid.DataBind(); System.Web.HttpContext.Current.Response.ClearContent(); System.Web.HttpContext.Current.Response.Buffer = true; System.Web.HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Data Report.xls")); System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; System.Web.HttpContext.Current.Response.Charset = ""; System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); dgGrid.RenderControl(htw); System.Web.HttpContext.Current.Response.Output.Write(sw.ToString()); System.Web.HttpContext.Current.Response.Flush(); System.Web.HttpContext.Current.Response.End(); }