把html字符串转成base64编码传入,直接传入html各种标签后端无法直接接收
接收htmlbase64编码然后通过浏览器下载文件
java
@Override
public void htmlToExcel(ExportPo po, HttpServletResponse response)throws Exception {
String fileName=po.getFileName()+".xlsx";
log.info("开始导出"+fileName);
byte[] decodedBytes = Base64.getDecoder().decode(po.getHtmlBaseCode64());
ByteArrayInputStream inputStream = new ByteArrayInputStream(decodedBytes);
Workbook workbook = new Workbook(inputStream);
response.setContentType("application/octet-stream");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
try (ServletOutputStream outputStream = response.getOutputStream()){
workbook.save(outputStream, SaveFormat.XLSX);
outputStream.flush();
}
log.info(fileName+"导出完成");
}
@Override
public void htmlToWord(ExportPo po, HttpServletResponse response) throws Exception {
String fileName=po.getFileName()+".docx";
log.info("开始导出"+fileName);
byte[] decodedBytes = Base64.getDecoder().decode(po.getHtmlBaseCode64());
ByteArrayInputStream inputStream = new ByteArrayInputStream(decodedBytes);
Document doc = new Document(inputStream);
response.setContentType("application/octet-stream");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
try (ServletOutputStream outputStream = response.getOutputStream()){
doc.save(outputStream, com.aspose.words.SaveFormat.DOCX);
outputStream.flush();
}
log.info(fileName+"导出完成");
}
读取本地html文件转出后保存到本地
java
private static void htmlToExcel() throws Exception {
String htmlFile = "C:\\Users\\lenovo\\Desktop\\111.html";
String excelFile = "C:\\Users\\lenovo\\Desktop\\test2.xlsx";
Workbook workbook=new Workbook(htmlFile);
workbook.save(excelFile);
}
private static void htmlToWord() throws Exception {
String htmlFile = "C:\\Users\\lenovo\\Desktop\\111.html";
String wordFile = "C:\\Users\\lenovo\\Desktop\\保供简报.docx";
Document doc = new Document(htmlFile);
doc.save(wordFile, SaveFormat.DOCX);
}
}
java
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<scope>system</scope>
<version>25.10</version>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-25.10.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<scope>system</scope>
<version>19.3</version>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-words-19.3.jar</systemPath>
</dependency>