Java POI組件實現多個Excel文件整合成一個多Sheet的Excel文件

轉自:https://blog.csdn.net/linhaiyun_ytdx/article/details/82952769

代碼:

  1. package com.weichai;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.util.Iterator;
  7. import org.apache.poi.ss.usermodel.DateUtil;
  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.XSSFRow;
  12. import org.apache.poi.xssf.usermodel.XSSFSheet;
  13. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  14. public class Test {
  15. public static void main(String[] args) throws Exception {
  16. // TODO Auto-generated method stub
  17. // 將所有類型的盡調excel文件合併成一個excel文件
  18. String Path = "D:\\ExcelTest";
  19. File file = new File(Path);
  20. File[] tempList = file.listFiles();
  21. String TmpList [] = new String [2];
  22. System.out.println("該目錄下對象個數:" + tempList.length);
  23. for (int i = 0; i < tempList.length; i++) {
  24. if (tempList[i].isFile()) {
  25. TmpList[i] = tempList[i].toString();
  26. System.out.println("文件:"+TmpList[i]+" 待處理");
  27. }
  28. }
  29. XSSFWorkbook newExcelCreat = new XSSFWorkbook();
  30. for (String fromExcelName : TmpList) { // 遍歷每個源excel文件,TmpList爲源文件的名稱集合
  31. InputStream in = new FileInputStream(fromExcelName);
  32. XSSFWorkbook fromExcel = new XSSFWorkbook(in);
  33. int length = fromExcel.getNumberOfSheets();
  34. if(length<=1){ //長度爲1時
  35. XSSFSheet oldSheet = fromExcel.getSheetAt(0);
  36. XSSFSheet newSheet = newExcelCreat.createSheet(oldSheet.getSheetName());
  37. copySheet(newExcelCreat, oldSheet, newSheet);
  38. }else{
  39. for (int i = 0; i < length; i++) {// 遍歷每個sheet
  40. XSSFSheet oldSheet = fromExcel.getSheetAt(i);
  41. XSSFSheet newSheet = newExcelCreat.createSheet(oldSheet.getSheetName());
  42. copySheet(newExcelCreat, oldSheet, newSheet);
  43. }
  44. }
  45. }
  46. String allFileName = Path+ "\\New.xlsx"; //定義新生成的xlx表格文件
  47. FileOutputStream fileOut = new FileOutputStream(allFileName);
  48. newExcelCreat.write(fileOut);
  49. fileOut.flush();
  50. fileOut.close();
  51. // // 刪除各個源文件
  52. // for (String fromExcelName : TmpList) {// 遍歷每個源excel文件
  53. // File Existfile = new File(fromExcelName);
  54. // if (Existfile.exists()) {
  55. // Existfile.delete();
  56. // }
  57. // }
  58. System.out.println("運行結束!");
  59. }
  60. public static void copyCellStyle(XSSFCellStyle fromStyle, XSSFCellStyle toStyle) {
  61. toStyle.cloneStyleFrom(fromStyle);// 此一行代碼搞定
  62. // 下面統統不用
  63. /*
  64. * //對齊方式 toStyle.setAlignment(fromStyle.getAlignment()); //邊框和邊框顏色
  65. * toStyle.setBorderBottom(fromStyle.getBorderBottom());
  66. * toStyle.setBorderLeft(fromStyle.getBorderLeft());
  67. * toStyle.setBorderRight(fromStyle.getBorderRight());
  68. * toStyle.setBorderTop(fromStyle.getBorderTop());
  69. * toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
  70. * toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
  71. * toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
  72. * toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); //背景和前景
  73. * //toStyle.setFillPattern(fromStyle.getFillPattern());
  74. * //填充圖案,不起作用,轉爲黑色
  75. * toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
  76. * //不起作用
  77. * toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());
  78. * toStyle.setDataFormat(fromStyle.getDataFormat()); //數據格式
  79. * //toStyle.setFont(fromStyle.getFont()); //不起作用
  80. * toStyle.setHidden(fromStyle.getHidden());
  81. * toStyle.setIndention(fromStyle.getIndention());//首行縮進
  82. * toStyle.setLocked(fromStyle.getLocked());
  83. * toStyle.setRotation(fromStyle.getRotation());//旋轉
  84. * toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
  85. * //垂直對齊 toStyle.setWrapText(fromStyle.getWrapText()); //文本換行
  86. */
  87. }
  88. /**
  89. * 合併單元格
  90. * @param fromSheet
  91. * @param toSheet
  92. */
  93. public static void mergeSheetAllRegion(XSSFSheet fromSheet, XSSFSheet toSheet) {
  94. int num = fromSheet.getNumMergedRegions();
  95. CellRangeAddress cellR = null;
  96. for (int i = 0; i < num; i++) {
  97. cellR = fromSheet.getMergedRegion(i);
  98. toSheet.addMergedRegion(cellR);
  99. }
  100. }
  101. /**
  102. * 複製單元格
  103. * @param wb
  104. * @param fromCell
  105. * @param toCell
  106. */
  107. public static void copyCell(XSSFWorkbook wb, XSSFCell fromCell, XSSFCell toCell) {
  108. XSSFCellStyle newstyle = wb.createCellStyle();
  109. copyCellStyle(fromCell.getCellStyle(), newstyle);
  110. // toCell.setEncoding(fromCell.getStringCelllValue());
  111. // 樣式
  112. toCell.setCellStyle(newstyle);
  113. if (fromCell.getCellComment() != null) {
  114. toCell.setCellComment(fromCell.getCellComment());
  115. }
  116. // 不同數據類型處理
  117. int fromCellType = fromCell.getCellType();
  118. toCell.setCellType(fromCellType);
  119. if (fromCellType == XSSFCell.CELL_TYPE_NUMERIC) {
  120. if (XSSFDateUtil.isCellDateFormatted(fromCell)) {
  121. toCell.setCellValue(fromCell.getDateCellValue());
  122. } else {
  123. toCell.setCellValue(fromCell.getNumericCellValue());
  124. }
  125. } else if (fromCellType == XSSFCell.CELL_TYPE_STRING) {
  126. toCell.setCellValue(fromCell.getRichStringCellValue());
  127. } else if (fromCellType == XSSFCell.CELL_TYPE_BLANK) {
  128. // nothing21
  129. } else if (fromCellType == XSSFCell.CELL_TYPE_BOOLEAN) {
  130. toCell.setCellValue(fromCell.getBooleanCellValue());
  131. } else if (fromCellType == XSSFCell.CELL_TYPE_ERROR) {
  132. toCell.setCellErrorValue(fromCell.getErrorCellValue());
  133. } else if (fromCellType == XSSFCell.CELL_TYPE_FORMULA) {
  134. toCell.setCellFormula(fromCell.getCellFormula());
  135. } else { // nothing29
  136. }
  137. }
  138. /**
  139. * 行復制功能
  140. * @param wb
  141. * @param oldRow
  142. * @param toRow
  143. */
  144. public static void copyRow(XSSFWorkbook wb, XSSFRow oldRow, XSSFRow toRow) {
  145. toRow.setHeight(oldRow.getHeight());
  146. for (Iterator cellIt = oldRow.cellIterator(); cellIt.hasNext();) {
  147. XSSFCell tmpCell = (XSSFCell) cellIt.next();
  148. XSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex());
  149. copyCell(wb, tmpCell, newCell);
  150. }
  151. }
  152. /**
  153. * Sheet複製
  154. * @param wb
  155. * @param fromSheet
  156. * @param toSheet
  157. */
  158. public static void copySheet(XSSFWorkbook wb, XSSFSheet fromSheet, XSSFSheet toSheet) {
  159. mergeSheetAllRegion(fromSheet, toSheet);
  160. // 設置列寬
  161. int length = fromSheet.getRow(fromSheet.getFirstRowNum()).getLastCellNum();
  162. for (int i = 0; i <= length; i++) {
  163. toSheet.setColumnWidth(i, fromSheet.getColumnWidth(i));
  164. }
  165. for (Iterator rowIt = fromSheet.rowIterator(); rowIt.hasNext();) {
  166. XSSFRow oldRow = (XSSFRow) rowIt.next();
  167. XSSFRow newRow = toSheet.createRow(oldRow.getRowNum());
  168. copyRow(wb, oldRow, newRow);
  169. }
  170. }
  171. public class XSSFDateUtil extends DateUtil {
  172. }
  173. }

運行截圖:

將Student1和Student2的數據整合到一個Excel表單中

生成的New.xlsx文件如下圖所示:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章