我在做把查询结果导入到 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&nbsp;</td>
<td class="lstNorm">Guangdong&nbsp;</td> <td class="lstNorm">The Westin Guangzhou<br /><br / &nbsp;</td> <td class="lstNorm">Prego @ Westin Guangzhou<br />Qba @ Westin Guangzhou<br />Taste @ Westin Guangzhou<br / &nbsp;</td>
<td class="lstNorm">&nbsp;</td>
</tr> 这不是我想要的啊,
我只想要导出前的格式,能不能修改啊,代码那里需要修改,指教

解决方案 »

  1.   

    class="lstNorm" 等,这些样式都是需要在文件中的。
    否则会是一个普通的表格。你需要把样式也写进去。
      

  2.   

    要把lstNorm的样式写成<style></style>的方法用css没法link
      

  3.   

    class="lstNorm"没有找到样式
    根据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; 
      

  4.   

    可以直接用EXCEL的对象操作,
    整个SHEET设为文本样式,
    那样无论什么样的数据都能“原样”显示,
      

  5.   

    表格是啥样式导出就是啥样式的,但要在table是上直接写样式,不能放在.css中
      

  6.   

    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();
        }怎么在代码上面,设置格式,让它完整的输出?
      

  7.   

    直接用html控件设置属性连接你的样式将html导入到Excel中