JavaNote 4.8 使用POI创建Excel
一、Code
1、 package com.test; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.*; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Readxlsx { public static HSSFWorkbook workbook; public static FileOutputStream fileOutputStream; public static int RowNum = 10; public static int CellNum = 4; public static String filepath = "src/main/resources/Test.xls"; public static void main(String args[]){ Readxlsx readxlsx = new Readxlsx(); readxlsx.createXlsx(filepath); } public String createXlsx(String filepath){ File file = new File(filepath); if(!file.exists()){ try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } try { fileOutputStream = new FileOutputStream(file); workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("Test"); //冻结首行 sheet.createFreezePane(0,1,0,1); /* * 冻结首列 sheet.createFreezePane(1,0,1,1); * createFreezePane的第一个参数表示列,从1开始,0表示不冻结列 * 第二个参数表示行 * 第三个参数表示未被冻结的可见列,从1开始,如果第一个参数为0,此参数设置无效,如第一个参数为1,则冻结1列,第三个参数为2的话,第二列会不可见,需要拖动滚动条才可见 * 第四个参数表示行,使用与第三个参数一样 * */ for(int i = 0;i<CellNum; i++){ //设置单元格宽度,第一个参数表示列,第二个参数表示宽度 sheet.setColumnWidth(i, 5000); } //创建workbook的样式 HSSFCellStyle style = workbook.createCellStyle(); //水平居中 style.setAlignment(HorizontalAlignment.CENTER); //垂直居中 style.setVerticalAlignment(VerticalAlignment.CENTER); //设置边框颜色 style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); //设置边框样式 style.setBorderBottom(BorderStyle.MEDIUM); style.setBorderLeft(BorderStyle.MEDIUM); style.setBorderRight(BorderStyle.MEDIUM); style.setBorderTop(BorderStyle.MEDIUM); //自动换行 style.setWrapText(true); //创建字体 Font font = workbook.createFont(); //加粗 font.setBold(true); //字体名称 font.setFontName("宋体"); //字体颜色 font.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); //字体大小 font.setFontHeightInPoints((short) 14); //样式中应用字体 style.setFont(font); for(int i = 0;i < RowNum;i++){ //创建列 Row row = sheet.createRow(i); //设置行高 row.setHeight((short)500); for(int j = 0; j<CellNum ;j++){ //创建单元格 Cell cell = row.createCell(j); //设置单元格格式 // cell.setCellType(CellType.STRING); //应用单元格样式 cell.setCellStyle(style); if(i==0){ //将自定义Enum的值写入标题行row等于0 switch (j){ case 0: cell.setCellValue(TestEnum.User.toString(j)); break; case 1: cell.setCellValue(TestEnum.User.toString(j)); break; case 2: cell.setCellValue(TestEnum.User.toString(j)); break; case 3: cell.setCellValue(TestEnum.User.toString(j)); break; } } } } workbook.write(fileOutputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e){ e.getStackTrace(); } finally { try { fileOutputStream.close(); System.out.println("Excel生成成功"); } catch (IOException e) { e.printStackTrace(); } } return file.getName(); } }
2、 package com.test; public enum TestEnum { User("用户", 0), Id("账号", 1), Age("年龄", 2), Sex("性别", 3); private String name; private int index; TestEnum(String name, int index) { this.name = name; this.index = index; } public String toString(int index){ for(TestEnum testEnum:TestEnum.values()){ if(testEnum.index == index){ return testEnum.name; } } return null; } }
二、效果展示