|
@@ -0,0 +1,383 @@
|
|
|
+package com.jd.printserver.utils;
|
|
|
+
|
|
|
+import com.jacob.activeX.ActiveXComponent;
|
|
|
+import com.jacob.com.ComThread;
|
|
|
+import com.jacob.com.Dispatch;
|
|
|
+import com.jacob.com.Variant;
|
|
|
+import com.jd.printserver.common.constants.Constants;
|
|
|
+import com.jd.printserver.javafx.entity.PrintParam;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class JacobUtils {
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String excelFilePath = "C:\\Users\\Administrator\\Desktop\\test.xlsx";
|
|
|
+ PrintParam printParam = new PrintParam();
|
|
|
+ excelToPDF(excelFilePath,printParam);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取 生成的 pdf 文件路径,默认与源文件同一目录
|
|
|
+ *
|
|
|
+ * @param excelFilePath excel文件
|
|
|
+ * @return 生成的 pdf 文件
|
|
|
+ */
|
|
|
+ public static String getPdfFilePath(String excelFilePath) {
|
|
|
+ String[] s = excelFilePath.split("\\.");
|
|
|
+ return s[0] + ".pdf";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String excelToPDF(String inputFilePath, PrintParam printParam){
|
|
|
+ String outputFilePath = getPdfFilePath(inputFilePath);
|
|
|
+ return excelToPDF(inputFilePath, outputFilePath, printParam);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用jacob实现excel转PDF
|
|
|
+ *
|
|
|
+ * @param inputFilePath 导入Excel文件路径
|
|
|
+ * @param outputFilePath 导出PDF文件路径
|
|
|
+ */
|
|
|
+ public static String excelToPDF(String inputFilePath, String outputFilePath, PrintParam printParam) {
|
|
|
+ ActiveXComponent ax = null;
|
|
|
+ Dispatch excel = null;
|
|
|
+ try {
|
|
|
+ ComThread.InitSTA();
|
|
|
+ ax = new ActiveXComponent("Excel.Application");
|
|
|
+ // 这里Visible是控制文档打开后是可见还是不可见,若是静默打印,那么第三个参数就设为false就好了
|
|
|
+ ax.setProperty("Visible", new Variant(false));
|
|
|
+ //禁用宏
|
|
|
+ ax.setProperty("AutomationSecurity", new Variant(3));
|
|
|
+
|
|
|
+ Dispatch excels = ax.getProperty("Workbooks").toDispatch();
|
|
|
+
|
|
|
+ Object[] obj = {
|
|
|
+ inputFilePath,
|
|
|
+ new Variant(false),
|
|
|
+ new Variant(false)
|
|
|
+ };
|
|
|
+ //将excel表格 设置成A4的大小
|
|
|
+ //A3是8,A4是9,A5是11等等
|
|
|
+ //excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();
|
|
|
+ excel = Dispatch.call(excels, "Open", inputFilePath, new Integer(0), Boolean.FALSE).toDispatch();
|
|
|
+ //获取到sheets的集合对象
|
|
|
+ Dispatch sheets = Dispatch.get(excel, "sheets").toDispatch();
|
|
|
+ //获取到总表数
|
|
|
+ int count = Dispatch.get(sheets, "count").changeType(Variant.VariantInt).getInt();
|
|
|
+ if(count < 1){
|
|
|
+ log.error("EXCEL数据为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ //获取到sheet页
|
|
|
+ // new Object[]{1} 第一个sheet
|
|
|
+ Dispatch sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get, new Object[]{1}, new int[1]).toDispatch();
|
|
|
+
|
|
|
+ setPrintParam2Worksheet(sheet,printParam);
|
|
|
+
|
|
|
+ //转换格式
|
|
|
+ Object[] obj2 = {
|
|
|
+ //PDF格式等于0
|
|
|
+ new Variant(0),
|
|
|
+ outputFilePath,
|
|
|
+ //0=标准(生成的PDF图片不会模糊),1=最小的文件
|
|
|
+ new Variant(0)
|
|
|
+ };
|
|
|
+ Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]);
|
|
|
+ return outputFilePath;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("EXCEL转换PDF失败", e);
|
|
|
+ return null;
|
|
|
+ } finally {
|
|
|
+ if (excel != null) {
|
|
|
+ Dispatch.call(excel, "Close", new Variant(false));
|
|
|
+ }
|
|
|
+ if (ax != null) {
|
|
|
+ ax.invoke("Quit", new Variant[]{});
|
|
|
+ ax = null;
|
|
|
+ }
|
|
|
+ ComThread.Release();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setPrintParam2Worksheet(Dispatch worksheet, PrintParam printParam){
|
|
|
+ Dispatch page = Dispatch.get(worksheet, "PageSetup").toDispatch();
|
|
|
+
|
|
|
+ //int[] excelSize = getExcelWidthAndHeight(worksheet);
|
|
|
+ PaperSize paperSize =PaperSize.resultPaperSize2Value(printParam.getWidth(),printParam.getHeight());
|
|
|
+ //int excelWidth = excelSize[0];
|
|
|
+ //int excelHeight = excelSize[1];
|
|
|
+// int printWidth = paperSize.getWidth() - printParam.getMarginLeft() -printParam.getMarginRight();
|
|
|
+// int printHeight = paperSize.getHeight() - printParam.getMarginTop() - printParam.getMarginBottom();
|
|
|
+
|
|
|
+ // 纵向缩放比例
|
|
|
+ //float zScale = getMinScale(printWidth, printHeight, excelWidth, excelHeight);
|
|
|
+ // 横向缩放比例
|
|
|
+ //float hScale = getMinScale(printHeight, printWidth, excelWidth, excelHeight);
|
|
|
+ //缩放比例
|
|
|
+ float scale = printParam.getScale();
|
|
|
+ //打印方向 0 自动 1 横向 2 纵向
|
|
|
+ int orientation = printParam.getOrientation();
|
|
|
+ // 打印缩放 1 无打印缩放 2 将所有列打印在一页 3 将所有行打印在一页 4 自定义缩放
|
|
|
+ int fitToPages = printParam.getFitToPages();
|
|
|
+
|
|
|
+// if(orientation == 0){
|
|
|
+// //自动方向
|
|
|
+// if(zScale < hScale){
|
|
|
+// // 横向需要缩小比例小
|
|
|
+// // 打印方向 0 自动 1 横向 2 纵向
|
|
|
+// orientation = 1;
|
|
|
+// }else{
|
|
|
+// // 纵向需要缩小比例小
|
|
|
+// //打印方向 0 自动 1 横向 2 纵向
|
|
|
+// orientation = 2;
|
|
|
+// }
|
|
|
+// printParam.setOrientation(orientation);
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+// if(fitToPages == 4 && scale == 0f){
|
|
|
+// //自动缩放
|
|
|
+// if(orientation == 1){
|
|
|
+// //横向
|
|
|
+// scale = hScale;
|
|
|
+// }else if(orientation == 2){
|
|
|
+// //纵向
|
|
|
+// scale = zScale;
|
|
|
+// }
|
|
|
+// printParam.setScale(scale);
|
|
|
+// }
|
|
|
+
|
|
|
+ //页面大小 A3是8,A4是9,A5是11等等
|
|
|
+ Dispatch.put(page, "PaperSize", new Integer(paperSize.getValue()));
|
|
|
+
|
|
|
+ //左
|
|
|
+ Dispatch.put(page, "LeftMargin", new Variant(printParam.getMarginLeft()));
|
|
|
+ //上
|
|
|
+ Dispatch.put(page, "TopMargin", new Variant(printParam.getMarginTop()));
|
|
|
+ //右
|
|
|
+ Dispatch.put(page, "RightMargin", new Variant(printParam.getMarginRight()));
|
|
|
+ //下
|
|
|
+ Dispatch.put(page, "BottomMargin", new Variant(printParam.getMarginBottom()));
|
|
|
+ //是否设置区域打印
|
|
|
+ //Dispatch.put(page, "PrintArea", true);
|
|
|
+ //打印方向 0 自动 1 横向 2 纵向
|
|
|
+ if(orientation == 1){
|
|
|
+ //设置横向打印2还是纵向打印1
|
|
|
+ Dispatch.put(page, "Orientation", 2);
|
|
|
+ }else if(orientation == 2){
|
|
|
+ //设置横向打印2还是纵向打印1
|
|
|
+ Dispatch.put(page, "Orientation", 1);
|
|
|
+ }
|
|
|
+ // 打印缩放 1 无打印缩放 2 将所有列打印在一页 3 将所有行打印在一页 4 自定义缩放
|
|
|
+ if(fitToPages == 1){
|
|
|
+ //所有行为一页
|
|
|
+ Dispatch.put(page, "FitToPagesTall", false);
|
|
|
+ //所有列为一页(1或false)
|
|
|
+ Dispatch.put(page, "FitToPagesWide", false);
|
|
|
+ }else if(fitToPages == 2){
|
|
|
+ //所有行为一页
|
|
|
+ Dispatch.put(page, "FitToPagesTall", false);
|
|
|
+ //所有列为一页(1或false)
|
|
|
+ Dispatch.put(page, "FitToPagesWide", 1);
|
|
|
+ }else if(fitToPages == 3){
|
|
|
+ //所有行为一页
|
|
|
+ Dispatch.put(page, "FitToPagesTall", 1);
|
|
|
+ //所有列为一页(1或false)
|
|
|
+ Dispatch.put(page, "FitToPagesWide", false);
|
|
|
+ }else if(fitToPages == 4){
|
|
|
+ //设置缩放,值为100或false
|
|
|
+ Dispatch.put(page, "Zoom", scale);
|
|
|
+ }
|
|
|
+ log.info("sheet页设置成功");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /*public static float getMinScale(int printWidth, int printHeight, int imageWidth, int imageHeight){
|
|
|
+ float scale;
|
|
|
+ float wScale = 1f;
|
|
|
+ if(printWidth < imageWidth){
|
|
|
+
|
|
|
+ wScale = BigDecimal.valueOf(printWidth)
|
|
|
+ .divide(BigDecimal.valueOf(imageWidth),4, BigDecimal.ROUND_DOWN).floatValue();
|
|
|
+ }
|
|
|
+ float hScale = 1f;
|
|
|
+ if(printHeight < imageHeight){
|
|
|
+ hScale = BigDecimal.valueOf(printHeight)
|
|
|
+ .divide(BigDecimal.valueOf(imageHeight), 4, BigDecimal.ROUND_DOWN).floatValue();
|
|
|
+ }
|
|
|
+ if(wScale < hScale){
|
|
|
+ scale = wScale;
|
|
|
+ }else{
|
|
|
+ scale = hScale;
|
|
|
+ }
|
|
|
+ return scale;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取excel的宽和高
|
|
|
+ * @param
|
|
|
+ * @return int[] {excelWidth,excelHeight}
|
|
|
+ */
|
|
|
+ /*public static int[] getExcelWidthAndHeight(Dispatch worksheet){
|
|
|
+ // 获取工作表的列数和行数
|
|
|
+ Variant d = Dispatch.get(worksheet, "Columns").changeType(Variant.VariantInt);
|
|
|
+ Object a = d.getString();
|
|
|
+
|
|
|
+
|
|
|
+ int columns = Dispatch.get(worksheet, "Columns").changeType(Variant.VariantInt).getInt();
|
|
|
+ int rows = Dispatch.get(worksheet, "Rows").changeType(Variant.VariantInt).getInt();
|
|
|
+
|
|
|
+ // 计算总宽度和总高度
|
|
|
+ int excelWidth = 0;
|
|
|
+ int excelHeight = 0;
|
|
|
+ for (int i = 1; i <= columns; i++) {
|
|
|
+ Dispatch column = Dispatch.call(worksheet, "Column", i).toDispatch();
|
|
|
+ excelWidth += Dispatch.get(column, "Width").changeType(Variant.VariantInt).getInt();
|
|
|
+ }
|
|
|
+ for (int i = 1; i <= rows; i++) {
|
|
|
+ Dispatch column = Dispatch.call(worksheet, "Row", i).toDispatch();
|
|
|
+ excelHeight += Dispatch.get(column, "Height").changeType(Variant.VariantInt).getInt();
|
|
|
+ }
|
|
|
+ return new int[] {excelWidth,excelHeight};
|
|
|
+ }*/
|
|
|
+
|
|
|
+ public enum PaperSize{
|
|
|
+ // value请查询https://learn.microsoft.com/zh-cn/office/vba/api/excel.xlpapersize
|
|
|
+ xlPaperA3(1,8,"A3:297mm X 420mm",297,420),
|
|
|
+ xlPaperA4(2,9,"A4:210mm X 297mm",210,297),
|
|
|
+ xlPaperA5(3,11,"A5:148mm X 210mm",148,210),
|
|
|
+ xlPaperB5(4,13,"B5:182mm X 257mm",182,257),
|
|
|
+ xlPaperDC5(5,28,"Devolop C5:162mm X 229mm",162,229),
|
|
|
+ xlPaperDDL(6,27,"Devolop DL:110mm X 220mm",110,220),
|
|
|
+ xlPaperDB5(7,34,"Devolop B5:176mm X 250mm",176,250),
|
|
|
+ xlPaperDM(8,37,"Devolop Monarch:3.875inch X 7.5inch",(int)(3.875 / Constants.INCHESPER_MILLIMETER),(int)(7.5 / Constants.INCHESPER_MILLIMETER)),
|
|
|
+ xlPaperD9(9,19,"Devolop 9:3.875inch X 8.875inch",(int)(3.875 / Constants.INCHESPER_MILLIMETER),(int)(8.875 / Constants.INCHESPER_MILLIMETER)),
|
|
|
+ xlPaperD10(10,20,"Devolop 10:4.125inch X 9.5inch",(int)(4.125 / Constants.INCHESPER_MILLIMETER),(int)(9.5 / Constants.INCHESPER_MILLIMETER)),
|
|
|
+ xlPaperLT(11,1,"Letter:8.5inch X 11inch",(int)(8.5 / Constants.INCHESPER_MILLIMETER),(int)(11 / Constants.INCHESPER_MILLIMETER)),
|
|
|
+ xlPaperLG(12,5,"Legal:8.5inch X 14inch",(int)(8.5 / Constants.INCHESPER_MILLIMETER),(int)(14 / Constants.INCHESPER_MILLIMETER));
|
|
|
+ private int key;
|
|
|
+ private int value;
|
|
|
+ private String name;
|
|
|
+ private int width;
|
|
|
+ private int height;
|
|
|
+
|
|
|
+ private PaperSize(int key, int value, String name, int width, int height){
|
|
|
+ this.key = key;
|
|
|
+ this.value = value;
|
|
|
+ this.name = name;
|
|
|
+ this.width = width;
|
|
|
+ this.height = height;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getValue(){
|
|
|
+ return this.value;
|
|
|
+ }
|
|
|
+ public int getWidth(){
|
|
|
+ return this.width;
|
|
|
+ }
|
|
|
+ public int getHeight(){
|
|
|
+ return this.height;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static PaperSize resultKey2Value(int key){
|
|
|
+ if(PaperSize.xlPaperA3.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperA3;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperA4.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperA4;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperA5.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperA5;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperB5.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperB5;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperDC5.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperDC5;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperDDL.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperDDL;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperDB5.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperDB5;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperDM.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperDM;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperD9.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperD9;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperD10.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperD10;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperLG.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperLG;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperLT.compareKey(key)){
|
|
|
+ return PaperSize.xlPaperLT;
|
|
|
+ }
|
|
|
+ return PaperSize.xlPaperA3;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static PaperSize resultPaperSize2Value(int width, int height){
|
|
|
+ if(PaperSize.xlPaperA3.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperA3;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperA4.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperA4;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperA5.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperA5;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperB5.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperB5;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperDC5.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperDC5;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperDDL.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperDDL;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperDB5.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperDB5;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperDM.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperDM;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperD9.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperD9;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperD10.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperD10;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperLG.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperLG;
|
|
|
+ }
|
|
|
+ if(PaperSize.xlPaperLT.comparePageSize(width,height)){
|
|
|
+ return PaperSize.xlPaperLT;
|
|
|
+ }
|
|
|
+ return PaperSize.xlPaperA3;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean compareKey(int key){
|
|
|
+ if(this.width == key){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean comparePageSize(int width, int height){
|
|
|
+ if(this.width == width && this.height == height){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|