IncomeExportExcel.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package restcontroller.sale.cashbill;
  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 IncomeExportExcel {
  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) 3, (short) 11000);// 设置 备注 宽度
  106. }
  107. /**
  108. * 2022-07-14 17:41:39
  109. * 设置表格宽度(返回错误Excel的样式)
  110. * **/
  111. public static void setBatchDetailSheetColumn2(XSSFSheet sheet) {
  112. sheet.setDefaultRowHeight((short) 600);
  113. sheet.setColumnWidth((short) 0, (short) 5500);// 设置 经销商编码 宽度
  114. sheet.setColumnWidth((short) 1, (short) 5500);// 设置 账户名称 宽度
  115. sheet.setColumnWidth((short) 2, (short) 5500);// 设置 金额 宽度
  116. sheet.setColumnWidth((short) 3, (short) 5500);// 设置 分类 宽度
  117. sheet.setColumnWidth((short) 4, (short) 5500);// 设置 分类明细 宽度
  118. sheet.setColumnWidth((short) 5, (short) 5500);// 设置 归属日期 宽度
  119. sheet.setColumnWidth((short) 6, (short) 11000);// 设置 备注 宽度
  120. sheet.setColumnWidth((short) 7, (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. /*第一行*/
  149. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
  150. /*第一行塞值*/
  151. row = sheet.createRow(0);
  152. cell = row.createCell(0);// ID
  153. cell.setCellStyle(cellStyle4);
  154. cell.setCellValue("收入凭证导入");
  155. /*第二行*/
  156. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
  157. /*第二行塞值*/
  158. row = sheet.createRow(1);
  159. cell = row.createCell(0);// ID
  160. cell.setCellStyle(cellStyle1);
  161. cell.setCellValue("注意:①请按照以下示例填写收入凭证;②收入凭证最多不超过5000行;③收入凭证导入前,请记得删除示例行!");
  162. /*第三行塞值*/
  163. row = sheet.createRow(2);
  164. cell = row.createCell(0);
  165. cell.setCellStyle(cellStyle2);
  166. cell.setCellValue("经销商编码(必填)");
  167. cell = row.createCell(1);
  168. cell.setCellStyle(cellStyle2);
  169. cell.setCellValue("账户名称(必填)");
  170. cell = row.createCell(2);
  171. cell.setCellStyle(cellStyle2);
  172. cell.setCellValue(" 收入金额(必填)");
  173. cell = row.createCell(3);
  174. cell.setCellStyle(cellStyle2);
  175. cell.setCellValue("分类(必填)");
  176. cell = row.createCell(4);
  177. cell.setCellStyle(cellStyle2);
  178. cell.setCellValue(" 分类明细");
  179. cell = row.createCell(5);
  180. cell.setCellStyle(cellStyle2);
  181. cell.setCellValue(" 归属日期");
  182. cell = row.createCell(6);
  183. cell.setCellStyle(cellStyle2);
  184. cell.setCellValue("备注");
  185. /*第四行塞值*/
  186. row = sheet.createRow(3);
  187. cell = row.createCell(0);
  188. cell.setCellStyle(cellStyle3);
  189. cell.setCellValue("1111");
  190. cell = row.createCell(1);
  191. cell.setCellStyle(cellStyle3);
  192. cell.setCellValue("现金账户");
  193. cell = row.createCell(2);
  194. cell.setCellStyle(cellStyle3);
  195. cell.setCellValue("100");
  196. cell = row.createCell(3);
  197. cell.setCellStyle(cellStyle3);
  198. cell.setCellValue("货款");
  199. cell = row.createCell(4);
  200. cell.setCellStyle(cellStyle3);
  201. cell.setCellValue("分类明细");
  202. cell = row.createCell(5);
  203. cell.setCellStyle(cellStyle3);
  204. cell.setCellValue(" 归属日期");
  205. cell = row.createCell(6);
  206. cell.setCellStyle(cellStyle3);
  207. cell.setCellValue("备注");
  208. }
  209. /**
  210. * 2022-07-14 17:42:03
  211. * 设置表头
  212. * **/
  213. public static void batchDetailErr(XSSFSheet sheet, XSSFCellStyle cellStyle1,XSSFCellStyle cellStyle2,XSSFCellStyle cellStyle3,XSSFWorkbook workbook) {
  214. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  215. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  216. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  217. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  218. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  219. // bcs.setWrapText(true);
  220. XSSFRow row = null;
  221. XSSFCell cell = null;
  222. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  223. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  224. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  225. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  226. cellStyle1.setWrapText(true);
  227. /*第一行*/
  228. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
  229. /*第一行塞值*/
  230. row = sheet.createRow(0);
  231. cell = row.createCell(0);// ID
  232. cell.setCellStyle(cellStyle3);
  233. cell.setCellValue("收入凭证导入");
  234. /*第二行*/
  235. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
  236. /*第二行塞值*/
  237. row = sheet.createRow(1);
  238. cell = row.createCell(0);// ID
  239. cell.setCellStyle(cellStyle1);
  240. cell.setCellValue("注意:①请按照以下示例填写收入凭证;②收入凭证最多不超过5000行;③收入凭证导入前,请记得删除示例行!");
  241. /*第三行塞值*/
  242. row = sheet.createRow(2);
  243. cell = row.createCell(0);
  244. cell.setCellStyle(cellStyle2);
  245. cell.setCellValue("经销商编码(必填)");
  246. cell = row.createCell(1);
  247. cell.setCellStyle(cellStyle2);
  248. cell.setCellValue("账户名称(必填)");
  249. cell = row.createCell(2);
  250. cell.setCellStyle(cellStyle2);
  251. cell.setCellValue(" 收入金额(必填)");
  252. cell = row.createCell(3);
  253. cell.setCellStyle(cellStyle2);
  254. cell.setCellValue("分类(必填)");
  255. cell = row.createCell(4);
  256. cell.setCellStyle(cellStyle2);
  257. cell.setCellValue(" 分类明细");
  258. cell = row.createCell(5);
  259. cell.setCellStyle(cellStyle2);
  260. cell.setCellValue(" 归属日期");
  261. cell = row.createCell(6);
  262. cell.setCellStyle(cellStyle2);
  263. cell.setCellValue("备注");
  264. cell = row.createCell(7);
  265. cell.setCellStyle(cellStyle2);
  266. cell.setCellValue("错误信息");
  267. }
  268. }