1. 添加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
2.
```
@PostMapping(value = "/mergeCell",produces = {"application/json;charset=utf-8"})
@ResponseBody
public HashMap<String,String> mergeCell(@RequestParam("file") MultipartFile file){
String fileName = file.getOriginalFilename();
System.out.println(fileName);
String filePath = "D:\\WorkPlace";
File dir = new File(filePath);
if(!dir.exists()){
dir.mkdir();
}
String originFilePath = filePath+File.separator+fileName;
String newFilePath = filePath+File.separator+"合并后"+fileName;
System.out.println(filePath);
try {
MergeCellUtil.mergeCellAndSave(originFilePath,newFilePath);
return new HashMap<String, String>(){
{put("单元格合并", "合并成功");}};
} catch (Exception e) {
return new HashMap<String,String>(){
{put("单元格合并","合并失败");}};
}
}
```
3.
/**
* 检测 多次给付重疾险智核问卷0810.xlsx
*/
@Slf4j
public class MergeCellUtil {
/**
* 列方向合并相同数据的单元格. 空白单元格不合并. 将文件保存到硬盘
*
* @param originFilePath xlsx 原文件
* @param newFilePath 合并后生成的 xlsx 文件保存路径
*/
publi
POI实现合并单元格
于 2022-08-14 03:49:26 首次发布