我在做把查询结果导入到 excel 中,到成功了,但excel中格式全变了,代码如下,导出数据到execl
protected void Button2_Click(object sender, EventArgs e)
{
ToFiles(rpOutlet);
}
private void ToFiles(System.Web.UI.Control ctl)
{
System.Web.HttpContext HC = System.Web.HttpContext.Current;
HC.Response.Clear();
HC.Response.Buffer = true;
HttpContext.Current.Response.Charset = "UTF-8";
HC.Response.ContentEncoding = System.Text.Encoding.Default;
HC.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HC.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState=false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter btw = new System.Web.UI.HtmlTextWriter(sw);
ctl.RenderControl(btw);
HC.Response.Write(sw.ToString());
HC.Response.End();
}但是在 excel 中格式是这样的,<tr>
<td class="lstNorm">
<a href="cm_member_view.aspx?mem_sid=20091005225051105657"><img src="pix/ico_view.gif" border="0" alt="view member details"></a>
</td>
<td class="lstNorm" style="font-size:12pt;">1.</td>
<td class="lstNorm">Calvin Jian </td>
<td class="lstNorm">Guangdong </td> <td class="lstNorm">The Westin Guangzhou<br /><br / </td> <td class="lstNorm">Prego @ Westin Guangzhou<br />Qba @ Westin Guangzhou<br />Taste @ Westin Guangzhou<br / </td>
<td class="lstNorm"> </td>
</tr> 这不是我想要的啊,
我只想要导出前的格式,能不能修改啊,代码那里需要修改,指教
protected void Button2_Click(object sender, EventArgs e)
{
ToFiles(rpOutlet);
}
private void ToFiles(System.Web.UI.Control ctl)
{
System.Web.HttpContext HC = System.Web.HttpContext.Current;
HC.Response.Clear();
HC.Response.Buffer = true;
HttpContext.Current.Response.Charset = "UTF-8";
HC.Response.ContentEncoding = System.Text.Encoding.Default;
HC.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HC.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState=false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter btw = new System.Web.UI.HtmlTextWriter(sw);
ctl.RenderControl(btw);
HC.Response.Write(sw.ToString());
HC.Response.End();
}但是在 excel 中格式是这样的,<tr>
<td class="lstNorm">
<a href="cm_member_view.aspx?mem_sid=20091005225051105657"><img src="pix/ico_view.gif" border="0" alt="view member details"></a>
</td>
<td class="lstNorm" style="font-size:12pt;">1.</td>
<td class="lstNorm">Calvin Jian </td>
<td class="lstNorm">Guangdong </td> <td class="lstNorm">The Westin Guangzhou<br /><br / </td> <td class="lstNorm">Prego @ Westin Guangzhou<br />Qba @ Westin Guangzhou<br />Taste @ Westin Guangzhou<br / </td>
<td class="lstNorm"> </td>
</tr> 这不是我想要的啊,
我只想要导出前的格式,能不能修改啊,代码那里需要修改,指教
解决方案 »
- ASP.NET MVC中URL传递参数
- 没法将生产的ExtenShell.dll文件拷贝到c:\Winnt\assembly目录中。
- asp.net网站受到了url攻击.
- 请问asp.net(c#)如何把数据库的数据写入数组,然后把数组传入前台的js中
- 求助:怎么设置连接 点击前后状态变化 <a href="***.aspx" style="color:White; ">More... </a>
- 求救!!数据库INSERT出错,给指点一下!!!
- 有关PlaceHolder1问题
- 最近很郁闷,请高人指路,有关学习编程.........
- 请PDA高手帮帮忙啊
- 如何给一个BUTTON按钮同时添加前台脚本和后台代码?
- Asp.net邮件发送问题
- Web测试一般测试的什么呢?
否则会是一个普通的表格。你需要把样式也写进去。
根据LINK中样式,设置单元格样式
protected void Btn_ExportClick(object sender, EventArgs e)
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{}
Excel.Style s = (Excel.Style)((Excel.Range)sheet.Cells[1,2]).Style;
s.Font.Size = 20;
s.Font.Bold = true;
整个SHEET设为文本样式,
那样无论什么样的数据都能“原样”显示,
{
System.Web.HttpContext HC = System.Web.HttpContext.Current;
HC.Response.Clear();
HC.Response.Buffer = true;
HttpContext.Current.Response.Charset = "UTF-8";
HC.Response.ContentEncoding = System.Text.Encoding.Default;
HC.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HC.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState=false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter btw = new System.Web.UI.HtmlTextWriter(sw);
ctl.RenderControl(btw);
HC.Response.Write(sw.ToString());
HC.Response.End();
}怎么在代码上面,设置格式,让它完整的输出?