NPOI:操作总结

1.套路

  使用了NPOI一段时间,也慢慢了解了操作的流程,或者说套路:

  a.创建Workbook:

HSSFWorkbook Workbook = new HSSFWorkbook();

  b.在Workbook中创建一个工作表并获得该工作表的索引(暂且这么说吧):

HSSFSheet sheet = (HSSFSheet)Workbook.CreateSheet("sheet1");// 前面必须要加强制转换

  c.在工作表中创建行然后获得该行的索引:

HSSFRow row = (HSSFRow)sheet.CreateRow(0); // 0是指第一行,可以直接创建其他行,如创建第4行而不用先创建前面的

  d.在行中创建一个单元格并获得该单元格的索引:

HSSFCell cell = new (HSSFCell)row.CreateCell(0); // 该处的0和上面所说一样

  e.然后就可以愉快地对单元格进行操作了,看起来是一种递进方式,这样操作起来也方便。

2.封装的写单元格 WriteCell

  为了自己使用方便,我把设置单元格的值的功能封装了一下,可以实现随意按照行列位置写,单元格或者行的创建都封装了,只需要引用工作表:

private static void WriteCell(int row, int col, string val, ref HSSFSheet sheet)
        {
            if (sheet.GetRow(row - 1) == null) // 判断行是否被创建
            {
                HSSFRow t_row = (HSSFRow)sheet.CreateRow(row - 1);

                HSSFCell t_cell = (HSSFCell)t_row.CreateCell(col - 1);

                t_cell.SetCellValue(val);
            }
            else
            {
                HSSFRow t_row = (HSSFRow)sheet.GetRow(row - 1);

                if (t_row.GetCell(col - 1) == null) // 判断列(单元格)是否被创建
                {
                    HSSFCell t_cell = (HSSFCell)t_row.CreateCell(col - 1);

                    t_cell.SetCellValue(val);
                }
                else
                {
                    HSSFCell t_cell = (HSSFCell)t_row.GetCell(col - 1);

                    t_cell.SetCellValue(val);
                }
            }
        }

3.封装的复制单元格 CopyCell

  将一个工作表中的某个单元格的内容复制到另一个工作表中的某个单元格:

 private static bool CopyCell(int dst_row, int dst_col, int src_row, int src_col, ref HSSFSheet dst, ref HSSFSheet src)
        {
            if (src.GetRow(src_row - 1) == null)
            {
                return false; // 没有该行
            }
            else
            {
                HSSFRow t_src_row = (HSSFRow)src.GetRow(src_row - 1);
                if (t_src_row.GetCell(src_col - 1) == null)
                {
                    return false;
                }
                else
                {
                    HSSFCell t_src_cell = (HSSFCell)t_src_row.GetCell(src_col - 1);

                    WriteCell(dst_row, dst_col, t_src_cell.ToString(), ref dst);
                }
            }


            return true;
        }

4.设置行高

        private static void SetRowHeight(int row, short height, ref HSSFSheet sheet)
        {
            if (sheet.GetRow(row - 1) == null)
            {
                HSSFRow t_row = (HSSFRow)sheet.CreateRow(row - 1);

                t_row.Height = height;
            }
            else
            {
                HSSFRow t_row = (HSSFRow)sheet.GetRow(row - 1);

                t_row.Height = height;
            }
        }

5.设置列宽

sheet.SetColumnWidth(0, 20*256); // 第一行,并且列宽是256的倍数

6.设置指定单元格是否水平或垂直居中

private static void SetCellAlignmentCenter(int row,int col,bool vertical,bool horizontal,ref HSSFWorkbook wb)
        {
            HSSFCellStyle cellStyle = (HSSFCellStyle)wb.CreateCellStyle(); // 首先在工作簿中创建一个样式并获得索引,可以多次创建

            HSSFSheet sheet = (HSSFSheet)wb.GetSheetAt(0);

            if(vertical) // 垂直
            {
                cellStyle.VerticalAlignment = VerticalAlignment.Center;
            }
            else
            {
                cellStyle.VerticalAlignment = VerticalAlignment.Bottom; // 默认在底部
            }
            if(horizontal) // 水平
            {
                cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            }
            else
            {
                cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; // 默认左对齐
            }
            if (sheet.GetRow(row - 1) == null)
            {
                HSSFRow t_row = (HSSFRow)sheet.CreateRow(row - 1);

                HSSFCell t_cell = (HSSFCell)t_row.CreateCell(col - 1);

                t_cell.CellStyle = cellStyle;
            }
            else
            {
                HSSFRow t_row = (HSSFRow)sheet.GetRow(row - 1);

                if (t_row.GetCell(col - 1) == null)
                {
                    HSSFCell t_cell = (HSSFCell)t_row.CreateCell(col - 1);

                    t_cell.CellStyle = cellStyle;
                }
                else
                {
                    HSSFCell t_cell = (HSSFCell)t_row.GetCell(col - 1);

                    t_cell.CellStyle = cellStyle;
                }
            }


        }

7.设置单元格的字体颜色和大小

 enum CellFontColor
        {
            white,
            black,
            yellow,
            blue,
            green,
            red,
        }
 enum CellFontSize
        {
            s10,
            s11,
            s12,
            s14,
            s16,
            s18,
            s20,
            s24
        }
        private static void SetCellFont(int row,int col,CellFontColor color,CellFontSize size,ref HSSFWorkbook wb)
        {
            HSSFSheet sheet = (HSSFSheet)wb.GetSheetAt(0);
            ICellStyle style = wb.CreateCellStyle();
            IFont font = wb.CreateFont();
            switch(color)
            {
                case CellFontColor.black: font.Color = NPOI.HSSF.Util.HSSFColor.Black.Index;
                    break;
                case CellFontColor.blue: font.Color = NPOI.HSSF.Util.HSSFColor.Blue.Index;
                    break;
                case CellFontColor.green: font.Color = NPOI.HSSF.Util.HSSFColor.Green.Index;
                    break;
                case CellFontColor.red: font.Color = NPOI.HSSF.Util.HSSFColor.Red.Index;
                    break;
                case CellFontColor.white: font.Color = NPOI.HSSF.Util.HSSFColor.White.Index;
                    break;
                case CellFontColor.yellow: font.Color = NPOI.HSSF.Util.HSSFColor.Yellow.Index;
                    break;
            }
            switch(size)
            {
                case CellFontSize.s10: font.FontHeightInPoints = 10;
                    break;
                case CellFontSize.s11: font.FontHeightInPoints = 11;
                    break;
                case CellFontSize.s12: font.FontHeightInPoints = 12;
                    break;
                case CellFontSize.s14: font.FontHeightInPoints = 14;
                    break;
                case CellFontSize.s16: font.FontHeightInPoints = 16;
                    break;
                case CellFontSize.s18: font.FontHeightInPoints = 18;
                    break;
                case CellFontSize.s20: font.FontHeightInPoints = 20;
                    break;
                case CellFontSize.s24: font.FontHeightInPoints = 24;
                    break;
            }
            style.SetFont(font);

            if (sheet.GetRow(row - 1) == null)
            {
                HSSFRow t_row = (HSSFRow)sheet.CreateRow(row - 1);

                HSSFCell t_cell = (HSSFCell)t_row.CreateCell(col - 1);

                t_cell.CellStyle = style;
            }
            else
            {
                HSSFRow t_row = (HSSFRow)sheet.GetRow(row - 1);

                if (t_row.GetCell(col - 1) == null)
                {
                    HSSFCell t_cell = (HSSFCell)t_row.CreateCell(col - 1);

                    t_cell.CellStyle = style;
                }
                else
                {
                    HSSFCell t_cell = (HSSFCell)t_row.GetCell(col - 1);

                    t_cell.CellStyle = style;
                }
            }
            
        }

8.暂时总结到此,以上都是把一些操作封装了,封装的功能不是很完善,主要都是我常用的。

GitHub:https://github.com/whlook/MyExcelUtil 

 

posted @ 2017-05-18 21:17  whlook  阅读(1377)  评论(0编辑  收藏  举报