一、什么是POI
POI全称PoorObfuscation Implementation ,是Apache组件的一个开源项目,可以对微软的Office一系列办公软件进行读写操作。
官方下载地址:https://poi.apache.org/download.html,目前最新版本为POI 5.2.1。
本文实例对应的POI版本为3.9
二、 项目pom文件
org.apache.poi
poi
3.9
org.apache.poi
poi-ooxml
3.9
org.apache.commons
commons-collections4
4.2
三、生成Excel 工具类
package org.zhao.component;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.*;
/**
* 生成Excel
*
* @author Administrator
* @date 2022年03月11日
*/
public class WriteExcelComponent {
private static String getFileExtname(String filename) {
return filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
}
@SuppressWarnings("unchecked")
public static void writeExcel(String filePath, List
四、读取Excel 工具类
package org.zhao.component;
import org.apache.commons.collections4.MapUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.*;
/**
* Excel 读取excel中的数据
*
* @author Administrator
* @date 2022年03月11日
*/
public class ReadExcelComponent {
private static String getFileExtname(String filename) {
return filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
}
/**
* 读取某个sheet中的数据
*
* @param path 文件路径
* @param sheetNum sheet号
*/
public static Map readExcelData(String path, int sheetNum) {
List
五、运行实例
package org.zhao;
import org.zhao.component.ReadExcelComponent;
import org.zhao.component.WriteExcelComponent;
import java.util.*;
/**
* @author Administrator
* @date 2022年03月11日
*/
public class Main {
public static void writeDataToExcel() {
List> dataList = new ArrayList<>();
Map sheet_1 = new HashMap<>();
sheet_1.put("sheetName", "语文成绩");
sheet_1.put("sheetHeader", Arrays.asList("学号", "姓名", "分数"));
List> rows = new ArrayList<>();
rows.add(Arrays.asList("0001", "小张", "60"));
rows.add(Arrays.asList("0002", "小明", "80"));
rows.add(Arrays.asList("0003", "小王", "90"));
sheet_1.put("sheetRows", rows);
dataList.add(sheet_1);
Map sheet_2 = new HashMap<>();
sheet_2.put("sheetName", "数学成绩");
sheet_2.put("sheetHeader", Arrays.asList("学号", "姓名", "分数"));
List> rows2 = new ArrayList<>();
rows2.add(Arrays.asList("0001", "小张", "90"));
rows2.add(Arrays.asList("0002", "小明", "86"));
rows2.add(Arrays.asList("0003", "小王", "95"));
sheet_2.put("sheetRows", rows2);
dataList.add(sheet_2);
try {
WriteExcelComponent.writeExcel("D:/成绩单.xls", dataList);
System.out.println("创建成功");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readExcelData() {
List> allItems = ReadExcelComponent.readExcelDatas("D:/成绩单.xls");
System.out.println("allItems = " + allItems);
Map sheet2Items = ReadExcelComponent.readExcelData("D:/成绩单.xls", 2);
System.out.println("sheet2Items = " + sheet2Items);
}
public static void main(String[] args) {
// writeDataToExcel();
readExcelData();
}
}
六、完整实例下载地址:
https://download.csdn.net/download/networkTalent/84981699