我们正在开发一个Web应用程序,我们的一些查询结果需要导出到Excel。我们使用下面的C#代码导出:在某些机器上导出到Excel失败
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工作在某些机器上,而不是其他。这仅在最近才开始发生。
有没有可能的解决方案来解决这个问题,而不重写整个导出功能?
这不会生成Excel文件,它会通过生成HTML表格并使用误导性的扩展名和内容类型来伪造它。我承认,我从来没有见过*这种类型的黑客行为 - 使用视图来生成HTML表格并更改响应标题。使用像EPPlus这样的库来生成一个* real * Excel文件。它更容易,更快速,并且可以实现更小的下载。 –
非常感谢您的回复。使用EPPlus是一个选项,但我们必须重新编写所有页面的导出到Excel功能,这是我试图避免的选项。如果可能的话,我试图找到更快的解决方法。 – user3021830
您目前没有任何Excel支持,您正在生成HTML表格,并希望Excel能够解密它们。 EPPlus *是最快捷的选择。所有你需要的是'worksheet.LoadFromCollection(CurrentPackingListModel.Items);'从项目列表生成一个表格。之后,返回一个FileResult与工作表 –