PayExportExcel.java 12 KB

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