导出代码如下:
gvProductInfo.AllowPaging = False
            Response.Clear()
            Response.Buffer = True
            Response.Charset = "GB2312"
            Response.AppendHeader("Content-Disposition", "attachment;filename=Sheet.xls")
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
            '设置输出流为简体中文 
            Response.ContentType = "application/ms-excel"
            '设置输出文件类型为excel文件。 
            Me.EnableViewState = False
            Dim myCItrad As New System.Globalization.CultureInfo("ZH-CN", True)
            Dim oStringWriter As New System.IO.StringWriter(myCItrad)
            Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
            gvProductInfo.RenderControl(oHtmlTextWriter)
            Response.Write(oStringWriter.ToString())
            Response.[End]()
其中导出的数据中有一列数据为字符类型的数字。但是导出到Excel到,自动成了数字,请问如果在导出时把这一列设置为文件格式进行导出?

解决方案 »

  1.   

    字符型的数字变成了数字,是因为你没有设置单元格格式。
    需要在数据绑定的时候,给单元格添加一个属性
    我是用C#写的,你参考一下
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    if (!IsPostBack) {
    BindGridView();
    }
        } private void BindGridView() {
    string strQuery = "SELECT top 10 * FROM EMPLOYEE";
    SqlHelper help = new SqlHelper();
    DataTable dt = help.GetDataTable(strQuery, null);
    gvEmployee.DataSource = dt.DefaultView;
    gvEmployee.DataBind();
    } protected void btnExport_Click(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);
    gvEmployee.RenderControl(htw);
    Response.Write(style);//注意
    Response.Write(sw.ToString());
    Response.End();
    }        //注意:必须覆盖此方法
    public override void VerifyRenderingInServerForm(Control control) {
    //base.VerifyRenderingInServerForm(control);
    }
    protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e) {
    e.Row.Cells[6].Attributes.Add("class", "text");//在数据绑定中设置格式
    }
    }