在NET中使用NPOI导出Excel

前段时间客户要求网页上显示的报表能导出成Excel文件,而且要求Excel有好看的样式,虽然导出Excel的方式很多,但是都达不到要求,后来在网上找到使用NPOI组件的方式导出,自己进行调试,发现完全可以达到要求,为了使用方便和客户以后的需求,自己写了几个类对NPOI提供的方法进行了加工,代码如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//导出excel 相关
using System.IO;
using System.Data;
using System.Web;
using NPOI;
using NPOI.HSSF.Model;
using NPOI.POIFS.FileSystem;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Record;
using NPOI.HSSF.Util;
//
using System.Web.UI.WebControls;


namespace Eap.Utils
{
    #region 字体样式模板

        /************************************************************************/
        /*                 字体样式模板默认样式                                 */
        /************************************************************************/
        public class FontDefaultTemplate
        {
            public FontDefaultTemplate(HSSFWorkbook hSSFWorkbook)
            {
                mHSSFFont = hSSFWorkbook.CreateFont();

                mHSSFFont.FontName = "微软雅黑"; //字体类型
                mHSSFFont.FontHeightInPoints = 18;   //字号
                mHSSFFont.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL;   //字形  普通
                mHSSFFont.Color = HSSFColor.BLACK.index; //字体颜色
                mHSSFFont.Underline = HSSFFont.U_NONE;   //下划线    无
                mHSSFFont.IsItalic = false;  //斜体    无
                mHSSFFont.IsStrikeout = false;   //删除线    无  

            }

            public HSSFFont FontDefaultStyle
            {
                get { return mHSSFFont; }
            }

            private HSSFFont mHSSFFont;
        }  

        /************************************************************************/
        /*                字体样式模板                                       */
        /************************************************************************/
        public static class HSSFFontTemplates
        {
            ///


            /// 大标题1
            ///

            /// HSSFWorkbook
            /// HSSFFont
            public static HSSFFont FontTitle1(HSSFWorkbook hSSFWorkbook)
            {
                FontDefaultTemplate fontDefaultTemplate = new FontDefaultTemplate(hSSFWorkbook);
                HSSFFont font = fontDefaultTemplate.FontDefaultStyle;

                return font;
            }

            ///


            /// 大标题2
            ///

            /// HSSFWorkbook
            /// HSSFFont
            public static HSSFFont FontTitle2(HSSFWorkbook hSSFWorkbook)
            {
                FontDefaultTemplate fontDefaultTemplate = new FontDefaultTemplate(hSSFWorkbook);
                HSSFFont font = fontDefaultTemplate.FontDefaultStyle;

                font.FontHeightInPoints = 11;   //字号
                font.Color = HSSFColor.BLUE.index; //字体颜色

                return font;
            }

            ///


            /// 列标题1
            ///

            /// HSSFWorkbook
            /// HSSFFont
            public static HSSFFont FontColTitle1(HSSFWorkbook hSSFWorkbook)
            {
                FontDefaultTemplate fontDefaultTemplate = new FontDefaultTemplate(hSSFWorkbook);
                HSSFFont font = fontDefaultTemplate.FontDefaultStyle;

                font.FontHeightInPoints = 11;   //字号
                font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;   //字形    加粗         

                return font;
            }

            ///


            /// 内容1
            ///

            /// HSSFWorkbook
            /// HSSFFont
            public static HSSFFont FontContent1(HSSFWorkbook hSSFWorkbook)
            {
                FontDefaultTemplate fontDefaultTemplate = new FontDefaultTemplate(hSSFWorkbook);
                HSSFFont font = fontDefaultTemplate.FontDefaultStyle;

                font.FontHeightInPoints = 11;   //字号
                font.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL;   //字形    加粗
                font.Color = HSSFColor.BLACK.index; //字体颜色        

                return font;
            }
        }

    #endregion
   
    #region 单元格样式模板

        /************************************************************************/
        /*             单元格样式默认模板                                            */
        /************************************************************************/
        public class CellStyleDefaultTemplate
        {
            public CellStyleDefaultTemplate(HSSFWorkbook hSSFWorkbook, HSSFFont hSSFFont)
            {
                if (null == mHSSFCellStyle)
                {
                    mHSSFCellStyle = hSSFWorkbook.CreateCellStyle();
                }

                if (null != hSSFFont)
                {
                    mHSSFCellStyle.SetFont(hSSFFont);    //对应的字体样式
                }
               
                mHSSFCellStyle.Alignment = HSSFCellStyle.ALIGN_CENTER;   //左右对齐  居中
                mHSSFCellStyle.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER;    //上下对齐  居中

                mHSSFCellStyle.BorderTop = HSSFCellStyle.BORDER_THIN;    //上边框
                mHSSFCellStyle.BorderBottom = HSSFCellStyle.BORDER_THIN;    //下边框
                mHSSFCellStyle.BorderLeft = HSSFCellStyle.BORDER_THIN;     //左边框
                mHSSFCellStyle.BorderRight = HSSFCellStyle.BORDER_THIN;     //右边框

                mHSSFCellStyle.TopBorderColor = HSSFColor.BLACK.index;    //上边框颜色
                mHSSFCellStyle.BottomBorderColor = HSSFColor.BLACK.index;    //下边框颜色
                mHSSFCellStyle.LeftBorderColor = HSSFColor.BLACK.index;    //左边框颜色
                mHSSFCellStyle.RightBorderColor = HSSFColor.BLACK.index;    //右边框颜色

                mHSSFCellStyle.WrapText = false; //自动换行  不自动换行 
                mHSSFCellStyle.FillBackgroundColor = HSSFColor.WHITE.index;  //前景色    白色
                mHSSFCellStyle.FillForegroundColor = HSSFColor.WHITE.index;    //背景色    白色
                mHSSFCellStyle.FillPattern = HSSFCellStyle.SOLID_FOREGROUND; //填充方式  全部填充
            }

            public HSSFCellStyle CellStyleDefault
            {
                get{ return mHSSFCellStyle; }
            }

            private HSSFCellStyle mHSSFCellStyle;           
        }
       
        /************************************************************************/
        /*                    单元格样式新模板                                   */
        /************************************************************************/
        public static class CellStyleTemplates
        {
            ///


            /// 单元格样式 标题1
            ///

            /// HSSFWorkbook
            /// HSSFFont
            /// HSSFCellStyle
            public static HSSFCellStyle CellStyleTitle1(HSSFWorkbook hSSFWorkbook, HSSFFont hSSFFont)
            {
                CellStyleDefaultTemplate defaultTemplate = new CellStyleDefaultTemplate(hSSFWorkbook, hSSFFont);
                HSSFCellStyle style = defaultTemplate.CellStyleDefault;

                style.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;    //背景色 

                return style;
            }

            ///


            /// 单元格样式 列标题
            ///

            /// HSSFWorkbook
            /// HSSFFont
            /// HSSFCellStyle
            public static HSSFCellStyle CellStyleColTitle1(HSSFWorkbook hSSFWorkbook, HSSFFont hSSFFont)
            {
                CellStyleDefaultTemplate defaultTemplate = new CellStyleDefaultTemplate(hSSFWorkbook, hSSFFont);
                HSSFCellStyle style = defaultTemplate.CellStyleDefault;

                style.FillForegroundColor = HSSFColor.LIGHT_CORNFLOWER_BLUE.index;    //背景色

                return style;
            }

            ///


            /// 单元格样式
            ///

            /// HSSFWorkbook
            /// HSSFFont
            /// HSSFCellStyle
            public static HSSFCellStyle CellStyleContent1(HSSFWorkbook hSSFWorkbook, HSSFFont hSSFFont)
            {
                CellStyleDefaultTemplate defaultTemplate = new CellStyleDefaultTemplate(hSSFWorkbook, hSSFFont);
                HSSFCellStyle style = defaultTemplate.CellStyleDefault;

                return style;
            }

        }

    #endregion
  
    #region Excel模板导出

        public class WorkBookHelper
        {
            public WorkBookHelper(string sheetName)
            {
                mRowIndex = 0;  //0 行留作 标题用,这里从1 开始
                mColIndex = 0;
                mMaxColIndex = 0;
                //工作文件
                mHSSFWorkbook = new HSSFWorkbook();
               
                //工作薄
                mHSSFSheet = mHSSFWorkbook.CreateSheet(sheetName);
                mSheetName = sheetName;

                //样式
                mCellStyleTitle = CellStyleTemplates.CellStyleTitle1(mHSSFWorkbook, HSSFFontTemplates.FontTitle1(mHSSFWorkbook));
                mCellStyleColTitle = CellStyleTemplates.CellStyleColTitle1(mHSSFWorkbook, HSSFFontTemplates.FontColTitle1(mHSSFWorkbook));
                mCellStyleContent = CellStyleTemplates.CellStyleContent1(mHSSFWorkbook, HSSFFontTemplates.FontContent1(mHSSFWorkbook));

                //标题
                HSSFRow row = mHSSFSheet.CreateRow(0);
                HSSFCell cell = row.CreateCell(0);
                cell.CellStyle = mCellStyleTitle;
            }

            public HSSFRow CreateRows()
            {
                return mHSSFSheet.CreateRow(++mRowIndex);
            }

            public HSSFRow CreateRows(int r)
            {
                ++mRowIndex;

                if (r <= 0)
                    r = mRowIndex;

                return mHSSFSheet.CreateRow(r);
            }

            public void CreateHeaders(int r, int c, string[] colsHeader, short rowHeight)
            {
                HSSFRow row = CreateRows(r);
                if (c <= 0) c = 0;
                row.HeightInPoints = (rowHeight > 0) ? rowHeight : mDefaultHeight;
                mColIndex = c + colsHeader.Length;

                if (mMaxColIndex < mColIndex)
                    mMaxColIndex = mColIndex;

                HSSFCell cell;

                for (int i = 0; i <= (mColIndex - c - 1); i++)
                {
                    cell = row.CreateCell(c + i);
                    cell.SetCellValue(colsHeader[i]);
                    cell.CellStyle = mCellStyleColTitle;
                }
            }

            public void CreateRowByData(int r, int c, string[] rowData, short rowHeight)
            {
                HSSFRow row = CreateRows(r);
                if (c <= 0) c = 0;
                row.HeightInPoints = (rowHeight > 0) ? rowHeight : mDefaultHeight;

                HSSFCell cell;

                for (int i = 0; i <= mColIndex - 1; i++)
                {
                    cell = row.CreateCell(c + i);
                    cell.SetCellValue(rowData[i]);
                    cell.CellStyle = mCellStyleContent;
                }
            }

            public void CreateTableByData(int r, int c, DataTable data, short rowHeight)
            {
                HSSFRow row;
                HSSFCell cell;
                short height = (rowHeight > 0) ? rowHeight : mDefaultHeight;

                foreach(DataRow eachRow in data.Rows)
                {
                    row = CreateRows(r);
                    row.HeightInPoints = height;

                    for (int i = 0; i <= mColIndex - 1; i++)
                    {
                        cell = row.CreateCell(c + i);
                        cell.SetCellValue(eachRow[i].ToString());
                        cell.CellStyle = mCellStyleContent;
                    }
                }
            }

            public void CreateTableByData(int r, int c, DataTable data, string[] selectColNames, short rowHeight)
            {
                HSSFRow row;
                HSSFCell cell;
                short height = (rowHeight > 0) ? rowHeight : mDefaultHeight;
                int length = selectColNames.Length;

                foreach (DataRow eachRow in data.Rows)
                {
                    row = CreateRows(r);
                    row.HeightInPoints = height;

                    for (int i = 0; i <= length - 1; i++)
                    {
                        cell = row.CreateCell(c + i);
                        cell.SetCellValue(eachRow[selectColNames[i]].ToString());
                        cell.CellStyle = mCellStyleContent;
                    }
                }
            }

            public MemoryStream ExportExcel()
            {
                //write to  client

                MemoryStream stream = new MemoryStream();
                mHSSFWorkbook.Write(stream);
                stream.Flush();
                stream.Position = 0;

                return stream;
            }

            private const short mDefaultHeight = 30;

            //记录已经建立行的最大的行号
            //0 行留作 标题用
            private int mRowIndex; 

            public int RowIndex
            {
                get { return mRowIndex; }
            }

            private int mColIndex;

            public int ColIndex
            {
                get { return mColIndex; }
            }

            private int mMaxColIndex;

            public int MaxColIndex
            {
                get { return mMaxColIndex; }
            }
            //工作簿
            private HSSFWorkbook mHSSFWorkbook;

            public HSSFWorkbook HSSFWorkbook
            {
                get { return mHSSFWorkbook; }
            }

            private HSSFSheet mHSSFSheet;

            public HSSFSheet MHSSFSheet
            {
                get { return mHSSFSheet; }
            }

            //样式
            private HSSFCellStyle mCellStyleTitle;

            public HSSFCellStyle CellStyleTitle
            {
                get{ return mCellStyleTitle; }
                set { mCellStyleTitle = value; }
            }

            private HSSFCellStyle mCellStyleColTitle;

            public HSSFCellStyle CellStyleColTitle
            {
                get { return mCellStyleColTitle; }
                set { mCellStyleColTitle = value; }
            }

            private HSSFCellStyle mCellStyleContent;

            public HSSFCellStyle CellStyleContent
            {
                get { return mCellStyleContent; }
                set { mCellStyleContent = value; }
            }

            //工作薄属性
            private string mSheetName;

            public void SetColumnWidth(short width)
            {
                for (int i = 0; i <= mMaxColIndex - 1; i++)
                {
                    mHSSFSheet.SetColumnWidth(i, width * 256);
                }
            }

            public void SetTitle(short rowHeight)
            {
                HSSFRow row = mHSSFSheet.GetRow(0);
                row.HeightInPoints = (rowHeight > 0) ? rowHeight : mDefaultHeight;

                row.GetCell(0).SetCellValue(mSheetName);

                for (int i = 1; i <= mMaxColIndex - 1; i++)
                {
                    row.CreateCell(i).CellStyle = mCellStyleTitle;
                }

                AddMergedRegion(0, 0, 0, mMaxColIndex - 1);
            }

            public void AddMergedRegion(int firstRow, int lastRow, int firstCol, int lastCol)
            {
                mHSSFSheet.AddMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
            }

        }
       
下面是使用的实例:

        ///


        /// study progress report 学习进度报表
        ///

        public static class WorkBookTemplates
        {
            ///
            /// course gather report
            ///

            ///
            public static MemoryStream WorkBookTemplateGatherInfo(DataTable data)
            {
                //create helper
                WorkBookHelper workBookHelper = new WorkBookHelper("学习进度汇总报表");
               
                //create header
                string[] headers = { "学习岗位", "总登录次数", "总学习时间", "首次登录时间", "最近一次登录时间", "有效期(天)", "学习截至日期", "岗位课程总数", "已学习课程数", "学习率", "考试通过课程数", "通过率" };
                workBookHelper.CreateHeaders(0, 0, headers, 25);

                //create data
                workBookHelper.CreateTableByData(0, 0, data, 30);

                //define pattern
                workBookHelper.SetColumnWidth(30);
                workBookHelper.SetTitle(30);

                //return result by stream
                return workBookHelper.ExportExcel();
            }

            ///


            /// course detail  report  学习课程明细报表
            ///

            /// data sourse
            public static MemoryStream WorkBookTemplateDetailInfo(DataTable data)
            {
                //create helper
                WorkBookHelper workBookHelper = new WorkBookHelper("课程明细报表");

                //create header
                string[] headers = { "序号", "课程名称", "学习情况", "所得考分", "课程类型", "考试次数", "通过时间" };
                workBookHelper.CreateHeaders(0, 0, headers, 30);

                string[] colNames = { "RowNumber", "Name", "CourseStudyStatus", "userscore", "Coursekind", "TestCounts", "FinalPassTime" };
                //create data
                workBookHelper.CreateTableByData(0, 0, data, colNames, 30);

                //define pattern
                workBookHelper.SetColumnWidth(30);
                workBookHelper.SetTitle(40);

                //return result by stream
                return workBookHelper.ExportExcel();
            }

        }
    #endregion

}

 

这样不管是一些格式复杂的汇总报表还是简单的明细报表,都可以导出了。

公司使用的是MVC架构,所以导出报表的Action返回值是FileContentResult类型,如果导出的文件要求中文名,则使用Url.Encode转换下。

如:

         public FileContentResult OutputDetailInfoOfCourseData()
        {

//取数据代码.....
DataTable dt = .....;


            MemoryStream stream = WorkBookTemplates.WorkBookTemplateDetailInfo(dt );

            return File(stream.ToArray(), "application/vnd.ms-excel", Url.Encode("课程明细报表.xls"));
        }

 欢迎大家讨论,还请大家多多指教。

你可能感兴趣的:(net,NPOI)