c# - 导出到 Excel C#(Web 窗体)函数导出 ASP 网格突然无法工作和打印''
问题描述
我有一个隐藏网格,我使用它来允许 ASP 按钮将一些数据打印到 Excel 文件,而不在页面上显示网格。这种方法工作正常,但突然导出的 Excel 文件只是打印在两个不同div
的/div
单元格中 - 仅此而已。
我将在下面粘贴按钮、网格、事件和导出服务。
按钮:
<asp:LinkButton ID="ButtonExcelButton" runat="server" OnClick="ExcelButton_Click" CssClass="blue" Text="<i class='fas fa-file-excel'></i>" />
网格:
<asp:Panel ID="pnlHide" runat="server" Visible="false">
<asp:GridView ID="gridHidden" runat="server" AutoGenerateColumns="false" CellPadding="15" CellSpacing="5" OnRowDataBound="Grid_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Workpackage" DataField="EXT_Workpackage_ID" SortExpression="Code" />
<asp:BoundField HeaderText="Name" DataField="Job" SortExpression="Name" />
<asp:BoundField HeaderText="Job" DataField="name" SortExpression="Job" />
<asp:BoundField HeaderText="Billing" DataField="billing" SortExpression="Status" />
<asp:BoundField HeaderText="Allocated" DataField="Allocated_To" SortExpression="Billing" />
<asp:BoundField HeaderText="Created Date" DataField="createdate" SortExpression="Allocated" />
<asp:BoundField HeaderText="Earliest Start" DataField="earliest_start_date" SortExpression="Type" />
<asp:BoundField HeaderText="Latest Completion" DataFormatString="{0:dd MMM yyyy}" DataField="latest_completion" SortExpression="Created" />
<asp:BoundField HeaderText="Last Update" DataFormatString="{0:dd MMM yyyy}" DataField="last_update" SortExpression="EarliestStart" />
<asp:BoundField HeaderText="Actual" ItemStyle-HorizontalAlign="Center" DataField="Actual" SortExpression="ProductionBudget" />
<asp:BoundField HeaderText="Duration" ItemStyle-HorizontalAlign="Center" DataField="child_duration" SortExpression="EstimatedMaterials" />
<asp:BoundField HeaderText="Schedule A (£)" ItemStyle-HorizontalAlign="Center" DataField="scheduleatotal" SortExpression="ScheduleA" />
<asp:BoundField HeaderText="Contract Type" DataField="contracttype" SortExpression="ContractType" />
</Columns>
</asp:GridView>
</asp:Panel>
方法:
protected void ExcelButton_Click(object sender, EventArgs e)
{
ExportService.ExportGrid(Response, gridHidden);
}
出口服务:
public static void ExportGrid(HttpResponse response, GridView grid, string fileName = "Export", bool rebind = false)
{
response.Clear();
response.Clear();
response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));
response.Charset = "";
// comment out line below to open excel sheet without saving local copy
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
response.ContentType = "application/vnd.xlsx";
var stringWrite = new StringWriter();
var htmlWrite = new HtmlTextWriter(stringWrite);
if (rebind)
{
grid.DataBind();
}
grid.RenderControl(htmlWrite);
response.Write(stringWrite.ToString());
response.End();
}
如前所述,这种方法在几个月内运行良好,但最近引起了这种奇怪的行为。我已经研究过了,我仍然无法理解这个问题。
谢谢
解决方案
推荐阅读
- c++ - QT 如何使用 QDirIterator 访问子目录中的特定目录?
- kubernetes - “imagePullSecrets”有什么不同的值,“regsecret”有什么作用?
- python - 尝试将 DataFrame 写入羽毛时出错。羽毛是否支持列表列?
- r - R-cran 把图像放在外边距
- postgresql - GNU Parallel子进程(Postgres Vacuum)意外终止
- javascript - 在对象数组中查找某个键的总和
- reactjs - Mapbox GL:getLayer('id') 始终未定义
- html - 有没有办法在 iOS 12 上为 Safari 设置小点的密码输入样式?
- javascript - 无法弄清楚如何使用 Chart.js 跳过 x 轴上的第一个数据点和 X 轴上的标签跳过倒数第二个数据点
- dart - 为什么在显示字符串已通过尝试调用时给出 NoSuchMethodFound 错误?