• 一、excel转pdf格式jacob.jar


    1.下载jacob.jar包

    jacob资料
    2.将jacob-1.17-M2-x64.dll 存放在jdk/bin目录下


    3.导入本地依赖

    1. <dependency>
    2.      <groupId>com.jacobgroupId>
    3.      <artifactId>jacobartifactId>
    4.      <version>1.19version>
    5.      <scope>systemscope>
    6.      
    7.      <systemPath>D:\jacob-1.17-M2\jacob.jarsystemPath>
    8. dependency>


    4.直接传入输入的excel和输出的pdf路径

     示例代码:

    1. package com.ysb.zy.util.pdf;
    2. import com.jacob.activeX.ActiveXComponent;
    3. import com.jacob.com.ComThread;
    4. import com.jacob.com.Dispatch;
    5. import com.jacob.com.Variant;
    6. public class Utils {
    7. /**
    8. * 将excel到pdf文件
    9. * @param inputFilePath
    10. * @param outputFilePath
    11. */
    12. public static void jacobExcelToPDFs(String inputFilePath, String outputFilePath) {
    13. ActiveXComponent ax = null;
    14. Dispatch excel = null;
    15. try {
    16. ComThread.InitSTA();
    17. ax = new ActiveXComponent("Excel.Application");
    18. ax.setProperty("Visible", new Variant(false));
    19. //禁用宏
    20. ax.setProperty("AutomationSecurity", new Variant(3));
    21. Dispatch excels = ax.getProperty("Workbooks").toDispatch();
    22. Object[] obj = {
    23. inputFilePath,
    24. new Variant(false),
    25. new Variant(false)
    26. };
    27. excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();
    28. //获取到sheets的集合对象
    29. Dispatch sheets = Dispatch.get(excel, "sheets").toDispatch();
    30. //获取到总表数
    31. int count = Dispatch.get(sheets, "count").changeType(Variant.VariantInt).getInt();
    32. for (int i = 1; i <= count; i++) {
    33. //获取到sheet页
    34. Dispatch sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get, new Object[]{1}, new int[1]).toDispatch();
    35. Dispatch page = Dispatch.get(sheet, "PageSetup").toDispatch();
    36. //设置横向打印还是纵向打印
    37. Dispatch.put(page, "Orientation", 2);
    38. //设置缩放,值为100或false
    39. Dispatch.put(page, "Zoom", false);
    40. //所有行为一页
    41. Dispatch.put(page, "FitToPagesTall", false);
    42. //所有列为一页(1或false)
    43. Dispatch.put(page, "FitToPagesWide", 1);
    44. }
    45. //转换格式
    46. Object[] obj2 = {
    47. //PDF格式等于0
    48. new Variant(0),
    49. outputFilePath,
    50. //0=标准(生成的PDF图片不会模糊),1=最小的文件
    51. new Variant(0)
    52. };
    53. Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]);
    54. } catch (Exception e) {
    55. e.printStackTrace();
    56. throw e;
    57. } finally {
    58. if (excel != null) {
    59. Dispatch.call(excel, "Close", new Variant(false));
    60. }
    61. if (ax != null) {
    62. ax.invoke("Quit", new Variant[]{});
    63. ax = null;
    64. }
    65. ComThread.Release();
    66. }
    67. }
    68. public static void main(String[] args) {
    69. // D:\test
    70. jacobExcelToPDFs("D:\\test\\test.xlsx","D:\\test\\test.pdf");
    71. }
    72. }
  • 相关阅读:
    axios源码学习
    360收录量查询易语言代码
    录屏软件哪个好?比较好用的录屏软件,这4款值得一试!
    基于51单片机的贪吃蛇游戏设计
    2023-mac brew安装python最新版本,遇见的问题和处理方式
    算法模块如何保证依赖数据的同步
    LeetCode 第二天 977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II
    关于嵌入式的技术竞争力需要花点时间整理一下给大家
    高维多元时序数据之间的相似性度量
    mencpy和strcpy的区别?
  • 原文地址:https://blog.csdn.net/weixin_49576031/article/details/125905187