当前位置: 首页 > news >正文

佛山找人做网站长沙seo网站

佛山找人做网站,长沙seo网站,重庆市建设工程信息网综合网,网站开发信息文档SpringBoot 项目 基于aspose相关jar包 将excel 转换成pdf 导出 1、依赖的jar包 &#xff0c; jar获取链接 aspose相关三方jar &#xff0c;下载解压后,在项目路径下建一个libs包&#xff0c;然后将下图两个jar 拷贝至刚新建的libs目录中 2、pom.xml中加入maven引入 <depend…

SpringBoot 项目 基于aspose相关jar包 将excel 转换成pdf 导出

1、依赖的jar包 , jar获取链接 aspose相关三方jar ,下载解压后,在项目路径下建一个libs包,然后将下图两个jar 拷贝至刚新建的libs目录中

在这里插入图片描述

2、pom.xml中加入maven引入

        <dependency><groupId>com.aspose.cells</groupId><artifactId>cells-8.5.2 </artifactId><scope>system</scope><systemPath>${project.basedir}/src/main/resources/libs/aspose-cells-8.5.2.jar</systemPath><version>8.5.2</version></dependency><dependency><groupId>com.aspose.words</groupId><artifactId>words-15.8.0 </artifactId><scope>system</scope><systemPath>${project.basedir}/src/main/resources/libs/aspose-words-15.8.0.jar</systemPath><version>15.8.0</version></dependency>
2.1 使用SpringBoot打包插件生成jar包的时候,你会发现这个jar包不会被打进去,进而出现错误。解决这个问题就需要在maven打包插件中配置一个includeSystemScope属性
<build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><!--设置为true,以便把本地的system的jar也包括进来--><includeSystemScope>true</includeSystemScope></configuration></plugin></plugins></build>

3、编写转换工具类 如下

package com.by.excelToPdf;import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;public class PdfUtil {/*** excel 转 pdf* @param is 输入流* @return 输出流*/public static ByteArrayOutputStream excel2pdf(ByteArrayInputStream is) {ByteArrayOutputStream bos = null;try {bos = new ByteArrayOutputStream();// 验证 LicensegetLicense();Workbook wb = new Workbook(is);PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();pdfSaveOptions.setOnePagePerSheet(true);wb.save(bos, pdfSaveOptions);bos.flush();bos.close();} catch (Exception e) {System.out.println("convert failed");e.printStackTrace();}return bos;}/*** excel 转 pdf** @param excelFilePath excel文件路径*/public static void excel2pdf(String excelFilePath) {excel2pdf(excelFilePath, null, null);}/*** excel 转 pdf** @param excelFilePath excel文件路径* @param convertSheets 需要转换的sheet*/public static void excel2pdf(String excelFilePath, int[] convertSheets) {excel2pdf(excelFilePath, null, convertSheets);}/*** excel 转 pdf** @param excelFilePath excel文件路径* @param pdfFilePath   pdf文件路径*/public static void excel2pdf(String excelFilePath, String pdfFilePath) {excel2pdf(excelFilePath, pdfFilePath, null);}/*** excel 转 pdf** @param excelFilePath excel文件路径* @param pdfFilePath   pdf文件路径* @param convertSheets 需要转换的sheet*/public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {try {pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;// 验证 LicensegetLicense();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();}}/*** 获取 生成的 pdf 文件路径,默认与源文件同一目录** @param excelFilePath excel文件* @return 生成的 pdf 文件*/private static String getPdfFilePath(String excelFilePath) {return excelFilePath.split("\\.")[0] + ".pdf";}/*** 获取 license 去除水印* 若不验证则转化出的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();}}/*** 隐藏workbook中不需要的sheet页。** @param sheets 显示页的sheet数组*/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);}}}
}

4、调用 工具类中有基于流的方式入参、文件地址方式入参等,大家可根据自行需要选择合适的转换方法

    public static void main(String[] args) {String inputFile = "D:/testPdf/222.xlsx";String outputFile = "D:/testPdf/222.pdf";PdfUtil.excel2pdf(inputFile, outputFile);}

5、注意问题,以上转换在windows环境运行一切正常,可能部署到linux环境会存在中文乱码,引起乱码的原因可能是因为linux环境无中文相关字体

  • linux环境查看字段方法 字体路径/usr/share/fonts
# 刷新字体缓存
fc-cache
# 查看所有字体
fc-list
# 查看所有中文字体
fc-list :lang=zh
  • 如果无中文字体 我们可能把windows环境中的字段上传至linux字段目录下,windows环境字段路径C:\Windows\Fonts,上传后安装字段
yum -y install mkfontscale mkfontdir fontconfig
# mkfontscale:字体扩展、mkfontdir:新增字体目录、fc-cache:刷新缓存
mkfontscale && mkfontdir && fc-cache 
  • 如果使用docker 容器启动的应用服务,则还需要使用挂载卷的方式,将宿主体的字体和容器共享,具体方式即启动容器时 加上 “-v /usr/share/fonts/:/usr/share/fonts”
http://www.hkea.cn/news/483512/

相关文章:

  • 做搜索关键词任务网站网站维护是什么意思
  • 2018什么做网站百度网盘网页版入口
  • 深圳福田大型商城网站建设石家庄最新疫情最新消息
  • 网站版面结构chatgpt 网站
  • 网站后期推广是谁来做广州百度推广开户
  • 不上此网站枉做男人免费制作网站平台
  • 防红短链接生成佛山抖音seo
  • 网站建设php带数据库模板站长工具四叶草
  • 做网站客户拖着不验收店铺推广渠道有哪些方式
  • 站群系统哪个好用怎样进行seo推广
  • 淄博网站建设方案网络推广是做什么的
  • 网站建设销售工作职责seo平台怎么样
  • 免费的网站给一个百度seo优化服务项目
  • 现代广告创意设计郑州百度网站优化排名
  • 兰州网站建设哪家专业谷歌seo网站推广
  • 崇信门户网站留言回复上优化seo
  • 网站建设费用有哪些站长工具搜索
  • 云主机能干什么独立站seo怎么做
  • 苏州专业网站建设设计公司免费发布推广信息的b2b
  • 空间 两个网站网络推广培训班
  • 零基础学做网站推广公司简介
  • 公司做网站最好引擎搜索器
  • 济南手工网站建设北京百度seo服务
  • 网站建设原创百度seo官网
  • 徐州企业网站建设衡阳seo服务
  • 网站自然排名优化seo专员是什么职业
  • 视频网站制作广告代理公司
  • wordpress主题域名授权密钥生成镇海seo关键词优化费用
  • 北京东直门+网站建设汕头seo外包平台
  • 长沙 做网站企业网络组网设计