引入依赖
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>14.4.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url> https://repo.e-iceblue.cn/repository/maven-public /</url>
</repository>
</repositories>
完整代码
package com.common.utils;
import com.spire.xls.ExcelVersion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.DateUtil;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class ExcelUtils2 {
/**
*
* @param request
* @param inputStream xls 流
* @param fileName 文件名
* @return
*/
public static Map<String,Object> conver(HttpServletRequest request,InputStream inputStream,String fileName){
Map<String,Object> res = new HashMap<>();
//初始化一个Workbook类的实例
Workbook workbook = new Workbook();
try {
String filePathXLS = createFile(request, fileName);
//XLS写到临时目录
writeToLocal(filePathXLS,inputStream);
//加载XLS文件
workbook.loadFromFile(filePathXLS);
//将XLS文件保存为XLSX格式
String filePathXLSX = filePathXLS+"x";
//输 XLSX 出到filePathXLSX目录
workbook.saveToFile(filePathXLSX, ExcelVersion.Version2016);
//读取XLSX
File fileXLSX = new File(filePathXLSX);
if (fileXLSX.exists()){
//2:实例化InputStream类对象
FileInputStream fis = new FileInputStream(fileXLSX);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fis);
//删除指定sheet
xssfWorkbook.removeSheetAt(xssfWorkbook.getSheetIndex("Evaluation Warning"));
//HSSFWorkbook wb = new HSSFWorkbook(); // create the new Workbook
//HSSFSheet sheet = xssfWorkbook.createSheet(); // create
//生成Excel代码略
ByteArrayOutputStream bos = new ByteArrayOutputStream();
xssfWorkbook.write(bos);
byte[] brray = bos.toByteArray();
InputStream input = new ByteArrayInputStream(brray);
//FileUtils.forceDelete(fileXLSX);
FileUtils.forceDelete(new File(filePathXLS));
//return input;
res.put("stream",input);
//后续需关闭此流
res.put("del",fileXLSX);
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
/**
* 创建临时文件
* @param request
* @param fileName
* @return
* @throws IOException
*/
public static String createFile(HttpServletRequest request,String fileName) throws IOException {
String contextPath = request.getContextPath(); // /JQ_Resource
String absolutePath = System.getProperty("user.dir");
fileName = DateUtils.format(new Date(),DateUtils.YYYYMMDDHHMMSS)+"_"+fileName;
String filePath = absolutePath + "/temp/"+fileName;
String dirPath = absolutePath + "/temp/";
File file = new File(filePath);
File dir = new File(dirPath);
if (dir.isDirectory()) {
// 清除该目录下的文件及子目录文件而不删除该目录文件夹。该目录不存在会报错
//FileUtils.cleanDirectory(dir);
} else {
dir.mkdirs();
}
if (file.exists()) {
// 删除指定文件,不存在报异常
//FileUtils.forceDelete(file);
}else {
file.createNewFile();
}
return filePath;
}
/**
* 将InputStream写入本地文件
* @param destination 写入本地目录
* @param input 输入流
* @throws IOException
*/
private static void writeToLocal(String destination, InputStream input)
throws IOException {
int index;
byte[] bytes = new byte[1024];
FileOutputStream downloadFile = new FileOutputStream(destination);
while ((index = input.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
downloadFile.close();
input.close();
}
}