Reputation: 2924
We are developing a Web application and some of our query results need to be Exported to Excel. We are using the following C# code to Export :
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);
The partial View I am returning to result is as follows :
<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>
But export to Excel works on some machines, but not others. This started to happen only recently.
Is there a possible solution to fix this without re-writing the whole Exporting functionality?
Upvotes: 0
Views: 175
Reputation: 21
Apologies this is not an answer but i am unable to comment as i have low rep.
IS excel installed on the machines it is not working on? From my memory excel must be installed for you to export. I have used Epplus to get around this in the past.
Are you able to add some exception handling and logging around this issued so you can get more details from the error? Even if you just write it to a txt file on the machine.
Sorry i dont have any actual answers.
Upvotes: 2
Reputation: 147
May below code will be helpful for you:
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();
}
Upvotes: -1