最近公司有個項目用到了poi,須要按自定義導出模板進行數據導出,藉此瞭解了一下poi的相關操做java
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author liyhu * @date 2019年09月28日 */ public class ExcelUtil { public static Workbook createExcel5(Workbook workbook, String sheetName, List<List<String>> data, List<String> headers) { Sheet sheet = workbook.createSheet(sheetName); sheet.setDefaultRowHeight((short) (2 * 256)); int size = headers.size(); sheet.addMergedRegion(new CellRangeAddress(0,0,0,size-1)); Row firstTitleRow = sheet.createRow(0); Cell firstTitleCell = firstTitleRow.createCell(0); firstTitleCell.setCellValue("員工"); Font font = workbook.createFont(); font.setBold(true); font.setFontName("宋體"); Font redFont = workbook.createFont(); redFont.setBold(true); redFont.setFontName("宋體"); redFont.setColor(Font.COLOR_RED); CellStyle firstCellStyle = workbook.createCellStyle(); firstCellStyle.setAlignment(HorizontalAlignment.CENTER); firstCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); firstCellStyle.setFont(font); firstTitleCell.setCellStyle(firstCellStyle); // --------------- sheet.addMergedRegion(new CellRangeAddress(1,1,0,size-1)); Row secondTitleRow = sheet.createRow(1); Cell secondTitleCell = secondTitleRow.createCell(0); secondTitleCell.setCellValue("導入人:"); CellStyle secondCellStyle = workbook.createCellStyle(); secondCellStyle.setAlignment(HorizontalAlignment.LEFT); secondCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); secondCellStyle.setFont(font); secondTitleCell.setCellStyle(secondCellStyle); // ------ Row titleRow = sheet.createRow(2); DataFormat dataFormat = workbook.createDataFormat();//建立格式化對象 short text = dataFormat.getFormat("TEXT"); CellStyle textStyle = workbook.createCellStyle(); //標題樣式 textStyle.setDataFormat(text); CellStyle titleStyle = workbook.createCellStyle(); //標題樣式 titleStyle.setAlignment(HorizontalAlignment.CENTER); titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); font.setFontHeightInPoints((short) 14); titleStyle.setFont(font); titleStyle.setBorderBottom(BorderStyle.THIN); titleStyle.setBorderRight(BorderStyle.THIN); titleStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); Map<Integer,Integer> colWidthMap=new HashMap<>(); for (int i = 0; i < size; i++) { Cell cell = titleRow.createCell(i); cell.setCellStyle(titleStyle); RichTextString richTextString=new XSSFRichTextString(headers.get(i)); richTextString.applyFont(font); if(richTextString.getString().startsWith("*")){ richTextString.applyFont(0,1,redFont); } cell.setCellValue(richTextString); sheet.setDefaultColumnStyle(i,textStyle); colWidthMap.put(i, 14); } CellStyle dataStyle = workbook.createCellStyle(); //標題樣式 dataStyle.setAlignment(HorizontalAlignment.CENTER); dataStyle.setVerticalAlignment(VerticalAlignment.CENTER); dataStyle.setDataFormat(text); dataStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());;//設置單元格背景色爲騷粉 for (int i = 0; i < data.size(); i++) { List<String> oneRow = data.get(i); Row row = sheet.createRow(i + 3); for (int j = 0; j < oneRow.size(); j++) { Cell cell = row.createCell(j); cell.setCellValue(oneRow.get(j)); cell.setCellStyle(dataStyle); int length = oneRow.get(j).length(); Integer defaultColWidth = colWidthMap.get(j); if(length > defaultColWidth ){ colWidthMap.put(j,length); } } } for (Map.Entry<Integer, Integer> entry : colWidthMap.entrySet()) { sheet.setColumnWidth(entry.getKey(),entry.getValue() * 256); } return workbook; } }
client 端apache
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @author liyhu * @date 2019年09月28日 */ public class PoiClient { public static void main(String[] args) { Workbook workbook=new XSSFWorkbook(); String sheetName="one"; List<List<String>> data=new ArrayList<>(); for (int i = 0; i < 9; i++) { data.add(Arrays.asList("name"+i,"ageemail45454545email45454545email45454545email45454545"+i,"email45454545email45454545email45454545email45454545"+i)); } data.add(Arrays.asList("name","age","email")); List<String> headers=Arrays.asList("部門名稱","*部門名稱","成本中心"); Workbook wb = ExcelUtil.createExcel(workbook, sheetName, data, headers); try (FileOutputStream os = new FileOutputStream("D:\\a.xlsx")){ wb.write(os); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("ok"); } }