ExportExcel.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package restcontroller.webmanage.sale.promotion;
  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) 5500);
  100. sheet.setColumnWidth((short) 1, (short) 5500);
  101. sheet.setColumnWidth((short) 2, (short) 5500);
  102. sheet.setColumnWidth((short) 3, (short) 5500);
  103. sheet.setColumnWidth((short) 4, (short) 5500);
  104. sheet.setColumnWidth((short) 5, (short) 5500);
  105. sheet.setColumnWidth((short) 6, (short) 5500);
  106. sheet.setColumnWidth((short) 7, (short) 5500);
  107. sheet.setColumnWidth((short) 8, (short) 5500);
  108. }
  109. /**
  110. * 2022-07-14 17:41:39
  111. * 设置表格宽度(返回错误Excel的样式)
  112. **/
  113. public static void setBatchDetailSheetColumn2(XSSFSheet sheet) {
  114. sheet.setDefaultRowHeight((short) 600);
  115. sheet.setColumnWidth((short) 0, (short) 5500);
  116. sheet.setColumnWidth((short) 1, (short) 5500);
  117. sheet.setColumnWidth((short) 2, (short) 5500);
  118. sheet.setColumnWidth((short) 3, (short) 5500);
  119. sheet.setColumnWidth((short) 4, (short) 5500);
  120. sheet.setColumnWidth((short) 5, (short) 5500);
  121. sheet.setColumnWidth((short) 6, (short) 5500);
  122. sheet.setColumnWidth((short) 7, (short) 5500);
  123. sheet.setColumnWidth((short) 8, (short) 5500);
  124. sheet.setColumnWidth((short) 9, (short) 5500);
  125. }
  126. /**
  127. * 2022-07-14 17:42:03
  128. * 设置表头
  129. * cellStyle1 中文提示信息样式
  130. * cellStyle2 标题提示信息样式
  131. * cellStyle3 正文提示信息样式
  132. **/
  133. public static void batchDetail(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFCellStyle cellStyle3, XSSFCellStyle cellStyle4, XSSFWorkbook workbook) {
  134. XSSFRow row = null;
  135. XSSFCell cell = null;
  136. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  137. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  138. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  139. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  140. cellStyle1.setWrapText(true);
  141. cellStyle2.setBorderBottom(BorderStyle.THIN); //下边框
  142. cellStyle2.setBorderLeft(BorderStyle.THIN);//左边框
  143. cellStyle2.setBorderTop(BorderStyle.THIN);//上边框
  144. cellStyle2.setBorderRight(BorderStyle.THIN);//右边框
  145. cellStyle2.setWrapText(true);
  146. /*第一行*/
  147. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9));
  148. /*第一行塞值*/
  149. row = sheet.createRow(0);
  150. cell = row.createCell(0);// ID
  151. cell.setCellStyle(cellStyle4);
  152. cell.setCellValue("促销商品导入");
  153. /*第二行*/
  154. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 9));
  155. /*第二行塞值*/
  156. row = sheet.createRow(1);
  157. cell = row.createCell(0);// ID
  158. cell.setCellStyle(cellStyle1);
  159. cell.setCellValue("注意:①请按照以下示例填写订单;②订单商品最多不超过5000行;③订单导入前,请记得删除示例行!");
  160. /*第三行塞值*/
  161. row = sheet.createRow(2);
  162. cell = row.createCell(0);
  163. cell.setCellStyle(cellStyle2);
  164. cell.setCellValue("产品编码(必填)");
  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. cell = row.createCell(4);
  175. cell.setCellStyle(cellStyle2);
  176. cell.setCellValue("价格一活动价");
  177. cell = row.createCell(5);
  178. cell.setCellStyle(cellStyle2);
  179. cell.setCellValue("价格二活动价");
  180. cell = row.createCell(6);
  181. cell.setCellStyle(cellStyle2);
  182. cell.setCellValue("价格三活动价");
  183. cell = row.createCell(7);
  184. cell.setCellStyle(cellStyle2);
  185. cell.setCellValue("价格四活动价");
  186. cell = row.createCell(8);
  187. cell.setCellStyle(cellStyle2);
  188. cell.setCellValue("价格五活动价");
  189. /*第四行塞值*/
  190. row = sheet.createRow(3);
  191. cell = row.createCell(0);
  192. cell.setCellStyle(cellStyle3);
  193. cell.setCellValue("12345");
  194. cell = row.createCell(1);
  195. cell.setCellStyle(cellStyle3);
  196. cell.setCellValue("0");
  197. cell = row.createCell(2);
  198. cell.setCellStyle(cellStyle3);
  199. cell.setCellValue("0");
  200. cell = row.createCell(3);
  201. cell.setCellStyle(cellStyle3);
  202. cell.setCellValue("0");
  203. cell = row.createCell(4);
  204. cell.setCellStyle(cellStyle3);
  205. cell.setCellValue("0");
  206. cell = row.createCell(5);
  207. cell.setCellStyle(cellStyle3);
  208. cell.setCellValue("0");
  209. cell = row.createCell(6);
  210. cell.setCellStyle(cellStyle3);
  211. cell.setCellValue("0");
  212. cell = row.createCell(7);
  213. cell.setCellStyle(cellStyle3);
  214. cell.setCellValue("0");
  215. cell = row.createCell(8);
  216. cell.setCellStyle(cellStyle3);
  217. cell.setCellValue("0");
  218. }
  219. /**
  220. * 2022-07-14 17:42:03
  221. * 设置表头
  222. **/
  223. public static void batchDetailErr(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFCellStyle cellStyle3, XSSFWorkbook workbook) {
  224. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  225. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  226. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  227. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  228. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  229. // bcs.setWrapText(true);
  230. XSSFRow row = null;
  231. XSSFCell cell = null;
  232. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  233. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  234. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  235. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  236. cellStyle1.setWrapText(true);
  237. /*第一行*/
  238. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9));
  239. /*第一行塞值*/
  240. row = sheet.createRow(0);
  241. cell = row.createCell(0);// ID
  242. cell.setCellStyle(cellStyle3);
  243. cell.setCellValue("促销商品导入");
  244. /*第二行*/
  245. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 9));
  246. /*第二行塞值*/
  247. row = sheet.createRow(1);
  248. cell = row.createCell(0);// ID
  249. cell.setCellStyle(cellStyle1);
  250. cell.setCellValue("注意:①请按照以下示例填写订单;②订单商品最多不超过5000行;③订单导入前,请记得删除示例行!");
  251. /*第三行塞值*/
  252. row = sheet.createRow(2);
  253. cell = row.createCell(0);
  254. cell.setCellStyle(cellStyle2);
  255. cell.setCellValue("产品编码(必填)");
  256. cell = row.createCell(1);
  257. cell.setCellStyle(cellStyle2);
  258. cell.setCellValue("起订量");
  259. cell = row.createCell(2);
  260. cell.setCellStyle(cellStyle2);
  261. cell.setCellValue("增量");
  262. cell = row.createCell(3);
  263. cell.setCellStyle(cellStyle2);
  264. cell.setCellValue("限购数");
  265. cell = row.createCell(4);
  266. cell.setCellStyle(cellStyle2);
  267. cell.setCellValue("价格一活动价");
  268. cell = row.createCell(5);
  269. cell.setCellStyle(cellStyle2);
  270. cell.setCellValue("价格二活动价");
  271. cell = row.createCell(6);
  272. cell.setCellStyle(cellStyle2);
  273. cell.setCellValue("价格三活动价");
  274. cell = row.createCell(7);
  275. cell.setCellStyle(cellStyle2);
  276. cell.setCellValue("价格四活动价");
  277. cell = row.createCell(8);
  278. cell.setCellStyle(cellStyle2);
  279. cell.setCellValue("价格五活动价");
  280. cell = row.createCell(9);
  281. cell.setCellStyle(cellStyle2);
  282. cell.setCellValue("错误信息");
  283. }
  284. }