日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

excel转实现pdf、图片、base64互转

發(fā)布時(shí)間:2024/1/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 excel转实现pdf、图片、base64互转 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一:jar包
aspose-cells依賴
excel轉(zhuǎn)成pdf以及去除水印所需依賴包
鏈接:https://pan.baidu.com/s/1rWeG1wgnIP03YmqpD1Vp5g
提取碼:4gw1

//圖片轉(zhuǎn)base所需jar包<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.12</version></dependency> package com.emodor.attendance.utils.itext;import com.aspose.cells.PdfSaveOptions; import com.aspose.cells.Workbook; import org.apache.commons.lang.StringUtils; import org.apache.poi.openxml4j.opc.internal.FileHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sun.misc.BASE64Encoder;import java.io.*; import java.util.Base64;/*** @Author cgz* @Date 2023/2/9 16:37*/ public class ExcelToPDFUtil {/*** @Description: 驗(yàn)證aspose.word組件是否授權(quán):無授權(quán)的文件有水印標(biāo)記*/public static boolean isWordLicense() {return true; // boolean result = false; // ByteArrayInputStream byteArrayInputStream = null; // try { // String s = // "<License>\n" + // " <Data>\n" + // " <Products>\n" + // " <Product>Aspose.Total for Java</Product>\n" + // " <Product>Aspose.Words for Java</Product>\n" + // " </Products>\n" + // " <EditionType>Enterprise</EditionType>\n" + // " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" + // " <LicenseExpiry>20991231</LicenseExpiry>\n" + // " <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" + // " </Data>\n" + // " <Signature>111</Signature>\n" + // "</License>"; // byteArrayInputStream = new ByteArrayInputStream(s.getBytes()); // //InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("license.xml"); // com.aspose.words.License license = new com.aspose.words.License(); // license.setLicense(byteArrayInputStream); // result = true; // } catch (Exception e) { // logger.error("校驗(yàn)aspose.word組件授權(quán)失敗,原因:" , e); // } finally { // IOUtils.closeQuietly(byteArrayInputStream); // } // return result;}//圖片轉(zhuǎn)base64public static String getImageStr(String imgFile) {InputStream inputStream = null;byte[] data = null;try {inputStream = new FileInputStream(imgFile);data = new byte[inputStream.available()];inputStream.read(data);inputStream.close();} catch (IOException e) {e.printStackTrace();}BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);}public static String excel2pdf(String Address) {if (!isWordLicense()) { // 驗(yàn)證License 若不驗(yàn)證則轉(zhuǎn)化出的pdf文檔會(huì)有水印產(chǎn)生return null;}String imageStr=null;try {File pdfFile = new File("D:/data/2.pdf"); // 輸出路徑FileInputStream excelstream = new FileInputStream(Address);Workbook wb = new Workbook(excelstream);// excel路徑,這里是先把數(shù)據(jù)放進(jìn)緩存表里,然后把緩存表轉(zhuǎn)化成PDFFileOutputStream fileOS = new FileOutputStream(pdfFile);PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();pdfSaveOptions.setOnePagePerSheet(true);//參數(shù)true把內(nèi)容放在一張PDF頁面上;wb.save(fileOS, pdfSaveOptions); // Pdf2ImgUtil.pdfToImageLast("D:/data/2.pdf", "D:/data/4.png", 128);imageStr= getImageStr("D:/data/4.png");fileOS.close();} catch (Exception e) {e.printStackTrace();}return imageStr;}public static void main(String[] args) {String s = excel2pdf("D:/data/1.xlsx");System.out.println(s);}/*** PDF文件轉(zhuǎn)PNG圖片,全部頁數(shù)** @param PdfFilePath pdf完整路徑* @param dpi dpi越大轉(zhuǎn)換后越清晰,相對(duì)轉(zhuǎn)換速度越慢* @return 返回轉(zhuǎn)換后圖片集合list*/public static void pdfToImageLast(String PdfFilePath, File files, float dpi) {File file = new File(PdfFilePath);//@SuppressWarnings("resource")//抑制警告PDDocument pdDocument = null;try {if (files != null) {pdDocument = PDDocument.load(file);PDFRenderer renderer = new PDFRenderer(pdDocument);/* dpi越大轉(zhuǎn)換后越清晰,相對(duì)轉(zhuǎn)換速度越慢 */PdfReader reader = new PdfReader(PdfFilePath);int pages = reader.getNumberOfPages();logger.info("PDF文檔轉(zhuǎn)PNG圖片: pdf總共多少頁-----" + pages);BufferedImage image = renderer.renderImageWithDPI(0, dpi);ImageIO.write(image, "png", files);} else {System.out.println("PDF文檔轉(zhuǎn)PNG圖片失敗:" + "創(chuàng)建" + "失敗");}} catch (IOException e) {logger.error("PDF文檔轉(zhuǎn)PNG圖片失敗:", e);} finally {try {pdDocument.close();} catch (IOException e) {logger.error("PDF文件轉(zhuǎn)圖片 - 關(guān)閉PDF文件失敗!!! ", e);}}}}

總結(jié)

以上是生活随笔為你收集整理的excel转实现pdf、图片、base64互转的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。