再springboot中需要使用java代码操作excel表格进行读写,可以使用Apache POI。
只需要导入maven坐标
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
接下来只需写java
进行写操作时,先创建一个XSSFWorkbook对象,通过其创建工作表,在填写数据
进行读操作时,创建文件输入流,添加并把数据打印出来
public static void write() throws IOException {
XSSFWorkbook excel = new XSSFWorkbook();
XSSFSheet sheet = excel.createSheet("info");
sheet.createRow(0).createCell(0).setCellValue("hello");
FileOutputStream out = new FileOutputStream(new File("D:\\test.xlsx"));
excel.write(out);
out.close();
excel.close();
}
public static void read() throws IOException {
FileInputStream fileInputStream = new FileInputStream(new File("D:\\test.xlsx"));
XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = excel.getSheetAt(0);
//获取最后行号
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i <= lastRowNum; i++) {
XSSFRow row = sheet.getRow(i);
if (row != null) {
//获取最后列号
int lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
System.out.print(row.getCell(j)+"\t");
}
System.out.println();
}
}
excel.close();
fileInputStream.close();
}
完成读写操作