IncomeExportExcel.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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) 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) 11000);// 设置 备注 宽度
  118. sheet.setColumnWidth((short) 6, (short) 8000);// 设置 错误信息 宽度
  119. }
  120. /**
  121. * 2022-07-14 17:42:03
  122. * 设置表头
  123. * cellStyle1 中文提示信息样式
  124. * cellStyle2 标题提示信息样式
  125. * cellStyle3 正文提示信息样式
  126. * **/
  127. public static void batchDetail(XSSFSheet sheet, XSSFCellStyle cellStyle1, XSSFCellStyle cellStyle2, XSSFCellStyle cellStyle3, XSSFCellStyle cellStyle4,XSSFWorkbook workbook) {
  128. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  129. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  130. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  131. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  132. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  133. // bcs.setWrapText(true);
  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, 5));
  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, 5));
  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. /*第四行塞值*/
  181. row = sheet.createRow(3);
  182. cell = row.createCell(0);
  183. cell.setCellStyle(cellStyle2);
  184. cell.setCellValue("1111");
  185. cell = row.createCell(1);
  186. cell.setCellStyle(cellStyle2);
  187. cell.setCellValue("现金账户");
  188. cell = row.createCell(2);
  189. cell.setCellStyle(cellStyle2);
  190. cell.setCellValue("100");
  191. cell = row.createCell(3);
  192. cell.setCellStyle(cellStyle2);
  193. cell.setCellValue("货款");
  194. cell = row.createCell(4);
  195. cell.setCellStyle(cellStyle2);
  196. cell.setCellValue("分类明细");
  197. cell = row.createCell(5);
  198. cell.setCellStyle(cellStyle2);
  199. cell.setCellValue("备注");
  200. }
  201. /**
  202. * 2022-07-14 17:42:03
  203. * 设置表头
  204. * **/
  205. public static void batchDetailErr(XSSFSheet sheet, XSSFCellStyle cellStyle1,XSSFCellStyle cellStyle2,XSSFCellStyle cellStyle3,XSSFWorkbook workbook) {
  206. // HSSFCellStyle bcs = ExportExcel.createTitleCellStyle1(workbook);
  207. // bcs.setBorderBottom(BorderStyle.THIN); //下边框
  208. // bcs.setBorderLeft(BorderStyle.THIN);//左边框
  209. // bcs.setBorderTop(BorderStyle.THIN);//上边框
  210. // bcs.setBorderRight(BorderStyle.THIN);//右边框
  211. // bcs.setWrapText(true);
  212. XSSFRow row = null;
  213. XSSFCell cell = null;
  214. cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
  215. cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
  216. cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
  217. cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
  218. cellStyle1.setWrapText(true);
  219. /*第一行*/
  220. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
  221. /*第一行塞值*/
  222. row = sheet.createRow(0);
  223. cell = row.createCell(0);// ID
  224. cell.setCellStyle(cellStyle3);
  225. cell.setCellValue("收入凭证导入");
  226. /*第二行*/
  227. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
  228. /*第二行塞值*/
  229. row = sheet.createRow(1);
  230. cell = row.createCell(0);// ID
  231. cell.setCellStyle(cellStyle1);
  232. cell.setCellValue("注意:①请按照以下示例填写收入凭证;②收入凭证最多不超过5000行;③收入凭证导入前,请记得删除示例行!");
  233. /*第三行塞值*/
  234. row = sheet.createRow(2);
  235. cell = row.createCell(0);
  236. cell.setCellStyle(cellStyle2);
  237. cell.setCellValue("经销商编码(必填)");
  238. cell = row.createCell(1);
  239. cell.setCellStyle(cellStyle2);
  240. cell.setCellValue("账户名称(必填)");
  241. cell = row.createCell(2);
  242. cell.setCellStyle(cellStyle2);
  243. cell.setCellValue(" 收入金额(必填)");
  244. cell = row.createCell(3);
  245. cell.setCellStyle(cellStyle2);
  246. cell.setCellValue("分类(必填)");
  247. cell = row.createCell(4);
  248. cell.setCellStyle(cellStyle2);
  249. cell.setCellValue(" 分类明细");
  250. cell = row.createCell(5);
  251. cell.setCellStyle(cellStyle2);
  252. cell.setCellValue("备注");
  253. cell = row.createCell(6);
  254. cell.setCellStyle(cellStyle2);
  255. cell.setCellValue("错误信息");
  256. }
  257. }