1.要求:到复杂表格后原模原样在前台展示。
2.思路
- 上传表格
- 将表格转换成pdf
- 将pdf文件以二进制的方式反给前端
3. 代码
1.引入maven 依赖
<!--pdf-->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>22.11</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<repositories>
<!--PDF私库一定要引入-->
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
2.创建PdfUtil
工具类
package com.xhl.xhldongrong.utils;
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import java.io.FileOutputStream;
import java.io.InputStream;
public class PdfUtil {
public static void excel2pdf(String excelFilePath) {
excel2pdf(excelFilePath, null, null);
}
public static void excel2pdf(String excelFilePath, int[] convertSheets) {
excel2pdf(excelFilePath, null, convertSheets);
}
public static void excel2pdf(String excelFilePath, String pdfFilePath) {
excel2pdf(excelFilePath, pdfFilePath, null);
}
public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
Workbook wb = new Workbook(excelFilePath);
FileOutputStream fileOS = new FileOutputStream(pdfFilePath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOS, pdfSaveOptions);
fileOS.flush();
fileOS.close();
System.out.println("convert success");
} catch (Exception e) {
System.out.println("convert failed");
e.printStackTrace();
}
}
private static String getPdfFilePath(String excelFilePath) {
return excelFilePath.split(".")[0] + ".pdf";
}
private static void getLicense() {
String licenseFilePath = "excel-license.xml";
try {
InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream(licenseFilePath);
License license = new License();
license.setLicense(is);
} catch (Exception e) {
System.out.println("license verify failed");
e.printStackTrace();
}
}
private static void printSheetPage(Workbook wb, int[] sheets) {
for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
wb.getWorksheets().get(i).setVisible(false);
}
if (null == sheets || sheets.length == 0) {
wb.getWorksheets().get(0).setVisible(true);
} else {
for (int i = 0; i < sheets.length; i++) {
wb.getWorksheets().get(i).setVisible(true);
}
}
}
}
3.创建ExcelReadController
public class ExcelReadController {
@Value("${escelRead}")
private String fileConfig;
@PostMapping("/uploadFile")
@ApiOperation("上传")
public ApiResponse<String> uploadFile(@RequestParam MultipartFile file) {
String fileName = file.getOriginalFilename();
String filePath = System.getProperty("user.dir") + fileConfig;
java.io.File dest = new java.io.File(filePath + fileName);
java.io.File pfile = new java.io.File(filePath);
if (!pfile.exists()) {
pfile.mkdirs();
}
if (dest.exists()) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddmmss");
fileName = sdf.format(Calendar.getInstance().getTime()) + fileName;
dest = new java.io.File(filePath + fileName);
}
try {
file.transferTo(dest);
} catch (Exception e) {
e.printStackTrace();
}
return ApiResponse.ofSuccess(fileName);
}
@ApiOperation("下载")
@GetMapping("/{filename}")
@ResponseBody
public void outPic(@PathVariable String filename, HttpServletResponse response) {
String name = filename.replaceAll("[.](.*)", "");
String filePath = System.getProperty("user.dir") + fileConfig;
String pdfName = filePath + name + ".pdf";
File file = new File(pdfName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
PdfUtil.excel2pdf(System.getProperty("user.dir") + fileConfig + filename, pdfName);
try {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(pdfName);
byte[] b = new byte[1024];
int t = 0;
while ((t = fis.read(b)) != -1) {
byteArrayOutputStream.write(b, 0, t);
}
response.getOutputStream().write(byteArrayOutputStream.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}