C#NPOI示例

主程序要添加NPOI的库。代码如下

        static void Main(string[] args)
        {
            Export();  
        }
        public static void Export()
        {
            // 创建新的Excel工作簿
            IWorkbook workbook = new XSSFWorkbook();

            // 创建一个工作表
            ISheet sheet = workbook.CreateSheet("Sheet1");

            // 创建合并单元格的样式
            ICellStyle cellStyle = workbook.CreateCellStyle();
            cellStyle.Alignment = HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = VerticalAlignment.Center;

            //设置字体
            IFont font = workbook.CreateFont();
            font.FontHeightInPoints = 12;
            font.FontHeight = 4 * 60;
            font.FontName = "宋体";
            font.IsBold = true;
            cellStyle.SetFont(font);

            // 设置边框样式为实线
            cellStyle.BorderTop = BorderStyle.Thin;
            cellStyle.BorderBottom = BorderStyle.Thin;
            cellStyle.BorderLeft = BorderStyle.Thin;
            cellStyle.BorderRight = BorderStyle.Thin;

            // 创建合并单元格
            int row = 0; // 指定的行号
            int cell = 0;

            // 在合并后的单元格中设置值和样式
            IRow row1 = sheet.CreateRow(row);
            IRow row2 = sheet.CreateRow(row + 1);
            row1.Height = 20 * 20;
            row2.Height = 20 * 20;

            // 创建合并单元格
            CellRangeAddress cellRangeAddress = new CellRangeAddress(row, row + 1, cell, cell);
            sheet.AddMergedRegion(cellRangeAddress);
            ICell cell0 = row1.CreateCell(row);
            cell0.SetCellValue("机器编号");
            cell0.CellStyle = cellStyle;

            //自定义行宽
            sheet.SetColumnWidth(row, 20 * 256);

            cellRangeAddress = new CellRangeAddress(row, row + 1, cell + 1, cell + 1);
            sheet.AddMergedRegion(cellRangeAddress);
            ICell cell1 = row1.CreateCell(cell + 1);
            cell1.SetCellValue("位置");
            cell1.CellStyle = cellStyle;

            //自定义行宽
            sheet.SetColumnWidth(row + 1, 30 * 256);

            cellRangeAddress = new CellRangeAddress(row, row, cell + 2, cell + 3);
            sheet.AddMergedRegion(cellRangeAddress);
            ICell cell2 = row1.CreateCell(cell + 2);
            cell2.SetCellValue("自费结算");

            cellRangeAddress = new CellRangeAddress(row, row, cell + 4, cell + 9);
            sheet.AddMergedRegion(cellRangeAddress);
            ICell cell3 = row1.CreateCell(cell + 4);
            cell3.SetCellValue("医保结算");
            cell3.CellStyle = cellStyle;

            #region 如果创建这个列,会导致实线边框样式无法呈现
            row1.CreateCell(cell + 9).SetCellValue("");
            //被占跨行合并的第二行第一第二列
            row2.CreateCell(cell + 0).SetCellValue("");
            row2.CreateCell(cell + 1).SetCellValue("");
            //被占跨行合并的第二行第一第二列
            #endregion

            row2.CreateCell(cell + 2).SetCellValue("自费结算笔数");
            row2.CreateCell(cell + 3).SetCellValue("自费结算金额(元)");
            row2.CreateCell(cell + 4).SetCellValue("医保卡笔数");
            row2.CreateCell(cell + 5).SetCellValue("医保卡个账金额(元)");
            row2.CreateCell(cell + 6).SetCellValue("医保电子凭证笔数");
            row2.CreateCell(cell + 7).SetCellValue("医保电子凭证个账金额(元)");
            row2.CreateCell(cell + 8).SetCellValue("人脸医保笔数");
            row2.CreateCell(cell + 9).SetCellValue("人脸医保个账金额(元)");
            cell2.CellStyle = cellStyle;

            var irow = sheet.GetRow(1);
            SetColumnWidth(sheet, row + 2, row + 9, 22 * 256);
            SetCellStyle(sheet, 0, 1, 0, 9, cellStyle);

            // 写入到文件
            using (FileStream file = new FileStream("merged_cells.xlsx", FileMode.Create, FileAccess.Write))
            {
                workbook.Write(file);
            }


        }

        /// <summary>
        /// 设置列宽宽度
        /// </summary>
        public static void SetColumnWidth(ISheet sheet, int fristSolumn, int endSolumn, int intWidth)
        {
            for (int i = fristSolumn; i <= endSolumn; i++)
            {
                sheet.SetColumnWidth(i, intWidth);
            }
        }

        /// <summary>
        /// 给单元格四周加黑色边框
        /// </summary>
        /// <param name="sheet">单元格所在的sheet</param>
        /// <param name="rowstart">开始行的索引</param>
        /// <param name="rowend">结束行的索引</param>
        /// <param name="colstart">开始列的索引</param>
        /// <param name="colend">结束列的索引</param>
        public static void SetCellStyle(ISheet sheet, int rowstart, int rowend, int colstart, int colend, ICellStyle cellStyle)
        {
            for (int i = rowstart; i <= rowend; i++)
            {
                var irow = sheet.GetRow(i);
                if (irow != null && irow.Cells != null && irow.Cells.Count > 0)
                {
                    for (int j = 0; j < irow.Cells.Count; j++)
                    {
                        irow.Cells[j].CellStyle = cellStyle;
                    }
                }
            }
        }

 

posted @ 2025-01-16 14:52  xiaojianjian  阅读(55)  评论(0)    收藏  举报