ExportExcel.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package restcontroller.webmanage.sale.order;
  2. import java.awt.Color;
  3. import org.apache.poi.hssf.util.HSSFColor;
  4. import org.apache.poi.ss.usermodel.BorderStyle;
  5. import org.apache.poi.ss.usermodel.FillPatternType;
  6. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  7. import org.apache.poi.ss.usermodel.IndexedColors;
  8. import org.apache.poi.ss.util.CellRangeAddress;
  9. import org.apache.poi.xssf.usermodel.XSSFCell;
  10. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  11. import org.apache.poi.xssf.usermodel.XSSFColor;
  12. import org.apache.poi.xssf.usermodel.XSSFFont;
  13. import org.apache.poi.xssf.usermodel.XSSFRow;
  14. import org.apache.poi.xssf.usermodel.XSSFSheet;
  15. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16. public class ExportExcel {
  17. /**
  18. * 设置标题单元样式
  19. *
  20. * @param workbook
  21. * @return
  22. */
  23. public static XSSFCellStyle createTitleCellStyle1(XSSFWorkbook workbook) {
  24. XSSFCellStyle cellStyle = workbook.createCellStyle();
  25. XSSFFont font = workbook.createFont();
  26. font.setFontHeightInPoints((short) 12);
  27. font.setColor(IndexedColors.RED.getIndex());
  28. font.setFontName("微软雅黑");// 设置标题字体
  29. cellStyle.setFont(font);
  30. cellStyle.setWrapText(true); //自动换行
  31. cellStyle = workbook.createCellStyle();
  32. cellStyle.setFont(font);// 设置列标题样式
  33. XSSFColor colorGrey = new XSSFColor(new Color(217, 225, 242));
  34. cellStyle.setFillForegroundColor(colorGrey);
  35. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  36. cellStyle.setAlignment(HorizontalAlignment.LEFT); // 居左
  37. return cellStyle;
  38. }
  39. /**
  40. * 设置中文提示信息样式
  41. *
  42. * @param workbook
  43. * @return
  44. */
  45. public static XSSFCellStyle createTitleCellStyle2(XSSFWorkbook workbook) {
  46. XSSFCellStyle cellStyle = workbook.createCellStyle();
  47. XSSFFont font = workbook.createFont();
  48. font.setFontHeightInPoints((short) 12);
  49. font.setColor(IndexedColors.WHITE.getIndex());
  50. font.setFontName("微软雅黑");// 设置标题字体
  51. cellStyle.setFont(font);
  52. cellStyle.setWrapText(true); //自动换行
  53. cellStyle = workbook.createCellStyle();
  54. cellStyle.setFont(font);// 设置列标题样式
  55. XSSFColor colorBlue = new XSSFColor(new Color(48, 84, 150));
  56. cellStyle.setFillForegroundColor(colorBlue);
  57. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  58. cellStyle.setAlignment(HorizontalAlignment.LEFT); // 居左
  59. return cellStyle;
  60. }
  61. /**
  62. * 设置中文提示信息样式
  63. *
  64. * @param workbook
  65. * @return
  66. */
  67. public static XSSFCellStyle createTitleCellStyle3(XSSFWorkbook workbook) {
  68. XSSFCellStyle cellStyle = workbook.createCellStyle();
  69. XSSFFont font = workbook.createFont();
  70. font.setFontHeightInPoints((short) 12);
  71. font.setColor(IndexedColors.WHITE.getIndex());
  72. font.setFontName("微软雅黑");// 设置标题字体
  73. cellStyle.setFont(font);
  74. cellStyle.setWrapText(true); //自动换行
  75. cellStyle = workbook.createCellStyle();
  76. cellStyle.setFont(font);// 设置列标题样式
  77. XSSFColor colorBlue = new XSSFColor(new Color(48, 84, 150));
  78. cellStyle.setFillForegroundColor(colorBlue);
  79. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  80. cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居左
  81. return cellStyle;
  82. }
  83. /**
  84. * 设置正文单元样式
  85. *
  86. * @param workbook
  87. * @return
  88. */
  89. public static XSSFCellStyle createBodyCellStyle(XSSFWorkbook workbook) {
  90. XSSFCellStyle cellStyle = workbook.createCellStyle();
  91. XSSFFont font = workbook.createFont();
  92. font.setFontHeightInPoints((short) 12);
  93. font.setFontName("微软雅黑");// 设置标题字体
  94. // font.setFontName(HSSFFont.FONT_ARIAL);// 设置标题字体
  95. cellStyle.setFont(font);
  96. cellStyle = workbook.createCellStyle();
  97. cellStyle.setAlignment(HorizontalAlignment.LEFT); // 居左
  98. return cellStyle;
  99. }
  100. /**
  101. * 2022-07-14 17:41:39
  102. * 设置表格宽度(导入模板)
  103. * **/
  104. public static void setBatchDetailSheetColumn1(XSSFSheet sheet) {
  105. sheet.setDefaultRowHeight((short) 600);
  106. sheet.setColumnWidth((short) 0, (short) 5500);// 设置 品号 宽度
  107. sheet.setColumnWidth((short) 1, (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. }
  119. /**
  120. * 2022-07-14 17:42:03
  121. * 设置表头
  122. * cellStyle1 中文提示信息样式
  123. * cellStyle2 标题提示信息样式
  124. * cellStyle3 正文提示信息样式
  125. * **/
  126. public static void batchDetail(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFCellStyle cellStyle3, XSSFCellStyle cellStyle4,XSSFWorkbook workbook) {
  127. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  128. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  129. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  130. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  131. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  132. // bcs.setWrapText(true);
  133. XSSFRow row = null;
  134. XSSFCell cell = null;
  135. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  136. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  137. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  138. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  139. cellStyle1.setWrapText(true);
  140. cellStyle2.setBorderBottom(BorderStyle.THIN); //下边框
  141. cellStyle2.setBorderLeft(BorderStyle.THIN);//左边框
  142. cellStyle2.setBorderTop(BorderStyle.THIN);//上边框
  143. cellStyle2.setBorderRight(BorderStyle.THIN);//右边框
  144. cellStyle2.setWrapText(true);
  145. /*第一行*/
  146. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
  147. /*第一行塞值*/
  148. row = sheet.createRow(0);
  149. cell = row.createCell(0);// ID
  150. cell.setCellStyle(cellStyle4);
  151. cell.setCellValue("订单导入");
  152. /*第二行*/
  153. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1));
  154. /*第二行塞值*/
  155. row = sheet.createRow(1);
  156. cell = row.createCell(0);// ID
  157. cell.setCellStyle(cellStyle1);
  158. cell.setCellValue("注意:①请按照以下示例填写订单;②订单商品最多不超过5000行;③订单导入前,请记得删除示例行!");
  159. /*第三行塞值*/
  160. row = sheet.createRow(2);
  161. cell = row.createCell(0);
  162. cell.setCellStyle(cellStyle2);
  163. cell.setCellValue(" 商品编号(必填)");
  164. cell = row.createCell(1);
  165. cell.setCellStyle(cellStyle2);
  166. cell.setCellValue("数量(必填)");
  167. /*第四行塞值*/
  168. row = sheet.createRow(3);
  169. cell = row.createCell(0);
  170. cell.setCellStyle(cellStyle3);
  171. cell.setCellValue("test123");
  172. cell = row.createCell(1);
  173. cell.setCellStyle(cellStyle3);
  174. cell.setCellValue("10");
  175. }
  176. /**
  177. * 2022-07-14 17:42:03
  178. * 设置表头
  179. * **/
  180. public static void batchDetailErr(XSSFSheet sheet, XSSFCellStyle cellStyle1,XSSFCellStyle cellStyle2,XSSFCellStyle cellStyle3,XSSFWorkbook workbook) {
  181. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  182. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  183. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  184. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  185. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  186. // bcs.setWrapText(true);
  187. XSSFRow row = null;
  188. XSSFCell cell = null;
  189. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  190. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  191. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  192. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  193. cellStyle1.setWrapText(true);
  194. /*第一行*/
  195. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
  196. /*第一行塞值*/
  197. row = sheet.createRow(0);
  198. cell = row.createCell(0);// ID
  199. cell.setCellStyle(cellStyle3);
  200. cell.setCellValue("订单导入");
  201. /*第二行*/
  202. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 2));
  203. /*第二行塞值*/
  204. row = sheet.createRow(1);
  205. cell = row.createCell(0);// ID
  206. cell.setCellStyle(cellStyle1);
  207. cell.setCellValue("注意:①请按照以下示例填写订单;②订单商品最多不超过5000行;③订单导入前,请记得删除示例行!");
  208. /*第三行塞值*/
  209. row = sheet.createRow(2);
  210. cell = row.createCell(0);
  211. cell.setCellStyle(cellStyle2);
  212. cell.setCellValue("商品编码(必填)");
  213. cell = row.createCell(1);
  214. cell.setCellStyle(cellStyle2);
  215. cell.setCellValue("数量");
  216. cell = row.createCell(2);
  217. cell.setCellStyle(cellStyle2);
  218. cell.setCellValue("错误信息");
  219. }
  220. }