1. 程式人生 > >poi 匯出excel 格式

poi 匯出excel 格式

HSSFSheet sheet = workbook.createSheet("sheetName"); //建立sheet
sheet.setVerticallyCenter(true);

//下面樣式可作為匯出左右分欄的表格模板
sheet.setColumnWidth((short) 0, (short) 2600);// 設定列寬
sheet.setColumnWidth((short) 1, (short) 2400);
sheet.setColumnWidth((short) 2, (short) 2300);
sheet.setColumnWidth((short) 3, (short) 1600);
sheet.setColumnWidth((short) 4, (short) 1800);
sheet.setColumnWidth((short) 5, (short) 1000);// 空列設定小一些
sheet.setColumnWidth((short) 6, (short) 2600);// 設定列寬
sheet.setColumnWidth((short) 7, (short) 2400);
sheet.setColumnWidth((short) 8, (short) 2300);
sheet.setColumnWidth((short) 9, (short) 1600);
sheet.setColumnWidth((short) 10, (short) 1800);

HSSFCellStyle cellstyle = (HSSFCellStyle) workbook.createCellStyle();// 設定表頭樣式
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 設定居中

HSSFCellStyle headerStyle = (HSSFCellStyle) workbook .createCellStyle();// 建立標題樣式
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //設定垂直居中
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設定水平居中
HSSFFont headerFont = (HSSFFont) workbook.createFont(); //建立字型樣式
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字型加粗
headerFont.setFontName("Times New Roman"); //設定字型型別
headerFont.setFontHeightInPoints((short) 8); //設定字型大小
headerStyle.setFont(headerFont); //為標題樣式設定字型樣式

HSSFCellStyle headerStyle1 = (HSSFCellStyle) workbook .createCellStyle();// 建立標題樣式1
headerStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
headerStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont headerFont1 = (HSSFFont) workbook.createFont();
headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字型加粗
headerFont1.setFontName("Times New Roman");
headerFont1.setFontHeightInPoints((short) 8);
headerStyle1.setFont(headerFont1);

HSSFCellStyle headerStyle2 = (HSSFCellStyle) workbook .createCellStyle();// 建立標題樣式2
headerStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
headerStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont headerFont2 = (HSSFFont) workbook.createFont();
headerFont2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字型加粗
headerFont2.setFontName("Times New Roman");
headerFont2.setFontHeightInPoints((short) 8);
headerStyle2.setFont(headerFont2);
headerStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下邊框
headerStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左邊框
headerStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上邊框
headerStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右邊框

HSSFCellStyle cell_Style = (HSSFCellStyle) workbook .createCellStyle();// 設定字型樣式
cell_Style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell_Style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直對齊居中
cell_Style.setWrapText(true); // 設定為自動換行
HSSFFont cell_Font = (HSSFFont) workbook.createFont();
cell_Font.setFontName("宋體");
cell_Font.setFontHeightInPoints((short) 8);
cell_Style.setFont(cell_Font);
cell_Style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下邊框
cell_Style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左邊框
cell_Style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上邊框
cell_Style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右邊框

HSSFRow row = sheet.createRow((short)1); //建立行
HSSFCell cell = row.createCell((short)1); //建立列
cell.setCellStyle(headerStyle2); //單元格引用樣式

 

大佬地址:https://www.cnblogs.com/vipwolf/p/4226880.html