asp.net 如何将网页格式的excel转换为真正的excel?

2025-02-27 18:15:11
推荐回答(2个)
回答1:

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
Page page = new Page();
HtmlForm form = new HtmlForm();
GridView1.EnableViewState = false;
page.EnableEventValidation = false;
page.DesignerInitialize();
page.Controls.Add(form);
form.Controls.Add(GridView1);
page.RenderControl(htw);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application nd.ms-excel";
string name = "Keyword_Report-" + System.DateTime.Now.ToShortDateString();
Response.AddHeader("Content-Disposition", "attachment;filename=" + name + ".xls");
Response.Charset = "gb2312";
Response.ContentEncoding = Encoding.UTF8;
Response.Write(sb.ToString());
Response.End();

我测试过 可以用

回答2:

我前阵子刚弄过导出Excel,封装好的方法。需要的话,留个邮箱我发你。