FinStatisTest.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. *
  3. */
  4. package com.jeecg.demo.controller;
  5. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  6. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  7. import org.apache.poi.hssf.usermodel.HSSFSheet;
  8. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9. import org.apache.poi.ss.usermodel.*;
  10. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  11. import java.io.*;
  12. import java.text.SimpleDateFormat;
  13. import java.util.LinkedHashMap;
  14. /**
  15. * @author xzx
  16. *
  17. * 2019年2月26日
  18. *
  19. */
  20. public class FinStatisTest {
  21. private static final String EXCEL_XLS = "xls";
  22. private static final String EXCEL_XLSX = "xlsx";
  23. /**
  24. * 判断Excel的版本,获取Workbook
  25. * @param in
  26. * @param file
  27. * @return
  28. * @throws IOException
  29. */
  30. public static Workbook getWorkbok(InputStream in,File file) throws IOException{
  31. Workbook wb = null;
  32. if(file.getName().endsWith(EXCEL_XLS)){ //Excel 2003
  33. wb = new HSSFWorkbook(in);
  34. }else if(file.getName().endsWith(EXCEL_XLSX)){ // Excel 2007/2010
  35. wb = new XSSFWorkbook(in);
  36. }
  37. return wb;
  38. }
  39. /**
  40. * 判断文件是否是excel
  41. * @throws Exception
  42. */
  43. public static void checkExcelVaild(File file) throws Exception{
  44. if(!file.exists()){
  45. throw new Exception("文件不存在");
  46. }
  47. if(!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))){
  48. throw new Exception("文件不是Excel");
  49. }
  50. }
  51. private static Object getValue(Cell cell) {
  52. if(cell == null) {
  53. return "";
  54. }
  55. Object obj = null;
  56. switch (cell.getCellType()) {
  57. case BOOLEAN:
  58. obj = cell.getBooleanCellValue();
  59. break;
  60. case ERROR:
  61. obj = cell.getErrorCellValue();
  62. break;
  63. case NUMERIC:
  64. obj = cell.getNumericCellValue();
  65. break;
  66. case STRING:
  67. obj = cell.getStringCellValue();
  68. break;
  69. default:
  70. break;
  71. }
  72. return obj;
  73. }
  74. public static void main(String[] args) {
  75. LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
  76. map.put(4, "0101_基本工资");
  77. map.put(5, "0102_岗位津贴");
  78. map.put(6, "0401_高温费");
  79. map.put(7, "0104_证书津贴");
  80. map.put(8, "0103_项目补贴");
  81. map.put(9, "0105_社保津贴");
  82. map.put(10, "0107_其他补贴");
  83. map.put(11, "0107_独生子女费");
  84. map.put(12, "0203_加班费");
  85. map.put(13, "餐费_餐费");
  86. map.put(14, "0202_国定加班费");
  87. map.put(15, "0201_值班费");
  88. map.put(16, "0303_奖金");
  89. map.put(17, "0106_交通补贴");
  90. map.put(18, "0107_夜班津贴");
  91. map.put(19, "0107_白班津贴");
  92. map.put(20, "0203_签报");
  93. SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  94. try {
  95. // 同时支持Excel 2003、2007
  96. File srcFile = new File("d:/f_1812_2.xls"); // 创建文件对象
  97. FileInputStream in = new FileInputStream(srcFile); // 文件流
  98. checkExcelVaild(srcFile);
  99. Workbook srcWorkbook = getWorkbok(in,srcFile);
  100. //Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel2003/2007/2010都是可以处理的
  101. /**
  102. * 设置当前excel中sheet的下标:0开始
  103. */
  104. Sheet sheetSrc = srcWorkbook.getSheetAt(0); // 遍历Sheet
  105. HSSFWorkbook targetWb = new HSSFWorkbook();
  106. HSSFSheet sheetTarget = targetWb.createSheet("sheet1");
  107. FileOutputStream output=new FileOutputStream("d:/f_1812_targ3.xls");
  108. int rowNumSrc = sheetSrc.getLastRowNum();
  109. int targetRowNum = 0;
  110. for(int i=1 ; i<=rowNumSrc ; i++) {
  111. Row rowSrc = sheetSrc.getRow(i);
  112. int lastColNumSrc = rowSrc.getLastCellNum();
  113. for(Integer key : map.keySet()) {
  114. Cell cellSrc = rowSrc.getCell(key.intValue());
  115. double valueSrc = cellSrc.getNumericCellValue();
  116. if(valueSrc > 0d) {
  117. System.out.println(valueSrc);
  118. targetRowNum ++;
  119. Row newRow = sheetTarget.createRow(targetRowNum);
  120. //科目编码
  121. Cell cell0 = newRow.createCell(0);
  122. cell0.setCellType(CellType.STRING);
  123. HSSFCellStyle cellStyle = targetWb.createCellStyle();
  124. //创建一个DataFormat对象
  125. HSSFDataFormat format = targetWb.createDataFormat();
  126. //这样才能真正的控制单元格格式,@就是指文本型,具体格式的定义还是参考上面的原文吧
  127. cellStyle.setDataFormat(format.getFormat("@"));
  128. //具体如何创建cell就省略了,最后设置单元格的格式这样写
  129. cell0.setCellStyle(cellStyle);
  130. if(rowSrc.getCell(3) == null) {
  131. cell0.setCellValue("");
  132. }else {
  133. cell0.setCellValue(getValue(rowSrc.getCell(3))+"");
  134. }
  135. //借方金额
  136. Cell cell1 = newRow.createCell(1);
  137. cell1.setCellType(CellType.NUMERIC);
  138. cell1.setCellValue(valueSrc);
  139. //贷方金额
  140. Cell cell2 = newRow.createCell(2);
  141. cell2.setCellType(CellType.STRING);
  142. cell2.setCellValue("");
  143. //部门编码
  144. Cell cell3 = newRow.createCell(3);
  145. cell3.setCellType(CellType.STRING);
  146. if(rowSrc.getCell(1) == null) {
  147. cell3.setCellValue("");
  148. }else {
  149. cell3.setCellValue(getValue(rowSrc.getCell(1))+"");
  150. }
  151. //项目编码
  152. Cell cell4 = newRow.createCell(4);
  153. cell4.setCellType(CellType.STRING);
  154. cell4.setCellValue(map.get(key).split("_")[0]);
  155. //大类项目编码
  156. Cell cell5 = newRow.createCell(5);
  157. cell5.setCellType(CellType.STRING);
  158. cell5.setCellValue("00");
  159. //说明
  160. Cell cell6 = newRow.createCell(6);
  161. cell6.setCellType(CellType.STRING);
  162. cell6.setCellValue(getValue(rowSrc.getCell(0))
  163. +map.get(key).split("_")[1]
  164. +getValue(rowSrc.getCell(2)));
  165. }
  166. }
  167. }
  168. targetWb.write(output);
  169. output.flush();
  170. output.close();
  171. in.close();
  172. } catch (Exception e) {
  173. e.printStackTrace();
  174. }finally {
  175. }
  176. }
  177. }