ASP.NET 导出Excel表格

    /// <summary>
    /// 导出Excel表单 
    /// 特别说明:如果Excel有标题,请在第二个参数中使用如下格式:文件名称^标题
    /// </summary>
    /// <param name="dt">数据源</param>
    /// <param name="FileName">文件名称[标题]</param>
    public void ExportExcel(DataTable dt, string FileName)
    {
        string contentTitle = "";  //文档内容标题
        string fileNames = "";     //Excel文件名称
        string[] fileStr = FileName.Split('^');//Excel文件名称^文档内容标题
        if (FileName.Contains('^'))//文档有标题
        {
            fileNames = fileStr[0].ToString();//Excel文件名称
            contentTitle = fileStr[1].ToString();//文档内容标题
        }
        else //文档没有标题
        {
            fileNames = FileName;//Excel文件名称
        }
        Workbook workbook = new Workbook(); //工作簿
        Worksheet sheet = workbook.Worksheets[0]; //工作表
        Cells cells = sheet.Cells;//单元格
        //样式2
        Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
        style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
        style2.Font.Name = "宋体";//文字字体
        style2.Font.Size = 11;//文字大小
        style2.Font.IsBold = true;//粗体
        style2.IsTextWrapped = true;//单元格内容自动换行
        style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
        style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
        style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
        style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

        //样式3
        Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式
        style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中
        style3.Font.Name = "宋体";//文字字体
        style3.Font.Size = 11;//文字大小
        //style3.Borders[BorderType.LeftBorder].Color = System.Drawing.Color.Black;
        //style3.Borders[BorderType.RightBorder].Color = System.Drawing.Color.Black;
        //style3.Borders[BorderType.TopBorder].Color = System.Drawing.Color.Black;
        //style3.Borders[BorderType.BottomBorder].Color = System.Drawing.Color.Black;
        style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
        style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
        style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
        style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

        int Colnum = dt.Columns.Count;//表格列数
        int Rownum = dt.Rows.Count;//表格行数
        int initRow = 0;
        if (!string.IsNullOrEmpty(contentTitle))
        {   //为标题设置样式    
            Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
            styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
            styleTitle.Font.Name = "宋体";//文字字体
            styleTitle.Font.Size = 16;//文字大小
            styleTitle.Font.IsBold = true;//粗体
            //生成行1 标题行   
            cells.Merge(0, 0, 1, Colnum);//合并单元格
            cells[0, 0].PutValue(contentTitle);//填写内容
            cells[0, 0].SetStyle(styleTitle);
            cells.SetRowHeight(0, 38);
            initRow++;
        }
        //生成行2 列名行
        for (int i = 0; i < Colnum; i++)
        {
            cells[initRow, i].PutValue(dt.Columns[i].ColumnName);
            cells[initRow, i].SetStyle(style2);
            cells.SetRowHeight(1, 24);
        }
        initRow++;
        //生成数据行
        for (int i = 0; i < Rownum; i++)
        {
            for (int k = 0; k < Colnum; k++)
            {
                if (IsNumeric(dt.Rows[i][k].ToString()) && dt.Rows[i][k].ToString().Length <= 11 && !IsZeroStartText(dt.Rows[i][k].ToString()))
                {
                    cells[initRow + i, k].PutValue(dt.Rows[i][k].ToString(), true);
                }
                else
                {
                    cells[initRow + i, k].PutValue(dt.Rows[i][k].ToString());
                }
                //style3.Number = 49;//1表示保留证书,49全部数据
                cells[initRow + i, k].SetStyle(style3);
            }
            cells.SetRowHeight(initRow + i, 20);
        }
        sheet.AutoFitColumns();//让各列自适应宽度,这个很有用。  
        MemoryStream ms = workbook.SaveToStream();
        byte[] bytes = ms.ToArray();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileNames, System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值