引入 pom依赖
<!-- 读取后缀为xlsx或xls的excel操作需要导入包 -->
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!--jxl-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
// 根据抽象路径名创建文件
File file = new File(filePathName);
// 查询此文件的文件夹
File parentFile = file.getParentFile();
// 文件夹不存在就创建文件夹
if (!parentFile.exists()) {
//创建文件夹
parentFile.mkdirs();
}
//创建文件
try {
file.createNewFile();
} catch (IOException e) {
log.error("创建文件信息异常!", e);
return;
}
和下面代码 同样实现功能:
/**
* @Description: 创建 Excel表格
*/
package com.baidu;
import com.constant.ImageParamsConstant;
import com.dao.entity.Batch;
import com.redis.EnvironmentUtils;
import com.service.ShrinkageService;
import com.utils.TimeUtil;
import jxl.Workbook;
import jxl.write.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
@Override
public boolean createExcel(String lotNo, String macNo, Batch batch) {
// 检查 文件路径
if (!Files.isWritable(Paths.get(filePath))) {
log.info("path not exist,create path: {}", filePath);
try {
// 创建目录
Files.createDirectories(Paths.get(filePath));
} catch (IOException e) {
log.error(e.getMessage(), e);
return false;
}
}
// 检查 文件
String filePathName = StringUtils.join(filePath, File.separator, lotNo, ".xls");
Path path = Paths.get(filePathName);
if (Files.notExists(path)) {
try {
// 创建文件
Files.createFile(path);
} catch (Exception e) {
log.error("创建文件异常!");
return false;
}
}
String[] firstRow = {"我是程序员"};
String[] secondRow = {"no"};
WritableWorkbook workbook = null;
try (
OutputStream ops = Files.newOutputStream(Paths.get(filePathName));
) {
// 第一次写入
int n = 0;
int m = 0;
// 创建可写的工作薄对象- jxl类库
workbook = Workbook.createWorkbook(ops);
// 创建新的一页
WritableSheet sheet = workbook.createSheet("sheet1", 0);
// 创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
// 在表格中创建第一行数据
for (String row : firstRow) {
//创建单元格
Label label = new Label(n, 0, row);
sheet.addCell(label);
n++;
}
// 在表格中填充第二行数据
for (String row : secondRow) {
Label label = new Label(m, 1, row);
sheet.addCell(label);
m++;
}
// 把创建的内容写入到输出流中,并关闭输出流
workbook.write();
//必须在这里关闭流,如果在finally里面加的话会抛异常,本地文件中也不会写入内容。
workbook.close();
} catch (IOException | WriteException e) {
//这里处理 workbook的异常
log.error("workbook 写入异常!", e);
}
return true;
}