ExportExcel.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package restcontroller.webmanage.sale.rebatescheme;
  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. }
  101. /**
  102. * 2022-07-14 17:41:39
  103. * 设置表格宽度(返回错误Excel的样式)
  104. **/
  105. public static void setBatchDetailSheetColumn2(XSSFSheet sheet) {
  106. sheet.setDefaultRowHeight((short) 600);
  107. sheet.setColumnWidth((short) 0, (short) 8500);
  108. sheet.setColumnWidth((short) 1, (short) 8500);
  109. }
  110. /**
  111. * 2022-07-14 17:42:03
  112. * 设置表头
  113. * cellStyle1 中文提示信息样式
  114. * cellStyle2 标题提示信息样式
  115. * cellStyle3 正文提示信息样式
  116. **/
  117. public static void batchDetail(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFCellStyle cellStyle3, XSSFCellStyle cellStyle4, XSSFWorkbook workbook) {
  118. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  119. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  120. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  121. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  122. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  123. // bcs.setWrapText(true);
  124. XSSFRow row = null;
  125. XSSFCell cell = null;
  126. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  127. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  128. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  129. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  130. cellStyle1.setWrapText(true);
  131. cellStyle2.setBorderBottom(BorderStyle.THIN); //下边框
  132. cellStyle2.setBorderLeft(BorderStyle.THIN);//左边框
  133. cellStyle2.setBorderTop(BorderStyle.THIN);//上边框
  134. cellStyle2.setBorderRight(BorderStyle.THIN);//右边框
  135. cellStyle2.setWrapText(true);
  136. /*第一行*/
  137. //sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 0));
  138. /*第一行塞值*/
  139. row = sheet.createRow(0);
  140. cell = row.createCell(0);// ID
  141. cell.setCellStyle(cellStyle4);
  142. cell.setCellValue("不返利商品导入");
  143. /*第二行*/
  144. //sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 0));
  145. /*第二行塞值*/
  146. row = sheet.createRow(1);
  147. cell = row.createCell(0);// ID
  148. cell.setCellStyle(cellStyle1);
  149. cell.setCellValue("注意:①请按照以下示例填写商品;②调价商品最多不超过5000行;③导入前,请记得删除示例行!");
  150. /*第三行塞值*/
  151. row = sheet.createRow(2);
  152. cell = row.createCell(0);
  153. cell.setCellStyle(cellStyle2);
  154. cell.setCellValue("商品编码(必填)");
  155. /*第四行塞值*/
  156. row = sheet.createRow(3);
  157. cell = row.createCell(0);
  158. cell.setCellStyle(cellStyle3);
  159. cell.setCellValue("NBNTO-28571530003");
  160. }
  161. /**
  162. * 2022-07-14 17:42:03
  163. * 设置表头
  164. **/
  165. public static void batchDetailErr(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFCellStyle cellStyle3, XSSFWorkbook workbook) {
  166. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  167. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  168. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  169. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  170. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  171. // bcs.setWrapText(true);
  172. XSSFRow row = null;
  173. XSSFCell cell = null;
  174. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  175. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  176. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  177. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  178. cellStyle1.setWrapText(true);
  179. /*第一行*/
  180. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
  181. /*第一行塞值*/
  182. row = sheet.createRow(0);
  183. cell = row.createCell(0);// ID
  184. cell.setCellStyle(cellStyle3);
  185. cell.setCellValue("不返利商品导入");
  186. /*第二行*/
  187. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1));
  188. /*第二行塞值*/
  189. row = sheet.createRow(1);
  190. cell = row.createCell(0);// ID
  191. cell.setCellStyle(cellStyle1);
  192. cell.setCellValue("注意:①请按照以下示例填写商品;②调价商品最多不超过5000行;③调价单导入前,请记得删除示例行!");
  193. /*第三行塞值*/
  194. row = sheet.createRow(2);
  195. cell = row.createCell(0);
  196. cell.setCellStyle(cellStyle2);
  197. cell.setCellValue("商品编码(必填)");
  198. cell = row.createCell(1);
  199. cell.setCellStyle(cellStyle2);
  200. cell.setCellValue("错误信息");
  201. }
  202. }