今天得到个需求,《实现excel文件在线编辑、查看、保存》。
我这里遇到了许多坑,就先分享一下。这里只做在线预览的功能实现,因为其他的功能比较简单。
整体思路:xls转xlsx文件,用luckysheet在线预览
一:首先从客户手里得到了一堆xls文件(坑一:这些文件不是excel文件,只是后缀是xls。打开提示文件格式与后缀不符)
思路:既然xls文件存在问题,那就将用户的这个文件另存为xls文件。
解决一:使用Spire.Xls.jar https://download.csdn.net/download/zhangjiaqianghh/86052515
首先将xls文件转xml文件 ,然后将xml文件转为xlsx文件
二:文件流base64加密传输前端进行展示
后端代码:
pom.xml
spire.xls
工具类:
package com.pcitc.pm.common.util.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ExcelUtil {
public static void xlsToXlsx (String srcPath, String destPath) throws Exception {
//新建空文档,用于复制
Workbook destWorkbook = new Workbook();
//加载原文档
Workbook srcWorkbook = new Workbook();
srcWorkbook.loadFromFile(srcPath);
//原文档表数
int count = srcWorkbook.getWorksheets().size();
for (int i=0; i
//获取原文档第 i 张表
Worksheet srcSheet = srcWorkbook.getWorksheets().get(i);
//获取空文档第 i 张表,若没有则自动添加,
Worksheet destSheet = destWorkbook.getWorksheets().get(i);
//表名与原文档的表名保持一致
destSheet.setName(srcSheet.getName());
//复制
destSheet.copyFrom(srcSheet);
}
//保存文档
destWorkbook.saveToFile(destPath, FileFormat.Version2010);
}
//文件另存为
public static void toSave (String srcPath, String destPath) throws Exception {
Workbook wb = new Workbook();
wb.loadFromFile(srcPath);
wb.saveToFile(destPath);
}
public static void main(String[] args) throws Exception {
//加载xml文件
File file=new File("C://dd//dd//BB-0045_3_202203_0000_01_0.xls");
InputStream is=new FileInputStream(file);
FileOutputStream fos=new FileOutputStream("C://dd//aa//BB-0045_3_202203_0000_01_0.xml");
byte[] buffer=new byte[1024*4];
while(-1!= is.read(buffer)){
fos.write(buffer);
}
Workbook wb = new Workbook();
wb.loadFromXml("C:\dd\aa\BB-0045_3_202203_0000_01_0.xml");
//转为xls格式的Excel
wb.saveToFile("C:\dd\aa\BB-0045_3_202203_0000_01_0.xls",FileFormat.Version97to2003);
// toSave("C:\dd\dd\BB-0045_3_202202_0000_01_0.xls","C:\dd\aa\BB-0045_3_202202_0000_01_0.xls");
// xlsToXlsx("C:\dd\aa\BB-0045_3_202202_0000_01_0.xls", "C:\dd\aa\BB-0045_3_202202_0000_01_0.xlsx");
}
}
java :
@Override
public CommonResult getReportToUrlNew(String id) throws Exception {
CommonResult commonResult = new CommonResult();
ReportInfoPO reportInfoPO =reportRepository.getOne(id);
//ftp服务器文件名称
String urlt=reportInfoPO.getUrlInfo();
//调取ftp服务器上的文件
FTPUtil ftp=new FTPUtil();
FTPClient ftpClient=ftp.getFTPClient("10.105.41.178", 21, "ftpUser123","78a22d39!ftp");
// 跳转到文件目录
ftpClient.changeWorkingDirectory(new String("/RPTFB/XLS/".getBytes("GBK"),"iso-8859-1"));
System.out.println(ftpClient.changeWorkingDirectory(new String("/RPTFB/XLS/".getBytes("GBK"),"iso-8859-1")));
// 获取目录下文件集合
ftpClient.enterLocalPassiveMode();
FTPFile[] files = ftpClient.listFiles();
InputStream is=null;
if(files.length>0){
is=ftpClient.retrieveFileStream(new String(urlt.getBytes("GBK"),"ISO-8859-1"));
}
String urlty=urlt.substring(0,urlt.lastIndexOf("."));
long time=System.currentTimeMillis();
//因为从对方服务器拿到的文件有问题,所以需要先另存为新的文件
FileOutputStream fos=new FileOutputStream("E:/tools/file/"+urlty+time+".xml");
byte[] bufferN=new byte[1024*4];
while(-1!= is.read(bufferN)){
fos.write(bufferN);
}
Workbook wb = new Workbook();
wb.loadFromXml("E:/tools/file/"+urlty+time+".xml");
wb.saveToFile("E:/tools/file/"+urlty+".xlsx",FileFormat.Version2007);
File file=new File("E:/tools/file/"+urlty+".xlsx");
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer2 = new byte[(int)file.length()];
inputFile.read(buffer2);
String base = Base64.getEncoder().encodeToString(buffer2);
commonResult.setResult(base);
commonResult.setIsSuccess(true);
inputFile.close();
is.close();
fos.close();
ftp.closeFTP(ftpClient);
return commonResult;
}
html:



