ExportExcel_pj.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package restcontroller.webmanage.sale.aftersalesbom;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import common.YosException;
  5. import common.data.Rows;
  6. import org.apache.poi.ss.usermodel.*;
  7. import org.apache.poi.ss.util.CellRangeAddress;
  8. import org.apache.poi.xssf.usermodel.*;
  9. import java.awt.*;
  10. import java.util.Objects;
  11. public class ExportExcel_pj {
  12. /**
  13. * 2022-07-14 17:41:39
  14. * 设置表格宽度(导入模板)
  15. **/
  16. public static void setBatchDetailSheetColumn1(XSSFSheet sheet) {
  17. sheet.setDefaultRowHeight((short) 600);
  18. short width = 3500;
  19. sheet.setColumnWidth((short) 0, 4000);
  20. sheet.setColumnWidth((short) 1, 4000);
  21. for (int i = 1; i < 20; i++) {
  22. sheet.setColumnWidth((short) i, width);
  23. }
  24. }
  25. /**
  26. * 2022-07-14 17:41:39
  27. * 设置表格宽度(返回错误Excel的样式)
  28. **/
  29. public static void setBatchDetailSheetColumn2(XSSFSheet sheet) {
  30. sheet.setDefaultRowHeight((short) 600);
  31. short width = 3000;
  32. sheet.setColumnWidth((short) 0, 4000);
  33. sheet.setColumnWidth((short) 1, 4000);
  34. for (int i = 1; i < 18; i++) {
  35. sheet.setColumnWidth((short) i, width);
  36. }
  37. sheet.setColumnWidth((short) 20, width * 2);
  38. }
  39. /**
  40. * 2022-07-14 17:42:03
  41. * 设置表头
  42. * cellStyle1 中文提示信息样式
  43. * cellStyle2 标题提示信息样式
  44. * cellStyle3 正文提示信息样式
  45. **/
  46. public static void batchDetail(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFCellStyle cellStyle3, XSSFWorkbook workbook, Rows rows) {
  47. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  48. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  49. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  50. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  51. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  52. // bcs.setWrapText(true);
  53. XSSFRow row = null;
  54. XSSFCell cell = null;
  55. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  56. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  57. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  58. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  59. cellStyle1.setWrapText(true);
  60. cellStyle2.setBorderBottom(BorderStyle.THIN); //下边框
  61. cellStyle2.setBorderLeft(BorderStyle.THIN);//左边框
  62. cellStyle2.setBorderTop(BorderStyle.THIN);//上边框
  63. cellStyle2.setBorderRight(BorderStyle.THIN);//右边框
  64. cellStyle2.setWrapText(true);
  65. cellStyle1.setDataFormat(workbook.createDataFormat().getFormat("TEXT"));
  66. cellStyle2.setDataFormat(workbook.createDataFormat().getFormat("TEXT"));
  67. cellStyle3.setDataFormat(workbook.createDataFormat().getFormat("TEXT"));
  68. /*第一行*/
  69. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 18));
  70. /*第一行塞值*/
  71. row = sheet.createRow(0);
  72. cell = row.createCell(0);// ID
  73. cell.setCellStyle(cellStyle1);
  74. cell.setCellValue("注意:①;最多不超过5000行;②导入前,请记得删除示例行!");
  75. /*第二行塞值*/
  76. row = sheet.createRow(1);
  77. cell = row.createCell(0);
  78. cell.setCellStyle(cellStyle2);
  79. cell.setCellValue("bom名称");
  80. cell = row.createCell(1);
  81. cell.setCellStyle(cellStyle2);
  82. cell.setCellValue("层级");
  83. cell = row.createCell(2);
  84. cell.setCellStyle(cellStyle2);
  85. cell.setCellValue("层级");
  86. cell = row.createCell(3);
  87. cell.setCellStyle(cellStyle2);
  88. cell.setCellValue("层级");
  89. int j=0;
  90. for (int i = 1; i < 6; i++) {
  91. cell = row.createCell( 4+j);
  92. cell.setCellStyle(cellStyle2);
  93. cell.setCellValue("物料号"+i);
  94. cell = row.createCell( 5+j);
  95. cell.setCellStyle(cellStyle2);
  96. cell.setCellValue("启用时间"+i);
  97. cell = row.createCell( 6+j);
  98. cell.setCellStyle(cellStyle2);
  99. cell.setCellValue("停用时间"+i);
  100. j=j+3;
  101. }
  102. int a=2;
  103. for(common.data.Row bom : rows){
  104. String bomname = bom.getString("bomname");
  105. String module = bom.getString("module");
  106. String component = bom.getString("component");
  107. String accessorie = bom.getString("accessorie");
  108. /*第三行塞值*/
  109. row = sheet.createRow(a);
  110. cell = row.createCell(0);
  111. //cell.setCellStyle(cellStyle3);
  112. cell.setCellValue(bomname);
  113. cell = row.createCell(1);
  114. //cell.setCellStyle(cellStyle3);
  115. cell.setCellValue(module);
  116. cell = row.createCell(2);
  117. //cell.setCellStyle(cellStyle3);
  118. cell.setCellValue(component);
  119. cell = row.createCell(3);
  120. //cell.setCellStyle(cellStyle3);
  121. cell.setCellValue(accessorie);
  122. a++;
  123. }
  124. //按范围合并单元格
  125. for(int i = 0; i < 4; i++){
  126. mergeSameCellContentColumn(sheet, 2, i);
  127. }
  128. // for (int i = 1; i < 18; i++) {
  129. // cell = row.createCell(i);
  130. // cell.setCellStyle(cellStyle3);
  131. // cell.setCellValue("0");
  132. //
  133. // }
  134. }
  135. /**
  136. * 2022-07-14 17:42:03
  137. * 设置表头
  138. **/
  139. public static void batchDetailErr(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFWorkbook workbook) {
  140. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  141. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  142. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  143. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  144. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  145. // bcs.setWrapText(true);
  146. XSSFRow row = null;
  147. XSSFCell cell = null;
  148. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  149. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  150. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  151. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  152. cellStyle1.setWrapText(true);
  153. /*第一行*/
  154. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 19));//2.提示信息
  155. /*第一行塞值*/
  156. row = sheet.createRow(0);
  157. cell = row.createCell(0);// ID
  158. cell.setCellStyle(cellStyle1);
  159. cell.setCellValue("注意:①最多不超过5000行;②导入前,请记得删除示例行!");
  160. /*第二行塞值*/
  161. row = sheet.createRow(1);
  162. cell = row.createCell(0);
  163. cell.setCellStyle(cellStyle2);
  164. cell.setCellValue("bom名称");
  165. cell = row.createCell(1);
  166. cell.setCellStyle(cellStyle2);
  167. cell.setCellValue("层级");
  168. cell = row.createCell(2);
  169. cell.setCellStyle(cellStyle2);
  170. cell.setCellValue("层级");
  171. cell = row.createCell(3);
  172. cell.setCellStyle(cellStyle2);
  173. cell.setCellValue("层级");
  174. int j=0;
  175. for (int i = 1; i < 6; i++) {
  176. cell = row.createCell( 4+j);
  177. cell.setCellStyle(cellStyle2);
  178. cell.setCellValue("物料号"+i);
  179. cell = row.createCell( 5+j);
  180. cell.setCellStyle(cellStyle2);
  181. cell.setCellValue("启用时间"+i);
  182. cell = row.createCell( 6+j);
  183. cell.setCellStyle(cellStyle2);
  184. cell.setCellValue("停用时间"+i);
  185. j=j+3;
  186. }
  187. cell = row.createCell(19);
  188. cell.setCellStyle(cellStyle2);
  189. cell.setCellValue("错误信息");
  190. //按范围合并单元格
  191. for(int i = 0; i < 4; i++){
  192. mergeSameCellContentColumn(sheet, 2, i);
  193. }
  194. }
  195. public static void setBatchDetailSheetColumn3(XSSFSheet sheet) {
  196. sheet.setDefaultRowHeight((short) 700);
  197. short width = 3500;
  198. sheet.setColumnWidth((short) 0, 4000);
  199. sheet.setColumnWidth((short) 1, 4000);
  200. for (int i = 1; i < 20; i++) {
  201. sheet.setColumnWidth((short) i, width);
  202. }
  203. }
  204. /**
  205. * 合并指定Excel sheet页、指定列中连续相同内容的单元格
  206. *
  207. * @param sheet Excel sheet
  208. * @param startRow 从第几行开始, startRow的值从1开始
  209. * @param column 指定列
  210. */
  211. public static void mergeSameCellContentColumn(Sheet sheet, int startRow, int column) {
  212. int totalRows = sheet.getLastRowNum();
  213. int firstRow = 0;
  214. int lastRow = 0;
  215. // 上一次比较是否相同
  216. boolean isPrevCompareSame = false;
  217. String prevMergeAddress = null;
  218. String currentMergeAddress;
  219. // 从第几开始判断是否相同
  220. if (totalRows >= startRow) {
  221. for (int i = startRow; i <= totalRows; i++) {
  222. String lastRowCellContent = sheet.getRow(i - 1).getCell(column).getStringCellValue();
  223. String curRowCellContent = sheet.getRow(i).getCell(column).getStringCellValue();
  224. if (curRowCellContent.equals(lastRowCellContent)) {
  225. if (!isPrevCompareSame) {
  226. firstRow = i - 1;
  227. }
  228. lastRow = i;
  229. isPrevCompareSame = true;
  230. } else {
  231. isPrevCompareSame = false;
  232. currentMergeAddress = firstRow + lastRow + column + column + "";
  233. if (lastRow > firstRow && !Objects.equals(currentMergeAddress, prevMergeAddress)) {
  234. sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, column, column));
  235. prevMergeAddress = currentMergeAddress;
  236. }
  237. }
  238. // 最后一行时判断是否有需要合并的行
  239. if ((i == totalRows) && (lastRow > firstRow)) {
  240. currentMergeAddress = firstRow + lastRow + column + column + "";
  241. if (!Objects.equals(currentMergeAddress, prevMergeAddress)) {
  242. sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, column, column));
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }