|
@@ -0,0 +1,189 @@
|
|
|
+package com.caimei.utils;
|
|
|
+
|
|
|
+
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFFont;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.ss.usermodel.BorderStyle;
|
|
|
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
+import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
+import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFFont;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Aslee
|
|
|
+ * @date 2022/7/14
|
|
|
+ */
|
|
|
+public class PoiUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * XLS列头单元格样式
|
|
|
+ */
|
|
|
+ private HSSFCellStyle getHSSFTopCellStyle(HSSFWorkbook workbook) {
|
|
|
+
|
|
|
+ // 设置字体
|
|
|
+ HSSFFont font = workbook.createFont();
|
|
|
+ // 设置字体大小
|
|
|
+ font.setFontHeightInPoints((short) 11);
|
|
|
+ // 字体加粗
|
|
|
+ font.setBold(true);
|
|
|
+ // 设置字体名字
|
|
|
+ font.setFontName("Courier New");
|
|
|
+ // 设置样式;
|
|
|
+ HSSFCellStyle style = workbook.createCellStyle();
|
|
|
+ // 设置底边框;
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
+ // 设置底边框颜色;
|
|
|
+ style.setBottomBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置左边框;
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
+ // 设置左边框颜色;
|
|
|
+ style.setLeftBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置右边框;
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
+ // 设置右边框颜色;
|
|
|
+ style.setRightBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置顶边框;
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
+ // 设置顶边框颜色;
|
|
|
+ style.setTopBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 在样式用应用设置的字体;
|
|
|
+ style.setFont(font);
|
|
|
+ // 设置自动换行;
|
|
|
+ style.setWrapText(false);
|
|
|
+ // 设置水平对齐的样式为居中对齐;
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ // 设置垂直对齐的样式为居中对齐;
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+
|
|
|
+ return style;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * XLSX列头单元格样式
|
|
|
+ */
|
|
|
+ private XSSFCellStyle getXSSFTopCellStyle(XSSFWorkbook workbook) {
|
|
|
+
|
|
|
+ // 设置字体
|
|
|
+ XSSFFont font = workbook.createFont();
|
|
|
+ // 设置字体大小
|
|
|
+ font.setFontHeightInPoints((short) 11);
|
|
|
+ // 字体加粗
|
|
|
+ font.setBold(true);
|
|
|
+ // 设置字体名字
|
|
|
+ font.setFontName("Courier New");
|
|
|
+ // 设置样式;
|
|
|
+ XSSFCellStyle style = workbook.createCellStyle();
|
|
|
+ // 设置底边框;
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
+ // 设置底边框颜色;
|
|
|
+ style.setBottomBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置左边框;
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
+ // 设置左边框颜色;
|
|
|
+ style.setLeftBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置右边框;
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
+ // 设置右边框颜色;
|
|
|
+ style.setRightBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置顶边框;
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
+ // 设置顶边框颜色;
|
|
|
+ style.setTopBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 在样式用应用设置的字体;
|
|
|
+ style.setFont(font);
|
|
|
+ // 设置自动换行;
|
|
|
+ style.setWrapText(false);
|
|
|
+ // 设置水平对齐的样式为居中对齐;
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ // 设置垂直对齐的样式为居中对齐;
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+
|
|
|
+ return style;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * XLS列数据信息单元格样式
|
|
|
+ */
|
|
|
+ private HSSFCellStyle getHSSFCustomCellStyle(HSSFWorkbook workbook) {
|
|
|
+ // 设置字体
|
|
|
+ HSSFFont font = workbook.createFont();
|
|
|
+ // 设置字体大小
|
|
|
+ // font.setFontHeightInPoints((short)10);
|
|
|
+ // 字体加粗
|
|
|
+ // font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
+ // 设置字体名字
|
|
|
+ font.setFontName("Courier New");
|
|
|
+ // 设置样式;
|
|
|
+ HSSFCellStyle style = workbook.createCellStyle();
|
|
|
+ // 设置底边框;
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
+ // 设置底边框颜色;
|
|
|
+ style.setBottomBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置左边框;
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
+ // 设置左边框颜色;
|
|
|
+ style.setLeftBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置右边框;
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
+ // 设置右边框颜色;
|
|
|
+ style.setRightBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置顶边框;
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
+ // 设置顶边框颜色;
|
|
|
+ style.setTopBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 在样式用应用设置的字体;
|
|
|
+ style.setFont(font);
|
|
|
+ // 设置自动换行;
|
|
|
+ style.setWrapText(false);
|
|
|
+ // 设置水平对齐的样式为居中对齐;
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ // 设置垂直对齐的样式为居中对齐;
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ return style;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * XLSX列数据信息单元格样式
|
|
|
+ */
|
|
|
+ private XSSFCellStyle getXSSFCustomCellStyle(XSSFWorkbook workbook) {
|
|
|
+ // 设置字体
|
|
|
+ XSSFFont font = workbook.createFont();
|
|
|
+ // 设置字体大小
|
|
|
+ // font.setFontHeightInPoints((short)10);
|
|
|
+ // 字体加粗
|
|
|
+ // font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
+ // 设置字体名字
|
|
|
+ font.setFontName("Courier New");
|
|
|
+ // 设置样式;
|
|
|
+ XSSFCellStyle style = workbook.createCellStyle();
|
|
|
+ // 设置底边框;
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
+ // 设置底边框颜色;
|
|
|
+ style.setBottomBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置左边框;
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
+ // 设置左边框颜色;
|
|
|
+ style.setLeftBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置右边框;
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
+ // 设置右边框颜色;
|
|
|
+ style.setRightBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 设置顶边框;
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
+ // 设置顶边框颜色;
|
|
|
+ style.setTopBorderColor(IndexedColors.BLACK.index);
|
|
|
+ // 在样式用应用设置的字体;
|
|
|
+ style.setFont(font);
|
|
|
+ // 设置自动换行;
|
|
|
+ style.setWrapText(false);
|
|
|
+ // 设置水平对齐的样式为居中对齐;
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ // 设置垂直对齐的样式为居中对齐;
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ return style;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|