3.poi入门操作
3.1 搭建环境
1 <dependency> 2 <groupId>org.apache.poi</groupId> 3 <artifactId>poi</artifactId> 4 <version>4.1.0</version> 5 </dependency> 6 <!-- poi2007相应依赖坐标 --> 7 <dependency> 8 <groupId>org.apache.poi</groupId> 9 <artifactId>poi-ooxml</artifactId> 10 <version>4.1.0</version> 11 </dependency> 12 <dependency> 13 <groupId>org.apache.poi</groupId> 14 <artifactId>poi-ooxml-schemas</artifactId> 15 <version>4.1.0</version> 16 </dependency>
3.4基本操作
public class App { public static void main(String[] args) { //1.创建工作簿 Workbook wb = new XSSFWorkbook();//2007版 //2.创建sheet Sheet sheet = wb.createSheet("test"); //3文件流 try (FileOutputStream fos = new FileOutputStream("E:\poi\test.xlsx")) { wb.write(fos); } catch (IOException e) { e.printStackTrace(); } } }
Demo2 设置单元格样式
1 /** 2 * Demo2设置单元格样式 3 */ 4 public class Demo2 { 5 public static void main(String[] args) { 6 //1.创建工作簿 7 Workbook wb = new XSSFWorkbook();//2007版 8 //2.创建sheet 9 Sheet sheet = wb.createSheet("test"); 10 //创建行对象,参数:索引从0开始 11 Row row = sheet.createRow(2); 12 //创建单元格对象,索引从0开始 13 Cell cell = row.createCell(2); 14 cell.setCellValue("才高八斗"); 15 16 //样式处理 17 //创建样式对象 18 CellStyle style = wb.createCellStyle(); 19 style.setBorderTop(BorderStyle.THIN);//上边框 20 style.setBorderRight(BorderStyle.THIN);//右边框 21 style.setBorderBottom(BorderStyle.THIN);//下边框 22 style.setBorderLeft(BorderStyle.THIN);//左边框 23 24 //创建字体对象 25 Font font = wb.createFont(); 26 font.setFontName("华文行楷"); 27 font.setFontHeightInPoints((short) 28);//设置字号 28 style.setFont(font); 29 30 //行高和列宽 31 row.setHeightInPoints(50);//设置行高 32 sheet.setColumnWidth(2, 31 * 256);//第三列,设置列宽需要乘以256 33 34 //设置水平居中 35 style.setAlignment(HorizontalAlignment.CENTER); 36 //设置垂直居中 37 style.setVerticalAlignment(VerticalAlignment.CENTER); 38 39 //向单元格设置样式 40 cell.setCellStyle(style); 41 42 //3文件流 43 try (FileOutputStream fos = new FileOutputStream("E:\poi\test3.xlsx")) { 44 wb.write(fos); 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 49 } 50 }
Demo3 在Excel中添加图片
1 /** 2 * Demo3在Excel中添加图片 3 */ 4 public class Demo1 { 5 public static void main(String[] args) throws IOException { 6 //1.创建工作簿 7 Workbook wb = new XSSFWorkbook();//2007版 8 //2.创建sheet 9 Sheet sheet = wb.createSheet("test"); 10 11 //------------------------------------------------------------ 12 //1.读取图片流 13 FileInputStream fis = new FileInputStream("E:\poi\merry.jpg"); 14 15 //2.转化二进制数组 16 byte[] bytes = IOUtils.toByteArray(fis); 17 fis.read(bytes); 18 //3.向poi内存中添加一张图片,返回图片在图片集合中的索引 19 int index = ((XSSFWorkbook) wb).addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); 20 //4.绘制图片工具类 21 CreationHelper helper = wb.getCreationHelper(); 22 //5.创建一个绘图对象 23 Drawing<?> drawing = sheet.createDrawingPatriarch(); 24 //6.创建锚点,设置图片坐标 25 ClientAnchor anchor = helper.createClientAnchor(); 26 //设置图片起始位置 27 anchor.setRow1(1); 28 anchor.setCol1(1); 29 ////设置图片结束位置 30 anchor.setRow2(15); 31 anchor.setCol2(15); 32 //7.绘制图片 33 Picture picture = drawing.createPicture(anchor, index);//图片位置,图片的索引 34 // picture.resize();//自适应渲染图片 35 //------------------------------------------------------------ 36 //3文件流 37 try (FileOutputStream fos = new FileOutputStream("E:\poi\test4.xlsx")) { 38 wb.write(fos); 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } 42 43 } 44 }
加载Excel
/** * Demo4读取Excel并解析 * sheet.getLastRowNum():获取最后一行的索引值 * row.getLastCellNum():获取最后一个单元格的号码 */ public class Demo4 { public static void main(String[] args) throws IOException { //1.根据Excel文件创建工作簿 Workbook wb = new XSSFWorkbook("E:\poi\demo2.xlsx"); //2.获取Sheet Sheet sheet = wb.getSheetAt(0); //3.获取sheet中的每一行和每一个单元格 for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) { Row row = sheet.getRow(rowNum);//根据索引获取每一行 if (row == null){ continue; } StringBuilder sb = new StringBuilder(); for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) { //根据索引获取每一个单元格 Cell cell = row.getCell(cellNum); if(cell == null){ continue; } //获取每一个单元格的内容 Object value = getCellValue(cell); sb.append(value).append("-"); } System.out.println(sb.toString()); } } private static Object getCellValue(Cell cell) { //1.获取单元格的属性类型 CellType cellType = cell.getCellType(); //2.根据单元格的类型获取时间 Object value = null; switch (cellType) { case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { //日期格式 value = cell.getDateCellValue(); } else { value = cell.getNumericCellValue(); } break; case STRING: value = cell.getStringCellValue(); break; case BOOLEAN: value = cell.getBooleanCellValue(); break; case FORMULA://公式类型 value = cell.getCellFormula(); break; default: break; } return value; } }