第一部分:写入xlsx中
使用POI创建一个xlsx文件:
项目结构如下:
具体使用的POI中的
XSSFWorkbook xlsx对象
Sheet 工作簿对象
Row 行对象
Cell 单元格对象
FileOutputStream 流对象
代码如下:
1 package com.it.poiTest; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.util.ArrayList; 7 import java.util.HashMap; 8 import java.util.LinkedHashMap; 9 import java.util.List; 10 import java.util.Map; 11 12 import org.apache.poi.ss.usermodel.Cell; 13 import org.apache.poi.ss.usermodel.Row; 14 import org.apache.poi.ss.usermodel.Sheet; 15 import org.apache.poi.xssf.usermodel.XSSFRow; 16 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 17 18 public class CreateWorkBook { 19 @SuppressWarnings("unchecked") 20 public static void main(String[] args) { 21 //1.xlsx文件对象---可以表示xls和xlsx格式文件的类 22 XSSFWorkbook workbook = new XSSFWorkbook(); 23 try { 24 //2.工作簿对象---新创建的xls需要新创建新的工作簿,offine默认创建的时候会默认生成三个sheet 25 Sheet sheet = workbook.createSheet("first sheet"); 26 /** 27 * 3.这一截,封装数据 28 */ 29 List<Object[]> list = new ArrayList<Object[]>(); 30 list.add(new Object[]{"empId","empName","empAge"}); 31 list.add(new Object[]{"01","小明","18"}); 32 list.add(new Object[]{"02","小犟","12"}); 33 list.add(new Object[]{"03","小够","19"}); 34 list.add(new Object[]{"04","小离","20"}); 35 list.add(new Object[]{"05","小张","30"}); 36 37 //4.行对象---声明行 38 Row row; 39 for(int i=0; i<list.size();i++){ 40 row = sheet.createRow(i); 41 //5.列对象【单元格对象】---声明列 42 Object[] obj = list.get(i); 43 Cell cell = null; 44 for (int j = 0; j < obj.length; j++) { 45 cell = row.createCell(j); 46 cell.setCellValue(obj[j].toString()); 47 } 48 } 49 50 //6.未指定具体路径,则自动创建文件在项目下;若指定具体磁盘路径,也可以 51 FileOutputStream out = new FileOutputStream("createWorkBook.xlsx"); 52 //7.将此xlsx文件以流的形式写出 53 workbook.write(out); 54 //8.关闭流 55 out.close(); 56 System.out.println("createWorkBook success"); 57 } catch (FileNotFoundException e) { 58 e.printStackTrace(); 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } 62 63 64 } 65 }
第二部分:从xlsx文件中读取到项目中
代码如下:
1 package com.it.poiTest; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.Iterator; 9 import java.util.List; 10 11 import org.apache.poi.ss.usermodel.Cell; 12 import org.apache.poi.ss.usermodel.Row; 13 import org.apache.poi.ss.usermodel.Sheet; 14 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 15 16 public class GetWorkBook { 17 18 public static <E> void main(String[] args) { 19 //1。前三步获取xlsx文件对象 20 File file = new File("createWorkBook.xlsx"); 21 try { 22 //2. 23 FileInputStream in = new FileInputStream(file); 24 //3。 25 XSSFWorkbook workbook = new XSSFWorkbook(in); 26 //4.获取工作簿对象 27 Sheet sheet = workbook.getSheetAt(0); 28 //Sheet sheet1 = workbook.getSheet("first sheet"); 效果一样 29 //5.迭代取出数据 30 Iterator<Row> rowItera = sheet.iterator(); 31 List<Object[]> list =(List<Object[]>) new ArrayList<E>(); 32 Row row; 33 while(rowItera.hasNext()){ 34 row = rowItera.next(); 35 Iterator<Cell> cellItera = row.cellIterator(); 36 Cell cell; 37 while(cellItera.hasNext()){ 38 cell = cellItera.next(); 39 //6.打印 40 System.out.print(cell.getStringCellValue() + " "); 41 } 42 System.out.println(" "); 43 } 44 45 } catch (FileNotFoundException e) { 46 e.printStackTrace(); 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 } 51 52 }