在日常开发过程中,我们可能会遇到需要向Excel文件里面写入内容的情况,下面就给大家演示一下如何通过java实现对Excel文件内容写入和导出。
首先我们需要在pom文件中引入POI相关依赖:
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
<exclusions>
<exclusion>
<artifactId>xmlbeans</artifactId>
<groupId>org.apache.xmlbeans</groupId>
</exclusion>
<exclusion>
<artifactId>poi-ooxml</artifactId>
<groupId>org.apache.poi</groupId>
</exclusion>
<exclusion>
<artifactId>poi</artifactId>
<groupId>org.apache.poi</groupId>
</exclusion>
<exclusion>
<artifactId>poi-ooxml-schemas</artifactId>
<groupId>org.apache.poi</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.0</version>
</dependency>
在这里插入代码片
然后在代码中实现对Excel的读取和写入
try {
//根据类型获取不同的导出模板,读取本地Excel,转成文件写入流
if ("2".equals(reqDto.getTimeAccuracy())) {
inputStream = new FileInputStream(fileUrl + "2023年第四季度填报模板.xlsx");
} else {
inputStream = new FileInputStream(fileUrl + "2023年填报模板.xlsx");
}
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
for (Row row : sheet) {
if (row == null) {
continue;
}
String id = getCellStringValue(row.getCell(0));
if (!"指标编号".equals(id.trim())) {
String name = getCellStringValue(row.getCell(1));
FillingTemplate fillingTemplate = compareList.get(id + "|" + name.trim());
if (ObjectUtil.isNotEmpty(fillingTemplate)) {
Cell cell = row.getCell(2);
//这里判断当前cell是否有值,没有值的话默认读取excel中cell是不存在的,需要手动创建
if (cell == null) {
cell = row.createCell(2);
}
//逻辑处理赋值,可忽略
if (!StringUtils.isEmpty(fillingTemplate.getValue())) {
cell.setCellValue(fillingTemplate.getValue());
} else {
if (!StringUtils.isEmpty(fillingTemplate.getIndicatorId())) {
IndicatorVo indicatorValue = getIndicatorValue(fillingTemplate.getIndicatorId(), data);
if (!ObjectUtil.isEmpty(indicatorValue.getOriginalValue()) && !ObjectUtil.isEmpty(indicatorValue.getValue())) {
cell.setCellValue(indicatorValue.getValue().toString());
} else if (!ObjectUtil.isEmpty(indicatorValue.getOriginalValue()) && ObjectUtil.isEmpty(indicatorValue.getValue())) {
cell.setCellValue(indicatorValue.getOriginalValue().toString());
}
}
}
}
}
}
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("123123", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
response.setContentType("application/octet-stream;charset=utf-8");
workbook.write(response.getOutputStream());
workbook.close();
// outputStream.close();
} catch (Exception e) {
log.error("修改文档异常:" + e.getMessage());
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
在这里插入代码片
以上就是读取excel,然后写入到excel流中的过程,然后就是在前端导出excel就行了。