当前位置:   article > 正文

java使用openOffice将excel转换为pdf前,对excel进行预处理,将所有列显示在一页,将所有已经折叠的行全部展开_openoffice excel

openoffice excel
  1. import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
  2. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  3. import org.apache.poi.ss.usermodel.Cell;
  4. import org.apache.poi.ss.usermodel.Row;
  5. import org.apache.poi.ss.usermodel.Sheet;
  6. import org.apache.poi.ss.usermodel.Workbook;
  7. import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9. import org.springframework.stereotype.Component;
  10. import java.io.FileInputStream;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. * When converting to excel, change the page size first.
  17. * 2023-08-04
  18. *
  19. * @author LiZhongxin
  20. */
  21. @Component
  22. public class ExcelConverterRefreshFilter {
  23. /**
  24. * 修改excel页面大小
  25. */
  26. public void refresh(String oldFilePath, String oldType, String newFilePath) throws IOException {
  27. Workbook workbook = null;
  28. if (oldType.equals("xls")) {
  29. workbook = new HSSFWorkbook(new FileInputStream(oldFilePath));
  30. } else if (oldType.equals("xlsx")) {
  31. workbook = new XSSFWorkbook(new FileInputStream(oldFilePath));
  32. }
  33. assert workbook != null;
  34. int sheetCount = workbook.getNumberOfSheets();
  35. for (int i = 0; i < sheetCount; i++) {
  36. Sheet sheet = workbook.getSheetAt(i);
  37. //setp1: 将所有列显示在一页上.
  38. setPageSize(sheet, oldType);
  39. //step 2: 将所有行全部展开。
  40. setRowHeight(sheet);
  41. //这是官方写法,自动行高,好像碰到单元格合并的就不好使了,所有下边算法是我自己写的
  42. // XSSFRow xssfRow = (XSSFRow) sheet.getRow(j);
  43. // xssfRow.getCTRow().setCustomHeight(false);
  44. }
  45. workbook.write(new FileOutputStream(newFilePath));
  46. }
  47. private void setPageSize(Sheet sheet, String oldType) {
  48. if (oldType.equals("xls")) {
  49. //set all columns to appear on one page.
  50. HSSFPrintSetup printSetup = (HSSFPrintSetup) sheet.getPrintSetup();
  51. sheet.setAutobreaks(true);
  52. printSetup.setFitWidth((short) 1);
  53. printSetup.setFitHeight((short) 0);
  54. } else if (oldType.equals("xlsx")) {
  55. //set all columns to appear on one page.
  56. XSSFPrintSetup printSetup = (XSSFPrintSetup) sheet.getPrintSetup();
  57. printSetup.setFitHeight((short) 0);
  58. sheet.setFitToPage(true);
  59. }
  60. }
  61. private void setRowHeight(Sheet sheet) {
  62. //1.获取出现次数最多的行高作为一个基准.
  63. int mostRowHeight = getMostRowHeight(sheet);
  64. //2.计算出上下冗余.
  65. int redundancy = mostRowHeight > 0 ? mostRowHeight / 2 : 0;
  66. for (int j = 0; j < sheet.getLastRowNum(); j++) {
  67. Row row = sheet.getRow(j);
  68. //3.获取每行行高.
  69. int rowHeight = row.getHeight();
  70. int newRowHeight = 0;
  71. int cellNum = row.getLastCellNum();
  72. for (int k = 0; k < cellNum; k++) {
  73. Cell cell = row.getCell(k);
  74. //4.判断每行是否在大多数行的上限范围内.
  75. if ((rowHeight - mostRowHeight >= 0 && rowHeight - mostRowHeight <= redundancy) || (rowHeight - mostRowHeight <= 0 && rowHeight - mostRowHeight >= redundancy)) {
  76. System.out.println(cell.toString());
  77. //5.判断每个单元格中是否存在换行.
  78. String[] state = cell.toString().split("\n");
  79. //6.如果有换行,则行高乘以换行数.
  80. if (state.length > 1) {
  81. newRowHeight = state.length * rowHeight;
  82. }
  83. }
  84. }
  85. //7.设置新的行高.
  86. if (newRowHeight != 0) {
  87. row.setHeight((short) newRowHeight);
  88. }
  89. }
  90. }
  91. private int getMostRowHeight(Sheet sheet) {
  92. //1.索取所有行高.
  93. Map<Integer, Integer> rowHeightMap = new HashMap<>();
  94. for (int j = 0; j < sheet.getLastRowNum(); j++) {
  95. Row row = sheet.getRow(j);
  96. int rowHeight = row.getHeight();
  97. //2.判断map中是否存在此行高。如果存在,则将行高的值设置为+1。如果不存在,请将行高的值设置为1
  98. if (rowHeightMap.containsKey(rowHeight)) {
  99. rowHeightMap.put(rowHeight, rowHeightMap.get(rowHeight) + 1);
  100. } else {
  101. rowHeightMap.put(rowHeight, 1);
  102. }
  103. }
  104. //3.从map中获取最大值,即出现次数最多的行的高度。
  105. int mostRowHeight = 0;
  106. int mostRowHeightCount = 0;
  107. for (Map.Entry<Integer, Integer> entry : rowHeightMap.entrySet()) {
  108. if (entry.getValue() > mostRowHeightCount) {
  109. mostRowHeightCount = entry.getValue();
  110. mostRowHeight = entry.getKey();
  111. }
  112. }
  113. return mostRowHeight;
  114. }
  115. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/825736
推荐阅读
相关标签
  

闽ICP备14008679号