在某些计算机上导出到Excel失败

本文关键字:Excel 失败 计算机 | 更新日期: 2023-09-27 18:00:12

我们正在开发一个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在某些机器上有效,但在其他机器上无效。这种情况直到最近才开始发生。

有没有可能的解决方案可以在不重写整个导出功能的情况下修复此问题?

在某些计算机上导出到Excel失败

很抱歉,这不是一个答案,但我无法发表评论,因为我的代表性很低。

excel是否安装在不工作的机器上?根据我的记忆,必须安装excel才能导出。我过去曾使用Epplus来解决这个问题。

您是否能够添加一些异常处理和日志记录,以便从错误中获得更多详细信息?即使你只是把它写在机器上的一个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();
}