C#导出EXCEL的时候自动分行和换行?

2025-01-07 04:53:36
推荐回答(3个)
回答1:

改成下面这样就行了
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMdd") + ".xls");
Response.ContentEncoding = System.Text.Encoding.Default;//设置成默认的编码,用记事本打开导出的xls文件可以直接修改
Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
oHtmlTextWriter.Write("

"+
"
"+Label1.Text+"
" + Label2.Text +
"
" +
Label3.Text + "
");//把前两行按表格的形式导出,样式之类的也可以自己定义
GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
oStringWriter.Close();
oStringWriter.Dispose();
oHtmlTextWriter.Close();
oHtmlTextWriter.Dispose();
}
public override void VerifyRenderingInServerForm(Control control)
{
}

回答2:

先加分,我做好拉

回答3:

楼上这位,太过分了吧

为了分才帮人家?