自己用的POI读写Excel的工具类(只支持Excel2007(.xlsx)),写的不好,可以做个参考学习。
写Excel的方法public void writeFile(List<Map<String, String>> listMap, String address, String[] column) 参数 List<Map<String, String>> listMap :把要写入的数据存储为list,一个map为一行数据,map的键跟表头保持一致(数据类型最好不用Object); String address :存储Excel的地址(包括文件的名字和后缀名); String[] column :Excel表头。
读Excel的方法public List<Map<String, String>> readFile(String address) String address :读取的地址(包括文件的名字和后缀名)。 map的键是:1、2、3… …
package com.example.txzc.utils; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @Service public class PoiUtils { XSSFWorkbook workbook = null; private List<Map<String, String>> listMap; private Map<String, String> map; FileOutputStream stream = null; /** * 写入Excel * @param listMap 数据 * @param address 生成地址 * @param column 表头 */ public void writeFile(List<Map<String, String>> listMap, String address, String[] column) { if (listMap.size() == 0){ return; } workbook = new XSSFWorkbook(); XSSFSheet sheetName = workbook.createSheet("页名"); XSSFRow refund = sheetName.createRow(0); for (int i = 0; i < column.length; i++) { refund.createCell(i).setCellValue(column[i]); } for (int j = 0; j < listMap.size(); j++) { map = new LinkedHashMap<>(); for (int k = 0; k < column.length; k++) { map.put(column[k], listMap.get(j).get(column[k])); } XSSFRow row = sheetName.createRow(j + 1); for (int m = 0; m < column.length; m++) { row.createCell(m).setCellValue(map.get(column[m])); } } try { //设定 路径 File file = new File(address); // 需要抛异常 stream = new FileOutputStream(file); workbook.write(stream); } catch (IOException e) { e.printStackTrace(); } finally { //关流 try { if (stream != null) { stream.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 读取Excel并返回一个List集合 * @param address * @return */ public List<Map<String, String>> readFile(String address) { listMap = new ArrayList<>(); //创建Excel文件 FileInputStream inputStream = null; Workbook workBook = null; //表的总行数 int lastRowNum; //表的总列数 int lastCellNum; try { inputStream = new FileInputStream(new File(address)); workBook = new XSSFWorkbook(inputStream); Sheet sheet = workBook.getSheetAt(0); lastRowNum = sheet.getLastRowNum(); //获取第一行数据 for (int i = 1; i <= lastRowNum; i++) { map = new LinkedHashMap<>(); //获取i+1行的数据:第二行开始取值 Row row = sheet.getRow(i); Row rowYiHang = sheet.getRow(0); lastCellNum = rowYiHang.getLastCellNum(); for (int j = 0; j < lastCellNum; j++) { String value = row.getCell(j).getStringCellValue(); map.put(j + 1 + "", value); } if (!"".equals(map.get("1")) && map.get("1") != null) { listMap.add(map); } } } catch (IOException e) { e.printStackTrace(); } finally { if (workBook != null) { try { workBook.close(); } catch (IOException e) { e.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return listMap; } //读取文件的地址 public String filePath(String path, String fileName) { return path + "\\" + fileName + ".xlsx";//读取文件位置 } }