.net 导出Excel,设置Excel页眉及单元格换行方法

///

    /// 从GridView导出     ///     /// 导出的数据集     /// 导出的路径     ///     public string Export(DataSet ds, string path)     {         GridView GV = new GridView();//实例化一个Gridview         try         {             GV.DataSource = ds;             GV.AllowPaging = false;//禁止分页             GV.DataBind();//绑定数据             // GV.HeaderStyle.Height = 30;//设置表头的行高             GV.HeaderStyle.Font.Size = 10;             //GV.HeaderStyle.BackColor = System.Drawing.Color.Gray;//设置表头的背景色             // GV.HeaderStyle.Font.Bold = true;//设置表头加粗             GV.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;//设置表头居中             // GV.BackColor = System.Drawing.Color.FromArgb(255, 255, 153);//设置背景色             GV.HorizontalAlign = HorizontalAlign.Center;//设置居中             GV.RowStyle.HorizontalAlign = HorizontalAlign.Center;//设置居中             GV.RowStyle.Font.Size = 10;             GV.HeaderRow.Cells[1].Width = 140;//部门             if (drpType.SelectedValue == "1")             {                 GV.HeaderRow.Cells[5].Width = 90;                 GV.HeaderRow.Cells[6].Width = 90;                 GV.HeaderRow.Cells[8].Width = 90;                 GV.HeaderRow.Cells[9].Width = 180;             }             if (drpType.SelectedValue == "2")             {                 GV.HeaderRow.Cells[4].Width = 70;                 GV.HeaderRow.Cells[5].Width = 85;                 GV.HeaderRow.Cells[6].Width = 90;             }             if (drpType.SelectedValue == "3")             {                 GV.HeaderRow.Cells[5].Width = 90;                 GV.HeaderRow.Cells[6].Width = 90;                 GV.HeaderRow.Cells[8].Width = 90;

                GV.HeaderRow.Cells[7].Width = 100;                 GV.HeaderRow.Cells[9].Width = 180;             }             if (drpType.SelectedValue == "4")             {                 GV.HeaderRow.Cells[4].Width = 90;                 GV.HeaderRow.Cells[5].Width = 90;                 GV.HeaderRow.Cells[8].Width = 90;

                GV.HeaderRow.Cells[6].Width = 65;                 GV.HeaderRow.Cells[9].Width = 280;             }             if (drpType.SelectedValue == "5")             {                 GV.HeaderRow.Cells[1].Width = 250;             }             OutPutExcel(GV, Page, path);             return "导出成功!";         }         catch (Exception exc)         {             return exc.Message;//捕捉异常信息         }     }

 #region 将控件内容导出到excel
    /// 
    /// 将控件内容导出到excel
    /// 
    /// 需要导出内容的控件
    /// 提供Reponse事件的page
    /// 导出的文件名
    public void OutPutExcel(System.Web.UI.Control ExportObj, System.Web.UI.Page page, string fileName)
    {
        //定义文档类型、字符编码

        page.Response.Clear();
        page.Response.Buffer = true;
        page.Response.Charset = "GB2312";
        //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
        //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm
        page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        //Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档

        page.Response.ContentType = "application/ms-excel";

        System.Globalization.CultureInfo cult = new System.Globalization.CultureInfo("zh-CN", true);

        // 定义一个输入流
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(cult);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        //设置excel页眉,格式如下:
        //
        //&L&10:靠左,10号字体
        //\000A:换行
        StringBuilder printHead = new StringBuilder();
        printHead.Append("");
        oHtmlTextWriter.WriteLine("");
        oHtmlTextWriter.WriteLine(printHead.ToString());

        ExportObj.RenderControl(oHtmlTextWriter);

        //excel单元格换行,先将要换行的单元格数据加入标识字符比如"
",然后输出时将
替换成
page.Response.Write(oStringWriter.ToString().Replace("
", "
")); page.Response.End(); } #endregion

你可能感兴趣的:(net)