asp.net – Gridview导出到xlsx office07以上版本 -UTF-8格式-可防止gridview中的繁体等字符乱码现象

.net导出 excel 的xlsx 方法有很多种,测试发现导出 xlsx 比 xls 能缩小10倍的空间! 本文通过第三方插件来实现该功能,代码部分转载他人写好的,并做了些改动,可以完美导出 xlsx 格式

Dll下载地址:链接:https://pan.baidu.com/s/1Sw5kxZLYq3JpX9qaU7453A   提取码:5wkc

1.vs中先添加4个dll的引用:

2.App_Code 中添加一个类: ExcelHelper.cs    代码如下,把以下代码直接复制到你的ExcelHelper.cs 保存:

using System;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;  //xls
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;     //xlxs


public class ExcelHelper
{
    /// <summary>      
    /// DataTable导出到Excel文件,自动存在服务器目录下 ,现停用 
    /// </summary>      
    /// <param name="dtSource">源DataTable</param>      
    /// <param name="strHeaderText">表头文本</param>      
    /// <param name="strFileName">保存位置</param>   
    /// <param name="strSheetName">工作表名称</param>   
    /// <Author>CallmeYhz 2015-11-26 10:13:09</Author>      
    public static void ExporTtoFile(DataTable dtSource, string strHeaderText, string strFileName, string strSheetName, string[] oldColumnNames, string[] newColumnNames)
    {
        if (strSheetName == "")
        {
            strSheetName = "Sheet";
        }
        using (MemoryStream ms = ExporTtoMemoryStream(dtSource, strHeaderText, strSheetName, oldColumnNames, newColumnNames))
        {
            //using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
            //{
            //    byte[] data = ms.ToArray();
            //    fs.Write(data, 0, data.Length);
            //    fs.Flush();
            //}
        }
    }

    /// <summary>      
    /// DataTable导出到Excel文件(无表头)另外的是有表头的,存在服務器目錄下,现在停用FileStream
    /// </summary>      
    /// <param name="dtSource">源DataTable</param>      
    /// <param name="strHeaderText">表头文本</param>      
    /// <param name="strFileName">保存位置</param>   
    /// <param name="strSheetName">工作表名称</param>   
    /// <Author>CallmeYhz 2015-11-26 10:13:09</Author>      
    public static void MyExportTtoFile(DataTable dtSource, string strHeaderText, string strFileName, string strSheetName, string[] oldColumnNames, string[] newColumnNames)
    {
        if (strSheetName == "")
        {
            strSheetName = "Sheet";
        }
        MemoryStream getms = new MemoryStream();

        #region 为getms赋值
        if (oldColumnNames.Length != newColumnNames.Length)
        {
            getms = new MemoryStream();
        }
        HSSFWorkbook workbook = new HSSFWorkbook();
        //HSSFSheet sheet = workbook.CreateSheet();// workbook.CreateSheet();   
        ISheet sheet = workbook.CreateSheet(strSheetName);

        #region 右击文件 属性信息
        {
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "http://....../";
            workbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            if (HttpContext.Current.Session["realname"] != null)
            {
                si.Author = HttpContext.Current.Session["realname"].ToString();
            }
            else
            {
                if (HttpContext.Current.Session["username"] != null)
                {
                    si.Author = HttpContext.Current.Session["username"].ToString();
                }
            }                                       //填加xls文件作者信息      
            si.ApplicationName = "NPOI";            //填加xls文件创建程序信息      
            si.LastAuthor = "OA系统";           //填加xls文件最后保存者信息      
            si.Comments = "OA系统自动创建文件";      //填加xls文件作者信息      
            si.Title = strHeaderText;               //填加xls文件标题信息      
            si.Subject = strHeaderText;              //填加文件主题信息      
            si.CreateDateTime = DateTime.Now;
            workbook.SummaryInformation = si;
        }
        #endregion

        ICellStyle dateStyle = workbook.CreateCellStyle();
        IDataFormat format = workbook.CreateDataFormat();
        dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");

        #region 取得列宽
        int[] arrColWidth = new int[oldColumnNames.Length];
        for (int i = 0; i < oldColumnNames.Length; i++)
        {
            arrColWidth[i] = Encoding.GetEncoding(936).GetBytes(newColumnNames[i]).Length;
        }
        /* 
        foreach (DataColumn item in dtSource.Columns) 
        { 
            arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length; 
        } 
         * */

        for (int i = 0; i < dtSource.Rows.Count; i++)
        {
            for (int j = 0; j < oldColumnNames.Length; j++)
            {
                int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][oldColumnNames[j]].ToString()).Length;
                if (intTemp > arrColWidth[j])
                {
                    arrColWidth[j] = intTemp;
                }
            }
            /* 
            for (int j = 0; j < dtSource.Columns.Count; j++) 
            { 
                int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length; 
                if (intTemp > arrColWidth[j]) 
                { 
                    arrColWidth[j] = intTemp; 
                } 
            } 
             * */
        }
        #endregion
        int rowIndex = 0;

        foreach (DataRow row in dtSource.Rows)
        {
            #region 新建表,填充表头,填充列头,样式
            if (rowIndex == 65535 || rowIndex == 0)
            {
                if (rowIndex != 0)
                {
                    sheet = workbook.CreateSheet(strSheetName + ((int)rowIndex / 65535).ToString());
                }


                #region 列头及样式
                {
                    //HSSFRow headerRow = sheet.CreateRow(1);   
                    IRow headerRow = sheet.CreateRow(0);

                    ICellStyle headStyle = workbook.CreateCellStyle();
                    headStyle.Alignment = HorizontalAlignment.Center;
                    IFont font = workbook.CreateFont();
                    font.FontHeightInPoints = 10;
                    font.Boldweight = 700;
                    headStyle.SetFont(font);

                    for (int i = 0; i < oldColumnNames.Length; i++)
                    {
                        headerRow.CreateCell(i).SetCellValue(newColumnNames[i]);
                        headerRow.GetCell(i).CellStyle = headStyle;
                        //设置列宽   
                        sheet.SetColumnWidth(i, (arrColWidth[i] + 1) * 256);
                    }
                    /* 
                    foreach (DataColumn column in dtSource.Columns) 
                    { 
                        headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); 
                        headerRow.GetCell(column.Ordinal).CellStyle = headStyle; 
 
                        //设置列宽    
                        sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); 
                    } 
                     * */
                }
                #endregion

                rowIndex = 1;
            }
            #endregion


            #region 填充内容
            IRow dataRow = sheet.CreateRow(rowIndex);
            //foreach (DataColumn column in dtSource.Columns)   
            for (int i = 0; i < oldColumnNames.Length; i++)
            {
                ICell newCell = dataRow.CreateCell(i);

                string drValue = row[oldColumnNames[i]].ToString();

                switch (dtSource.Columns[oldColumnNames[i]].DataType.ToString())
                {
                    case "System.String"://字符串类型      
                        newCell.SetCellValue(drValue);
                        break;
                    case "System.DateTime"://日期类型      
                        DateTime dateV;
                        DateTime.TryParse(drValue, out dateV);
                        newCell.SetCellValue(dateV);

                        newCell.CellStyle = dateStyle;//格式化显示      
                        break;
                    case "System.Boolean"://布尔型      
                        bool boolV = false;
                        bool.TryParse(drValue, out boolV);
                        newCell.SetCellValue(boolV);
                        break;
                    case "System.Int16"://整型      
                    case "System.Int32":
                    case "System.Int64":
                    case "System.Byte":
                        int intV = 0;
                        int.TryParse(drValue, out intV);
                        newCell.SetCellValue(intV);
                        break;
                    case "System.Decimal"://浮点型      
                    case "System.Double":
                        double doubV = 0;
                        double.TryParse(drValue, out doubV);
                        newCell.SetCellValue(doubV);
                        break;
                    case "System.DBNull"://空值处理      
                        newCell.SetCellValue("");
                        break;
                    default:
                        newCell.SetCellValue("");
                        break;
                }

            }
            #endregion

            rowIndex++;
        }


        using (MemoryStream ms = new MemoryStream())
        {
            workbook.Write(ms);
            ms.Flush();
            ms.Position = 0;

            //sheet.Dispose();   
            sheet = null;
            workbook = null;
            //workbook.Dispose();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet      
            getms = ms;
        }



        #endregion

        using (MemoryStream ms = getms)
        {
            //using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))  //停用
            //{
            //    byte[] data = ms.ToArray();
            //    fs.Write(data, 0, data.Length);
            //    fs.Flush();
            //}
        }
    }



    public class NpoiMemoryStream : MemoryStream
    {
        public NpoiMemoryStream()
        {
            AllowClose = true;
        }

        public bool AllowClose { get; set; }

        public override void Close()
        {
            if (AllowClose)
                base.Close();
        }
    }

    /// <summary>      
    /// DataTable导出到Excel的MemoryStream ,指定两个列名称组 
    /// </summary>      
    /// <param name="dtSource">源DataTable</param>      
    /// <param name="strHeaderText">表头文本</param>      
    /// <param name="strSheetName">工作表名称</param>   
    /// <Author>CallmeYhz 2015-11-26 10:13:09</Author>      
    public static MemoryStream ExporTtoMemoryStream(DataTable dtSource, string strHeaderText, string strSheetName, string[] oldColumnNames, string[] newColumnNames)
    {
        try
        {
            if (oldColumnNames.Length != newColumnNames.Length)
            {
                throw new Exception("新旧列名不一致!!");
                // return new MemoryStream();
            }
            XSSFWorkbook workbook = new XSSFWorkbook();
            //HSSFWorkbook workbook = new HSSFWorkbook();
            //HSSFSheet sheet = workbook.CreateSheet();// workbook.CreateSheet();   
            ISheet sheet = workbook.CreateSheet(strSheetName); //先创建默认第一个sheet

            #region 右击文件 属性信息
            {
                DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                dsi.Company = "LS";
                // workbook.DocumentSummaryInformation = dsi;    // xls 可用
                SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
                if (HttpContext.Current.Session["realname"] != null)
                {
                    si.Author = HttpContext.Current.Session["realname"].ToString();
                }
                else
                {
                    if (HttpContext.Current.Session["username"] != null)
                    {
                        si.Author = HttpContext.Current.Session["username"].ToString();
                    }
                }                                       //填加xls文件作者信息      
                si.ApplicationName = "eflow";            //填加xls文件创建程序信息      
                si.LastAuthor = "OA系统";           //填加xls文件最后保存者信息      
                si.Comments = "OA系统自动创建文件";      //填加xls文件作者信息      
                si.Title = strHeaderText;               //填加xls文件标题信息      
                si.Subject = strHeaderText;              //填加文件主题信息      
                si.CreateDateTime = DateTime.Now;
                // workbook.SummaryInformation = si;   // xls 可用
            }
            #endregion

            ICellStyle dateStyle = workbook.CreateCellStyle();
            IDataFormat format = workbook.CreateDataFormat();
            dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");

            #region 取得列宽
            int[] arrColWidth = new int[oldColumnNames.Length];
            for (int i = 0; i < oldColumnNames.Length; i++)
            {
                arrColWidth[i] = Encoding.GetEncoding("utf-8").GetBytes(newColumnNames[i]).Length;
            }
            /* 
            foreach (DataColumn item in dtSource.Columns) 
            { 
                arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length; 
            } 
             * */

            for (int i = 0; i < dtSource.Rows.Count; i++) //比较列名称和 数据的长度,取最长的那个
            {
                for (int j = 0; j < oldColumnNames.Length; j++)
                {
                    int intTemp = Encoding.GetEncoding("utf-8").GetBytes(dtSource.Rows[i][oldColumnNames[j]].ToString()).Length;
                    if (intTemp > arrColWidth[j])
                    {
                        arrColWidth[j] = intTemp;
                    }
                }
                /* 
                for (int j = 0; j < dtSource.Columns.Count; j++) 
                { 
                    int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length; 
                    if (intTemp > arrColWidth[j]) 
                    { 
                        arrColWidth[j] = intTemp; 
                    } 
                } 
                 * */
            }
            #endregion

            #region 表头及样式  只增加一次
            {
                IRow headerRow = sheet.CreateRow(0); //表头是增加一行
                headerRow.HeightInPoints = 25;
                headerRow.CreateCell(0).SetCellValue(strHeaderText);

                ICellStyle headStyle = workbook.CreateCellStyle(); //表头的style
                headStyle.Alignment = HorizontalAlignment.Center;
                IFont font = workbook.CreateFont();
                font.FontHeightInPoints = 20;
                font.Boldweight = 700;
                headStyle.SetFont(font);
                headerRow.GetCell(0).CellStyle = headStyle;
                //sheet.AddMergedRegion(new Region(0, 0, 0, dtSource.Columns.Count - 1));   
                sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, oldColumnNames.Length - 1));
            }
            #endregion

            ICellStyle rowNameStyle = workbook.CreateCellStyle();  //列名称style
            rowNameStyle.Alignment = HorizontalAlignment.Center;
            IFont fontrowName = workbook.CreateFont();
            fontrowName.FontHeightInPoints = 10;
            fontrowName.Boldweight = 700;
            rowNameStyle.SetFont(fontrowName);

            ICellStyle detailStyle = workbook.CreateCellStyle(); //数据明细行style
            detailStyle.Alignment = HorizontalAlignment.Left;
            detailStyle.VerticalAlignment = VerticalAlignment.Center;
            //style2.SetFont(font);
            detailStyle.IsLocked = false;

            IRow rowsName = sheet.CreateRow(1); //创建一行列名称
            for (int i = 0; i < oldColumnNames.Length; i++)
            {
                rowsName.CreateCell(i).SetCellValue(newColumnNames[i].ToString());
                rowsName.Sheet.SetColumnWidth(i, 4000);
                rowsName.GetCell(i).CellStyle = rowNameStyle;
            }

            int rowIndex = 0;  //行索引
            int sheetCount = 1; //sheet的页数,超过多少行可以新建一个sheet
            foreach (DataRow row in dtSource.Rows)
            {
                rowIndex++;
                if (rowIndex == 1000000)//每页最多导出60000
                {
                    rowIndex = 0;
                    sheetCount++;
                    sheet = workbook.CreateSheet(strSheetName + sheetCount);
                    IRow rows = sheet.CreateRow(0);
                    for (int i = 0; i < oldColumnNames.Length; i++)
                    {
                        rows.CreateCell(i).SetCellValue(newColumnNames[i]);
                        rows.Sheet.SetColumnWidth(i, 4000);
                        rows.GetCell(i).CellStyle = rowNameStyle;
                        sheet.SetColumnWidth(i, (arrColWidth[i] + 1) * 256);  //* 256
                    }
                }

                #region 填充内容
                IRow dataRow = sheet.CreateRow(rowIndex + 1);
                //foreach (DataColumn column in dtSource.Columns)   
                for (int i = 0; i < oldColumnNames.Length; i++)
                {
                    ICell newCell = dataRow.CreateCell(i);

                    string drValue = row[oldColumnNames[i]].ToString();

                    switch (dtSource.Columns[oldColumnNames[i]].DataType.ToString())
                    {
                        case "System.String"://字符串类型      
                            newCell.SetCellValue(drValue);
                            break;
                        case "System.DateTime"://日期类型      
                            DateTime dateV;
                            DateTime.TryParse(drValue, out dateV);
                            //dateV = dateV.Date;
                            newCell.SetCellValue(dateV);
                            newCell.CellStyle = dateStyle; //格式化显示    
                            newCell.SetCellValue(drValue);

   
                            //DateTime dateV;
                            //DateTime.TryParse(drValue, out dateV);
                            //dateV = dateV.Date;
                            //newCell.SetCellValue(dateV.Date);
                            //XSSFCellStyle style = (XSSFCellStyle)workbook.CreateCellStyle();
                            //XSSFDataFormat format2 = (XSSFDataFormat)workbook.CreateDataFormat();
                            //style.DataFormat = format2.GetFormat("yyyy-mm-dd");
                            //newCell.CellStyle = style; //格式化显示                          
                            //newCell.SetCellValue(drValue);

                            break;
                        case "System.Boolean"://布尔型      
                            bool boolV = false;
                            bool.TryParse(drValue, out boolV);
                            newCell.SetCellValue(boolV);
                            break;
                        case "System.Int16"://整型      
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Byte":
                            int intV = 0;
                            int.TryParse(drValue, out intV);
                            newCell.SetCellValue(intV);
                            break;
                        case "System.Decimal"://浮点型      
                        case "System.Double":
                            double doubV = 0;
                            double.TryParse(drValue, out doubV);
                            newCell.SetCellValue(doubV);


                            break;
                        case "System.DBNull"://空值处理      
                            newCell.SetCellValue("");
                            break;
                        default:
                            newCell.SetCellValue("");
                            break;
                    }
                    dataRow.GetCell(i).CellStyle = detailStyle;
                }
                #endregion

            }


            using (NpoiMemoryStream ms = new NpoiMemoryStream())
            {
                ms.AllowClose = false;
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                //ms.Seek(0, SeekOrigin.Begin);
                ms.AllowClose = true;

                //sheet.Dispose();   
                sheet = null;
                workbook = null;
                //workbook.Dispose();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet      
                return ms;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("读取临时内存出错,请稍后重试!!");
        }
    }


    /// <summary>      
    /// WEB上导出DataTable到Excel,默认sheet名是 sheet1    
    /// </summary>      
    /// <param name="dtSource">源DataTable</param>      
    /// <param name="strHeaderText">表头文本</param>      
    /// <param name="strFileName">文件名</param>      
    /// <Author>CallmeYhz 2015-11-26 10:13:09</Author>      
    public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)
    {
        ExportByWeb(dtSource, strHeaderText, strFileName, "sheet1");
    }

    /// <summary>   
    /// WEB导出DataTable到Excel,默认sheet名是 sheet1,需两个表头名称数组
    /// </summary>   
    /// <param name="dtSource">源DataTable</param>   
    /// <param name="strHeaderText">表头文本</param>   
    /// <param name="strFileName">输出文件名,包含扩展名</param>   
    /// <param name="oldColumnNames">要导出的DataTable列数组</param>   
    /// <param name="newColumnNames">导出后的对应列名</param>   
    public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName, string[] oldColumnNames, string[] newColumnNames)
    {
        ExportByWeb(dtSource, strHeaderText, strFileName, "sheet1", oldColumnNames, newColumnNames);
    }

    /// <summary>   
    /// WEB导出DataTable到Excel,自动按datable的列名称分割  
    /// </summary>   
    /// <param name="dtSource">源DataTable</param>   
    /// <param name="strHeaderText">表头文本</param>   
    /// <param name="strFileName">输出文件名</param>   
    /// <param name="strSheetName">工作表名称</param>   
    public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName, string strSheetName)
    {
        HttpContext curContext = HttpContext.Current;

        // 设置编码和附件格式      
        //curContext.Response.ContentType = "application/vnd.ms-excel";
        curContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        curContext.Response.ContentEncoding = Encoding.UTF8;
        curContext.Response.Charset = "";
        curContext.Response.AppendHeader("Content-Disposition",
            "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

        //生成列   
        string columns = "";
        for (int i = 0; i < dtSource.Columns.Count; i++)
        {
            if (i > 0)
            {
                columns += ",";
            }
            columns += dtSource.Columns[i].ColumnName;
        }

        //long fileSize = ms.Length;
        ////加上设置大小下载下来的.xlsx文件打开时才不会报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
        //context.Response.AddHeader("Content-Length", fileSize.ToString());

        curContext.Response.BinaryWrite(ExporTtoMemoryStream(dtSource, strHeaderText, strSheetName, columns.Split(','), columns.Split(',')).ToArray());
        curContext.Response.End();

    }

    /// <summary>   
    /// 导出DataTable到Excel  ,需手动指定列名称的两个数组 
    /// </summary>   
    /// <param name="dtSource">要导出的DataTable</param>   
    /// <param name="strHeaderText">标题文字</param>   
    /// <param name="strFileName">文件名,包含扩展名</param>   
    /// <param name="strSheetName">工作表名</param>   
    /// <param name="oldColumnNames">要导出的DataTable列数组</param>   
    /// <param name="newColumnNames">导出后的对应列名</param>   
    public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName, string strSheetName, string[] oldColumnNames, string[] newColumnNames)
    {
        HttpContext curContext = HttpContext.Current;

        // 设置编码和附件格式      
        curContext.Response.ContentType = "application/vnd.ms-excel";
        curContext.Response.ContentEncoding = Encoding.UTF8;
        curContext.Response.Charset = "";
        curContext.Response.AppendHeader("Content-Disposition",
            "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

        curContext.Response.BinaryWrite(ExporTtoMemoryStream(dtSource, strHeaderText, strSheetName, oldColumnNames, newColumnNames).ToArray());
        curContext.Response.End();
    }

    /// <summary>读取excel      
    /// 默认第一行为表头,导入第一个工作表   
    /// </summary>      
    /// <param name="strFileName">excel文档路径</param>      
    /// <returns></returns>      
    public static DataTable Import(string strFileName)
    {
        DataTable dt = new DataTable();

        HSSFWorkbook hssfworkbook;
        using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
        {
            hssfworkbook = new HSSFWorkbook(file);
        }
        ISheet sheet = hssfworkbook.GetSheetAt(0);
        System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

        IRow headerRow = sheet.GetRow(0);
        int cellCount = headerRow.LastCellNum;

        for (int j = 0; j < cellCount; j++)
        {
            ICell cell = headerRow.GetCell(j);
            dt.Columns.Add(cell.ToString());
        }

        for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
        {
            IRow row = sheet.GetRow(i);
            DataRow dataRow = dt.NewRow();

            for (int j = row.FirstCellNum; j < cellCount; j++)
            {
                if (row.GetCell(j) != null)
                    dataRow[j] = row.GetCell(j).ToString();
            }
            dt.Rows.Add(dataRow);
        }
        return dt;
    }
    /// <summary>   
    /// 从Excel中获取数据到DataTable   
    /// </summary>   
    /// <param name="strFileName">Excel文件全路径(服务器路径)</param>   
    /// <param name="SheetName">要获取数据的工作表名称</param>   
    /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
    /// <returns></returns>   
    public static DataTable RenderDataTableFromExcel(string strFileName, string SheetName, int HeaderRowIndex)
    {
        using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
        {
            IWorkbook workbook = new HSSFWorkbook(file);
            ISheet sheet = workbook.GetSheet(SheetName);
            return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);
        }
    }

    /// <summary>   
    /// 从Excel中获取数据到DataTable   
    /// </summary>   
    /// <param name="strFileName">Excel文件全路径(服务器路径)</param>   
    /// <param name="SheetIndex">要获取数据的工作表序号(从0开始)</param>   
    /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
    /// <returns></returns>   
    public static DataTable RenderDataTableFromExcel(string strFileName, int SheetIndex, int HeaderRowIndex)
    {
        using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
        {
            IWorkbook workbook = new HSSFWorkbook(file);
            string SheetName = workbook.GetSheetName(SheetIndex);
            return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);
        }
    }

    /// <summary>   
    /// 从Excel中获取数据到DataTable   
    /// </summary>   
    /// <param name="ExcelFileStream">Excel文件流</param>   
    /// <param name="SheetName">要获取数据的工作表名称</param>   
    /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
    /// <returns></returns>   
    public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, string SheetName, int HeaderRowIndex)
    {
        IWorkbook workbook = new HSSFWorkbook(ExcelFileStream);
        ExcelFileStream.Close();
        return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);
    }

    /// <summary>   
    /// 从Excel中获取数据到DataTable   
    /// </summary>   
    /// <param name="ExcelFileStream">Excel文件流</param>   
    /// <param name="SheetIndex">要获取数据的工作表序号(从0开始)</param>   
    /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
    /// <returns></returns>   
    public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, int SheetIndex, int HeaderRowIndex)
    {
        IWorkbook workbook = new HSSFWorkbook(ExcelFileStream);
        ExcelFileStream.Close();
        string SheetName = workbook.GetSheetName(SheetIndex);
        return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);
    }

    /// <summary>   
    /// 从Excel中获取数据到DataTable   
    /// </summary>   
    /// <param name="workbook">要处理的工作薄</param>   
    /// <param name="SheetName">要获取数据的工作表名称</param>   
    /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
    /// <returns></returns>   
    public static DataTable RenderDataTableFromExcel(IWorkbook workbook, string SheetName, int HeaderRowIndex)
    {
        ISheet sheet = workbook.GetSheet(SheetName);
        DataTable table = new DataTable();
        try
        {
            IRow headerRow = sheet.GetRow(HeaderRowIndex);
            int cellCount = headerRow.LastCellNum;

            for (int i = headerRow.FirstCellNum; i < cellCount; i++)
            {
                DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
                table.Columns.Add(column);
            }

            int rowCount = sheet.LastRowNum;

            #region 循环各行各列,写入数据到DataTable
            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
            {
                IRow row = sheet.GetRow(i);
                DataRow dataRow = table.NewRow();
                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    ICell cell = row.GetCell(j);
                    if (cell == null)
                    {
                        dataRow[j] = null;
                    }
                    else
                    {
                        //dataRow[j] = cell.ToString();   
                        switch (cell.CellType)
                        {
                            case CellType.Blank:
                                dataRow[j] = null;
                                break;
                            case CellType.Boolean:
                                dataRow[j] = cell.BooleanCellValue;
                                break;
                            case CellType.Numeric:
                                dataRow[j] = cell.ToString();
                                break;
                            case CellType.String:
                                dataRow[j] = cell.StringCellValue;
                                break;
                            case CellType.Error:
                                dataRow[j] = cell.ErrorCellValue;
                                break;
                            case CellType.Formula:
                            default:
                                dataRow[j] = "=" + cell.CellFormula;
                                break;
                        }
                    }
                }
                table.Rows.Add(dataRow);
                //dataRow[j] = row.GetCell(j).ToString();   
            }
            #endregion
        }
        catch (System.Exception ex)
        {
            table.Clear();
            table.Columns.Clear();
            table.Columns.Add("出错了");
            DataRow dr = table.NewRow();
            dr[0] = ex.Message;
            table.Rows.Add(dr);
            return table;
        }
        finally
        {
            //sheet.Dispose();   
            workbook = null;
            sheet = null;
        }
        #region 清除最后的空行
        for (int i = table.Rows.Count - 1; i > 0; i--)
        {
            bool isnull = true;
            for (int j = 0; j < table.Columns.Count; j++)
            {
                if (table.Rows[i][j] != null)
                {
                    if (table.Rows[i][j].ToString() != "")
                    {
                        isnull = false;
                        break;
                    }
                }
            }
            if (isnull)
            {
                table.Rows[i].Delete();
            }
        }
        #endregion
        return table;
    }
}

3.回到你需要导出GridView的页面,建立一个button,后台click事件加入以下代码,此处导出两列数据:

string[] oldColumn = new string[] {
"number" ,
"TransactionDate"
};

string[] newColumn = new string[] {
"PO号" ,
"PO日期"
};

DataTable dt = GetDataTable(); //这个是你自己的查询数据的代码,得到一个 DataTable 类型的 dt 就行了
if (dt.Rows.Count > 0)
{
//调用改写的NPOI方法
ExcelHelper.ExportByWeb(dt, "报表", "ReportHeader" + ".xlsx", "sheet1", oldColumn, newColumn);
}

//oldColumn 指 DataTable 查询的数据库的列名
//newColumn  指显示在Excel 上的列名称。

----欢迎转载----
THE END
分享
二维码
< <上一篇
下一篇>>