我为报告创建了动态网格,并保留了一个选项作为导出到Excel。我用html Render-Control导出但我的问题是当我试图在excel中导出时,我得到了空白的Excel表格。任何人都可以帮助我。提前致谢
Response.Clear();
string date_only_for_excel = DateTime.Now.Date.ToString("dd/MM/yyyy");
string FileName = "REPORT_" + date_only_for_excel + ".xls";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + "");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
htmlWrite.Write("<table><tr><td colspan='24'></td></tr>");
htmlWrite.Write("<table><tr><td colspan='24'></td></tr>");
//htmlWrite.Write("<table><tr><td colspan='24' style='text-align:left;font-size:22;color:#f79646;margin-left:100px;'>BOQ REPORT</td></tr>");
htmlWrite.Write("<table><tr><td colspan='24'></td></tr>");
htmlWrite.Write("<table><tr><td colspan='24'></td></tr>");
htmlWrite.Write("<table><tr><td colspan='24'></td></tr>");
Div2.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
我创建了动态网格并添加到此面板
<asp:Panel ID="Export_Panel" runat="server">
</asp:Panel>
答案 0 :(得分:0)
使用gridView1.RenderControl(htmlWrite);
代替
Div2.RenderControl(htmlWrite);
答案 1 :(得分:0)
你可以试试这个:
protected void ExportToExcel(GridView gv)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=YourFileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (var sw = new StringWriter())
{
var hw = new HtmlTextWriter(sw);
//To Export all pages
gv.AllowPaging = false;
gv.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in gv.HeaderRow.Cells)
{
cell.BackColor = gv.HeaderStyle.BackColor;
}
foreach (GridViewRow row in gv.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex%2 == 0)
{
cell.BackColor = gv.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = gv.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
gv.RenderControl(hw);
//style to format numbers to string
var style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}