excel 处理为pdf



import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.cms.dao.OrderQualityMapper;
import com.cms.entity.Msg;
import com.cms.entity.OrderPacking;
import com.cms.entity.OrderQuality;
import com.cms.entity.UfOmsOrderSop;
import com.cms.service.OrderPackService;
import com.cms.service.UfOmsOrderSopService;
import com.cms.util.DateUtil;
import com.cms.util.ResultUtil;
import com.cms.vo.base.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



@RestController
@RequestMapping("/uploadword")
@Api(tags = "上传word文档")
public class ********* {



/**
* 在线查看文档
*
* @return
*/
@RequestMapping("/asduploadwordsop")
@ApiOperation(value = "查看word文档接口", notes = "查看word文档接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "Id", value = "主键id", dataType = "String"),
})
public void asduploadwordsop(@RequestParam String Id, HttpServletResponse response) throws IOException {
object sop = null;
FileInputStream bis = null;
OutputStream os1 = null;
FileOutputStream os = null;
try {
String href = sop.getWordpath().substring(sop.getWordpath().indexOf(".") + 1);
if ("doc".equals(href) || "docx".equals(href) || "DOC".equals(href) || "DOCX".equals(href)) {
//除去pdf水印
InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);


File file1 = new File("D:\\**");
if (!file1.exists()) {// 目录不存在就创建
file1.mkdirs();
}
File file = new File("D:\\**\\demo.pdf"); //新建一个空白pdf文档
if (file.exists()) {
file.delete();
}
//转PDF
os = new FileOutputStream(file);
Document doc = new Document(sop.getWordpath()); //Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
//输出到浏览器
response.setContentType("text/html; charset=UTF-8");
response.setContentType("application/pdf");
bis = new FileInputStream(file.getPath());
}
if ("xls".equals(href) || "xlsx".equals(href) || "XLS".equals(href) || "XLSX".equals(href)) {
//除去pdf水印
InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml");
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);


File file1 = new File("D:\\***");
if (!file1.exists()) {// 目录不存在就创建
file1.mkdirs();
}
File file = new File("D:\\***\\***.pdf"); //新建一个空白pdf文档
if (file.exists()) {
file.delete();
}


Workbook wb = new Workbook(sop.getWordpath());// 原始excel路径
FileOutputStream fileOS = new FileOutputStream(file);
wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
response.setContentType("text/html; charset=UTF-8");
response.setContentType("application/pdf");
bis = new FileInputStream(file.getPath());
}
os1 = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 1024];
while ((count = bis.read(buffer)) != -1) {
os1.write(buffer, 0, count);
}
os1.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os1 != null) {
os1.close();
}
if (os != null) {
os.close();
}
if (bis != null) {
bis.close();
}
}
}


/**
* 下载文档
*
* @return
*/
@RequestMapping("/*****")
public void dowsopdetail( HttpServletResponse response) throws IOException {
object sop = null;
InputStream fis = null;
OutputStream toClient = null;
try {
// path是指欲下载的文件的路径。
File file = new File(sop.getWordpath());
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1);
filename = sop.getWordname() + "." + ext;


// 以流的形式下载文件。
fis = new BufferedInputStream(new FileInputStream(sop.getWordpath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO8859-1"));
response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
} finally {
if (fis != null) {
fis.close();
}
if (toClient != null) {
toClient.close();
}
}


}



public boolean setFile(MultipartFile multipartFile) throws IOException {
boolean filetype = false;
try {
// 获取文件名
String name = "";
if (multipartFile != null && !StringUtils.isEmpty(packingId)) {
name = multipartFile.getOriginalFilename();// 直接返回文件的名字
String subffix = name.substring(name.lastIndexOf(".") + 1, name.length());// 我这里取得文件后缀
String name1 = name.substring(0, name.lastIndexOf("."));
String fileName = packingId + DateUtil.getDateFormat(new Date(), "YYYYMMDDHHMMSS");// 文件保存进来,我给他重新命名
String filepath = "D:\\***\\**" + packingId;// 获取项目路径到webapp
File file = new File(filepath);
if (!file.exists()) {// 目录不存在就创建
file.mkdirs();
}
multipartFile.transferTo(new File(file + "\\" + fileName + "." + subffix));// 保存文件
// 保存数据到数据库
******

}
if (multipartFile != null && !StringUtils.isEmpty(qualityId)) {
name = multipartFile.getOriginalFilename();// 直接返回文件的名字
String subffix = name.substring(name.lastIndexOf(".") + 1, name.length());// 我这里取得文件后缀
String name1 = name.substring(0, name.lastIndexOf("."));
String fileName = qualityId + DateUtil.getDateFormat(new Date(), "YYYYMMDDHHMMSS");// 文件保存进来,我给他重新命名
String filepath = "D:\\***\\**" + qualityId;// 获取项目路径到webapp
File file = new File(filepath);
if (!file.exists()) {// 目录不存在就创建
file.mkdirs();
}
multipartFile.transferTo(new File(file + "\\" + fileName + "." + subffix));// 保存文件
// 保存数据到数据库
**************
}
filetype = true;
} catch (Exception e) {
System.out.println(e);
filetype = false;
return filetype;
}
return filetype;
}
}


 

license.xml 

<?xml version="1.0"?>
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

aspose-words-15.8.0-jdk16.jar  需要jar包
aspose-cells-8.5.2.jar


这个地址永久有效

链接:https://pan.baidu.com/s/1g6QSHDmXO-PHQsW7NiciGA
提取码:lmqr

posted @ 2023-11-03 08:03  喜欢22度的晴天  阅读(6)  评论(0编辑  收藏  举报