Location>code7788 >text

C#NPOI example

Popularity:201 ℃/2025-01-16 14:50:01
public static void Export() { //Create a new Excel workbook IWorkbook workbook = new XSSFWorkbook(); //Create a worksheet ISheet sheet = ("Sheet1"); //Create a style for merged cells ICellStyle cellStyle = (); = ; = ; //Set font IFont font = (); = 12; = 4 * 60; = "Song Dynasty"; = true; (font); //Set the border style to solid line = ; = ; = ; = ; //Create merged cells int row = 0; //specified line number int cell = 0; //Set values ​​and styles in merged cells IRow row1 = (row); IRow row2 = (row + 1); = 20 * 20; = 20 * 20; //Create merged cells CellRangeAddress cellRangeAddress = new CellRangeAddress(row, row + 1, cell, cell); (cellRangeAddress); ICell cell0 = (row); ("Machine number"); = cellStyle; //Custom line width (row, 20 * 256); cellRangeAddress = new CellRangeAddress(row, row + 1, cell + 1, cell + 1); (cellRangeAddress); ICell cell1 = (cell + 1); ("Location"); = cellStyle; //Custom line width (row + 1, 30 * 256); cellRangeAddress = new CellRangeAddress(row, row, cell + 2, cell + 3); (cellRangeAddress); ICell cell2 = (cell + 2); ("Settlement at self-pay"); cellRangeAddress = new CellRangeAddress(row, row, cell + 4, cell + 9); (cellRangeAddress); ICell cell3 = (cell + 4); ("Medical insurance settlement"); = cellStyle; #regionIf you create this column, the solid border style will not be rendered. (cell + 9).SetCellValue(""); //The second row, first and second column of the occupied cross-row merge (cell + 0).SetCellValue(""); (cell + 1).SetCellValue(""); //The second row, first and second column of the occupied cross-row merge #endregion (cell + 2).SetCellValue("Number of self-pay settlements"); (cell + 3).SetCellValue("Self-pay settlement amount (yuan)"); (cell + 4).SetCellValue("Number of medical insurance cards"); (cell + 5).SetCellValue("Medical insurance card account amount (yuan)"); (cell + 6).SetCellValue("Number of medical insurance electronic vouchers"); (cell + 7).SetCellValue("Medical insurance electronic voucher account amount (yuan)"); (cell + 8).SetCellValue("Number of facial medical insurance records"); (cell + 9).SetCellValue("Face medical insurance account amount (yuan)"); = cellStyle; var irow = (1); SetColumnWidth(sheet, row + 2, row + 9, 22 * 256); SetCellStyle(sheet, 0, 1, 0, 9, cellStyle); //write to file using (FileStream file = new FileStream("merged_cells.xlsx", , )) { (file); } } /// <summary> ///Set column width/// </summary> public static void SetColumnWidth(ISheet sheet, int fristSolumn, int endSolumn, int intWidth) { for (int i = fristSolumn; i <= endSolumn; i++) { (i, intWidth); } } /// <summary> ///Add black borders around cells/// </summary> /// <param name="sheet">The sheet where the cell is located</param> /// <param name="rowstart">index of starting row</param> /// <param name="rowend">The index of the ending row</param> /// <param name="colstart">index of starting column</param> /// <param name="colend">end column index</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 = (i); if (irow != null && != null && > 0) { for (int j = 0; j < ; j++) { [j].CellStyle = cellStyle; } } } }