ExportExcel.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package restcontroller.webmanage.sale.itempriceadjust;
  2. import org.apache.poi.ss.usermodel.BorderStyle;
  3. import org.apache.poi.ss.usermodel.FillPatternType;
  4. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  5. import org.apache.poi.ss.usermodel.IndexedColors;
  6. import org.apache.poi.ss.util.CellRangeAddress;
  7. import org.apache.poi.xssf.usermodel.*;
  8. import java.awt.*;
  9. public class ExportExcel {
  10. /**
  11. * 设置标题单元样式
  12. *
  13. * @param workbook
  14. * @return
  15. */
  16. public static XSSFCellStyle createTitleCellStyle1(XSSFWorkbook workbook) {
  17. XSSFCellStyle cellStyle = workbook.createCellStyle();
  18. XSSFFont font = workbook.createFont();
  19. font.setFontHeightInPoints((short) 12);
  20. font.setColor(IndexedColors.RED.getIndex());
  21. font.setFontName("微软雅黑");// 设置标题字体
  22. cellStyle.setFont(font);
  23. cellStyle.setWrapText(true); //自动换行
  24. cellStyle = workbook.createCellStyle();
  25. cellStyle.setFont(font);// 设置列标题样式
  26. XSSFColor colorGrey = new XSSFColor(new Color(217, 225, 242));
  27. cellStyle.setFillForegroundColor(colorGrey);
  28. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  29. cellStyle.setAlignment(HorizontalAlignment.LEFT); // 居左
  30. return cellStyle;
  31. }
  32. /**
  33. * 设置中文提示信息样式
  34. *
  35. * @param workbook
  36. * @return
  37. */
  38. public static XSSFCellStyle createTitleCellStyle2(XSSFWorkbook workbook) {
  39. XSSFCellStyle cellStyle = workbook.createCellStyle();
  40. XSSFFont font = workbook.createFont();
  41. font.setFontHeightInPoints((short) 12);
  42. font.setColor(IndexedColors.WHITE.getIndex());
  43. font.setFontName("微软雅黑");// 设置标题字体
  44. cellStyle.setFont(font);
  45. cellStyle.setWrapText(true); //自动换行
  46. cellStyle = workbook.createCellStyle();
  47. cellStyle.setFont(font);// 设置列标题样式
  48. XSSFColor colorBlue = new XSSFColor(new Color(48, 84, 150));
  49. cellStyle.setFillForegroundColor(colorBlue);
  50. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  51. cellStyle.setAlignment(HorizontalAlignment.LEFT); // 居左
  52. return cellStyle;
  53. }
  54. /**
  55. * 设置中文提示信息样式
  56. *
  57. * @param workbook
  58. * @return
  59. */
  60. public static XSSFCellStyle createTitleCellStyle3(XSSFWorkbook workbook) {
  61. XSSFCellStyle cellStyle = workbook.createCellStyle();
  62. XSSFFont font = workbook.createFont();
  63. font.setFontHeightInPoints((short) 12);
  64. font.setColor(IndexedColors.WHITE.getIndex());
  65. font.setFontName("微软雅黑");// 设置标题字体
  66. cellStyle.setFont(font);
  67. cellStyle.setWrapText(true); //自动换行
  68. cellStyle = workbook.createCellStyle();
  69. cellStyle.setFont(font);// 设置列标题样式
  70. XSSFColor colorBlue = new XSSFColor(new Color(48, 84, 150));
  71. cellStyle.setFillForegroundColor(colorBlue);
  72. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  73. cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居左
  74. return cellStyle;
  75. }
  76. /**
  77. * 设置正文单元样式
  78. *
  79. * @param workbook
  80. * @return
  81. */
  82. public static XSSFCellStyle createBodyCellStyle(XSSFWorkbook workbook) {
  83. XSSFCellStyle cellStyle = workbook.createCellStyle();
  84. XSSFFont font = workbook.createFont();
  85. font.setFontHeightInPoints((short) 12);
  86. font.setFontName("微软雅黑");// 设置标题字体
  87. // font.setFontName(HSSFFont.FONT_ARIAL);// 设置标题字体
  88. cellStyle.setFont(font);
  89. cellStyle = workbook.createCellStyle();
  90. cellStyle.setAlignment(HorizontalAlignment.LEFT); // 居左
  91. return cellStyle;
  92. }
  93. /**
  94. * 2022-07-14 17:41:39
  95. * 设置表格宽度(导入模板)
  96. **/
  97. public static void setBatchDetailSheetColumn1(XSSFSheet sheet) {
  98. sheet.setDefaultRowHeight((short) 600);
  99. sheet.setColumnWidth((short) 0, (short) 8500);
  100. sheet.setColumnWidth((short) 1, (short) 8500);
  101. sheet.setColumnWidth((short) 2, (short) 8500);
  102. sheet.setColumnWidth((short) 3, (short) 8500);
  103. sheet.setColumnWidth((short) 4, (short) 8500);
  104. sheet.setColumnWidth((short) 5, (short) 8500);
  105. sheet.setColumnWidth((short) 6, (short) 8500);
  106. sheet.setColumnWidth((short) 7, (short) 8500);
  107. }
  108. /**
  109. * 2022-07-14 17:41:39
  110. * 设置表格宽度(返回错误Excel的样式)
  111. **/
  112. public static void setBatchDetailSheetColumn2(XSSFSheet sheet) {
  113. sheet.setDefaultRowHeight((short) 600);
  114. sheet.setColumnWidth((short) 0, (short) 8500);
  115. sheet.setColumnWidth((short) 1, (short) 8500);
  116. sheet.setColumnWidth((short) 2, (short) 8500);
  117. sheet.setColumnWidth((short) 3, (short) 8500);
  118. sheet.setColumnWidth((short) 4, (short) 8500);
  119. sheet.setColumnWidth((short) 5, (short) 8500);
  120. sheet.setColumnWidth((short) 6, (short) 8500);
  121. sheet.setColumnWidth((short) 7, (short) 8500);
  122. sheet.setColumnWidth((short) 8, (short) 8500);
  123. }
  124. /**
  125. * 2022-07-14 17:42:03
  126. * 设置表头
  127. * cellStyle1 中文提示信息样式
  128. * cellStyle2 标题提示信息样式
  129. * cellStyle3 正文提示信息样式
  130. **/
  131. public static void batchDetail(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFCellStyle cellStyle3, XSSFCellStyle cellStyle4, XSSFWorkbook workbook) {
  132. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  133. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  134. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  135. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  136. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  137. // bcs.setWrapText(true);
  138. XSSFRow row = null;
  139. XSSFCell cell = null;
  140. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  141. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  142. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  143. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  144. cellStyle1.setWrapText(true);
  145. cellStyle2.setBorderBottom(BorderStyle.THIN); //下边框
  146. cellStyle2.setBorderLeft(BorderStyle.THIN);//左边框
  147. cellStyle2.setBorderTop(BorderStyle.THIN);//上边框
  148. cellStyle2.setBorderRight(BorderStyle.THIN);//右边框
  149. cellStyle2.setWrapText(true);
  150. /*第一行*/
  151. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
  152. /*第一行塞值*/
  153. row = sheet.createRow(0);
  154. cell = row.createCell(0);// ID
  155. cell.setCellStyle(cellStyle4);
  156. cell.setCellValue("商品调价单导入");
  157. /*第二行*/
  158. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
  159. /*第二行塞值*/
  160. row = sheet.createRow(1);
  161. cell = row.createCell(0);// ID
  162. cell.setCellStyle(cellStyle1);
  163. cell.setCellValue("注意:①请按照以下示例填写调价;②调价商品最多不超过5000行;③导入前,请记得删除示例行!");
  164. /*第三行塞值*/
  165. row = sheet.createRow(2);
  166. cell = row.createCell(0);
  167. cell.setCellStyle(cellStyle2);
  168. cell.setCellValue("商品编码(必填)");
  169. cell = row.createCell(1);
  170. cell.setCellStyle(cellStyle2);
  171. cell.setCellValue("备注(非必填)");
  172. cell = row.createCell(2);
  173. cell.setCellStyle(cellStyle2);
  174. cell.setCellValue("一级调整价(必填,价格不变时填原价)");
  175. cell = row.createCell(3);
  176. cell.setCellStyle(cellStyle2);
  177. cell.setCellValue("二级调整价(必填,价格不变时填原价)");
  178. cell = row.createCell(4);
  179. cell.setCellStyle(cellStyle2);
  180. cell.setCellValue("三级调整价(必填,价格不变时填原价)");
  181. cell = row.createCell(5);
  182. cell.setCellStyle(cellStyle2);
  183. cell.setCellValue("四级调整价(必填,价格不变时填原价)");
  184. cell = row.createCell(6);
  185. cell.setCellStyle(cellStyle2);
  186. cell.setCellValue("五级调整价(必填,价格不变时填原价)");
  187. cell = row.createCell(7);
  188. cell.setCellStyle(cellStyle2);
  189. cell.setCellValue("亲民价(非必填)");
  190. /*第四行塞值*/
  191. row = sheet.createRow(3);
  192. cell = row.createCell(0);
  193. cell.setCellStyle(cellStyle3);
  194. cell.setCellValue("NBNTO-28571530003");
  195. cell = row.createCell(1);
  196. cell.setCellStyle(cellStyle3);
  197. cell.setCellValue("备注");
  198. cell = row.createCell(2);
  199. cell.setCellStyle(cellStyle3);
  200. cell.setCellValue("100");
  201. cell = row.createCell(3);
  202. cell.setCellStyle(cellStyle3);
  203. cell.setCellValue("100");
  204. cell = row.createCell(4);
  205. cell.setCellStyle(cellStyle3);
  206. cell.setCellValue("100");
  207. cell = row.createCell(5);
  208. cell.setCellStyle(cellStyle3);
  209. cell.setCellValue("100");
  210. cell = row.createCell(6);
  211. cell.setCellStyle(cellStyle3);
  212. cell.setCellValue("100");
  213. cell = row.createCell(7);
  214. cell.setCellStyle(cellStyle3);
  215. cell.setCellValue("100");
  216. }
  217. /**
  218. * 2022-07-14 17:42:03
  219. * 设置表头
  220. **/
  221. public static void batchDetailErr(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFCellStyle cellStyle3, XSSFWorkbook workbook) {
  222. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  223. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  224. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  225. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  226. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  227. // bcs.setWrapText(true);
  228. XSSFRow row = null;
  229. XSSFCell cell = null;
  230. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  231. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  232. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  233. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  234. cellStyle1.setWrapText(true);
  235. /*第一行*/
  236. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
  237. /*第一行塞值*/
  238. row = sheet.createRow(0);
  239. cell = row.createCell(0);// ID
  240. cell.setCellStyle(cellStyle3);
  241. cell.setCellValue("商品调价单导入");
  242. /*第二行*/
  243. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  244. /*第二行塞值*/
  245. row = sheet.createRow(1);
  246. cell = row.createCell(0);// ID
  247. cell.setCellStyle(cellStyle1);
  248. cell.setCellValue("注意:①请按照以下示例填写调价;②调价商品最多不超过5000行;③调价单导入前,请记得删除示例行!");
  249. /*第三行塞值*/
  250. row = sheet.createRow(2);
  251. cell = row.createCell(0);
  252. cell.setCellStyle(cellStyle2);
  253. cell.setCellValue("商品编码(必填)");
  254. cell = row.createCell(1);
  255. cell.setCellStyle(cellStyle2);
  256. cell.setCellValue("备注(非必填)");
  257. cell = row.createCell(2);
  258. cell.setCellStyle(cellStyle2);
  259. cell.setCellValue("一级调整价(必填,价格不变时填原价)");
  260. cell = row.createCell(3);
  261. cell.setCellStyle(cellStyle2);
  262. cell.setCellValue("二级调整价(必填,价格不变时填原价)");
  263. cell = row.createCell(4);
  264. cell.setCellStyle(cellStyle2);
  265. cell.setCellValue("三级调整价(必填,价格不变时填原价)");
  266. cell = row.createCell(5);
  267. cell.setCellStyle(cellStyle2);
  268. cell.setCellValue("四级调整价(必填,价格不变时填原价)");
  269. cell = row.createCell(6);
  270. cell.setCellStyle(cellStyle2);
  271. cell.setCellValue("五级调整价(必填,价格不变时填原价)");
  272. cell = row.createCell(7);
  273. cell.setCellStyle(cellStyle2);
  274. cell.setCellValue("亲民价(非必填)");
  275. cell = row.createCell(8);
  276. cell.setCellStyle(cellStyle2);
  277. cell.setCellValue("错误信息");
  278. }
  279. }