asp.net 按照table导出Excel问题

yangfei_net 2014-08-13 09:23:41
项目中遇见一个问题,需要将数据导出成Excel文件,文件内容格式要和页面table显示的样式保持一致。自己因为方便便采用的是流保存的方式进行导出的。代码如下

虽然达到了按照样式保存的效果,但是导出的Excel文件,在电脑上打开编辑保存时会提示,如下问题:

保存后还会出现一个Excel的样式文件夹,如果把这个文件夹删除后,整个Excel就不能用了。

要想正常使用这个Excel文档,必须在进行编辑之前,先要另存一份Excel后缀的文档,然后使用另存的文档才可以。
这样给用户的感觉太不好了。
我想各位大神,有没有能遇见过这样的问题的或者有没有更好的办法按照页面table样式导出Excel的。请大家多多指教呀SOS
...全文
103 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaotomo 2014-08-14
  • 打赏
  • 举报
回复
用NPOI导出吧 http://www.cnblogs.com/relax/p/3586355.html
gwygwl_2285662869 2014-08-14
  • 打赏
  • 举报
回复
用GridView将数据导出到Excel进行格式化处理 //绑定控件导出到Excel设置 <table border="1" bordercolor="#bed0cd" cellpadding="0" cellspacing="0"> <tr> <td style="font-weight: bold; font-size: 11pt; text-align: center"> 将GridView中数据导出到Excel并进行格式化处理</td> </tr> <tr> <td> <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Size="11pt" ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"> <FooterStyle BackColor="#CCCC99" /> <RowStyle BackColor="#F7F7DE" /> <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="商品ID" HeaderText="商品ID" /> <asp:BoundField DataField="商品名称" HeaderText="商品名称" /> <asp:BoundField DataField="商品介绍" HeaderText="商品介绍" /> <asp:BoundField DataField="商品价格" HeaderText="商品价格" /> </Columns> </asp:GridView> </td> </tr> <tr> <td style="text-align: center"> <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="导出Excel并格式化" style="border-left-color: #3333ff; border-bottom-color: #3333ff; border-top-style: inset; border-top-color: #3333ff; border-right-style: inset; border-left-style: inset; border-right-color: #3333ff; border-bottom-style: inset"/></td> </tr> </table> //处理导出 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } private void Export(string FileType, string FileName) { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF7; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } private void bind() { SqlDataAdapter myda = new SqlDataAdapter("select top 10 GoodsID as 商品ID,GoodsName as 商品名称,GoodsIntroduce as 商品介绍,GoodsPrice as 商品价格 from tb_GoodsInfo", sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds); sqlcon.Close(); GridView1.DataSource = myds; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00"); } }
yangfei_net 2014-08-14
  • 打赏
  • 举报
回复
引用 2 楼 insus 的回复:
try it: http://www.cnblogs.com/insus/archive/2009/02/28/1400266.html
您好,不是打开的问题,我就想是知道的如何通过既要保存页面的table样式,导出的时候 还能生成真正的Excel,而不是HTML代码这样的。
kongwu525 2014-08-14
  • 打赏
  • 举报
回复
你是用WPS打开的吧?用EXCEL打开好像不会有这样的问题。 毕竟这样生成的文件不是真正的EXCEL,你可以用记事本打开你的文件看到的其实是HTML代码;但用记事本打开真正的EXCEL却是乱码。
wangnaisheng 2014-08-13
  • 打赏
  • 举报
回复
Response.ContentType = "application/ms-excel";//"application/ms-excel";//"application/vnd.ms-excel"; Response.Clear(); 最好在导出之前加一句这个。 试试

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧