2016-08-04 76 views
0

我们正在开发一个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工作在某些机器上,而不是其他。这仅在最近才开始发生。

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

+1

这不会生成Excel文件,它会通过生成HTML表格并使用误导性的扩展名和内容类型来伪造它。我承认,我从来没有见过*这种类型的黑客行为 - 使用视图来生成HTML表格并更改响应标题。使用像EPPlus这样的库来生成一个* real * Excel文件。它更容易,更快速,并且可以实现更小的下载。 –

+0

非常感谢您的回复。使用EPPlus是一个选项,但我们必须重新编写所有页面的导出到Excel功能,这是我试图避免的选项。如果可能的话,我试图找到更快的解决方法。 – user3021830

+0

您目前没有任何Excel支持,您正在生成HTML表格,并希望Excel能够解密它们。 EPPlus *是最快捷的选择。所有你需要的是'worksheet.LoadFromCollection(CurrentPackingListModel.Items);'从项目列表生成一个表格。之后,返回一个FileResult与工作表 –

回答

-1

下面的代码月份将是对你有所帮助:

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(); 
} 
+0

这不会生成Excel文件,它会通过生成HTML表格并使用误导来伪造扩展名 –

+0

事实上,OP的代码完全相同 - 视图呈现一张表格并且标题被更改,以使浏览器认为这是一个Excel文件 –

2

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

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

你能够添加一些异常处理和记录这个发布,所以你可以从错误中获得更多的细节?即使你只是把它写到机器上的txt文件中。

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

+0

非常感谢您的回复。所有机器上都安装Excel 2013。使用EPPlus是一个选项,但我们必须重新编写所有页面的导出到Excel功能,这是我试图避免的选项。我们正在捕获错误,但代码不会抛出任何异常。我可以导出一个Excel文件,但它不会打开或打开为空白。 – user3021830

+0

@ user3021830你*目前没有任何Excel支持,你正在使用黑客来生成HTML表格。这是不可修复的。这是因为Excel *会使用每台机器的默认区域设置导入*您生成的HTML文件。您对该流程拥有* no *的控制权。例如,在法国,德国,希腊,小数点分隔符是','。在这些国家的Excel中加载的任何CSV/HTML表格都将显示错误的数字值。一个真正的XLSX文件不会有任何问题,尽管 –

+0

@ user3021830你没有发布实际生成HTML表的代码(即你的视图)。这可能是内容根本无法导入,例如多个表格,嵌套表格或任何其他可能导致导入的东西 –

相关问题