C#导出EXCEL的时候自动分行和换行?
protectedvoidbtnToExcel_Click(objectsender,EventArgse){Response.Clear();Response.Buff...
protected void btnToExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMdd") + ".xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.lable1.RenderControl(oHtmlTextWriter);
this.lable2.RenderControl(oHtmlTextWriter);
this.lable3.RenderControl(oHtmlTextWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{ }
这个是我到网上找的例子,但是我现在导出的时候三个lable在一行,并且也没分隔开,我想导出的时候lable1占一行 lable2和lable3分别在第2行的A和G
,别的方法也可以,但要写出完整的代码.COPY过来就能用最好啦..Thinks..
你要多少分? 展开
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMdd") + ".xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.lable1.RenderControl(oHtmlTextWriter);
this.lable2.RenderControl(oHtmlTextWriter);
this.lable3.RenderControl(oHtmlTextWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{ }
这个是我到网上找的例子,但是我现在导出的时候三个lable在一行,并且也没分隔开,我想导出的时候lable1占一行 lable2和lable3分别在第2行的A和G
,别的方法也可以,但要写出完整的代码.COPY过来就能用最好啦..Thinks..
你要多少分? 展开
3个回答
展开全部
改成下面这样就行了
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("<table><tr><td>"+Label1.Text+"</td></tr>"+
"<tr><td>" + Label2.Text +
"</td><td></td><td></td><td></td><td></td><td></td><td>" +
Label3.Text + "</td></table>");//把前两行按表格的形式导出,样式之类的也可以自己定义
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)
{
}
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("<table><tr><td>"+Label1.Text+"</td></tr>"+
"<tr><td>" + Label2.Text +
"</td><td></td><td></td><td></td><td></td><td></td><td>" +
Label3.Text + "</td></table>");//把前两行按表格的形式导出,样式之类的也可以自己定义
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)
{
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先加分,我做好拉
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上这位,太过分了吧
为了分才帮人家?
为了分才帮人家?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询