|
1 | 1 | package com.xwintop.xJavaFxTool.services.littleTools; |
2 | 2 |
|
3 | 3 | import com.xwintop.xJavaFxTool.controller.littleTools.ExcelSplitToolController; |
| 4 | +import com.xwintop.xJavaFxTool.utils.DirectoryTreeUtil; |
4 | 5 | import lombok.Getter; |
5 | 6 | import lombok.Setter; |
6 | 7 | import lombok.extern.slf4j.Slf4j; |
|
9 | 10 | import org.apache.commons.csv.CSVParser; |
10 | 11 | import org.apache.commons.csv.CSVPrinter; |
11 | 12 | import org.apache.commons.csv.CSVRecord; |
| 13 | +import org.apache.commons.io.FileUtils; |
| 14 | +import org.apache.commons.io.LineIterator; |
12 | 15 | import org.apache.commons.lang3.StringUtils; |
13 | 16 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
14 | 17 | import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
@@ -111,8 +114,25 @@ public void splitCsv() throws Exception { |
111 | 114 | fileReader.close(); |
112 | 115 | } |
113 | 116 |
|
114 | | - public void splitFile() { |
115 | | - |
| 117 | + public void splitFile() throws Exception { |
| 118 | + String filePath = excelSplitToolController.getSelectFileTextField().getText(); |
| 119 | + String newFilePath = filePath; |
| 120 | + if (StringUtils.isNotEmpty(excelSplitToolController.getSaveFilePathTextField().getText())) { |
| 121 | + newFilePath = StringUtils.appendIfMissing(excelSplitToolController.getSaveFilePathTextField().getText(), "/", "/", "\\") + Paths.get(newFilePath).getFileName(); |
| 122 | + } |
| 123 | + int splitNumber = 0; |
| 124 | + if (excelSplitToolController.getSplitType1RadioButton().isSelected()) { |
| 125 | + File file = new File(filePath); |
| 126 | + LineNumberReader rf = new LineNumberReader(new FileReader(file)); |
| 127 | + rf.skip(file.length()); |
| 128 | + splitNumber = (int) Math.ceil((double) rf.getLineNumber() / excelSplitToolController.getSplitType1Spinner().getValue()); |
| 129 | + saveSplitFile(filePath, splitNumber, newFilePath); |
| 130 | + } else if (excelSplitToolController.getSplitType2RadioButton().isSelected()) { |
| 131 | + splitNumber = excelSplitToolController.getSplitType2Spinner().getValue(); |
| 132 | + saveSplitFile(filePath, splitNumber, newFilePath); |
| 133 | + } else if (excelSplitToolController.getSplitType3RadioButton().isSelected()) { |
| 134 | + throw new Exception("文件不支持按类拆分。"); |
| 135 | + } |
116 | 136 | } |
117 | 137 |
|
118 | 138 | private void saveSplitWorkbook(Sheet sheet, int splitNumber, String newFilePath) throws Exception { |
@@ -272,6 +292,24 @@ private void saveSplitCsv(Iterator<CSVRecord> iterator, String newFilePath, Stri |
272 | 292 | } |
273 | 293 | } |
274 | 294 |
|
| 295 | + private void saveSplitFile(String filePath, int splitNumber, String newFilePath) throws Exception { |
| 296 | + int addRowIndex = 0; |
| 297 | + int saveFileIndex = 0; |
| 298 | + LineIterator lineIterator = FileUtils.lineIterator(new File(filePath)); |
| 299 | + File newFile = new File(newFilePath + "-" + (saveFileIndex++)); |
| 300 | + newFile.delete(); |
| 301 | + while (lineIterator.hasNext()) { |
| 302 | + String line = lineIterator.next(); |
| 303 | + FileUtils.writeStringToFile(newFile, line + DirectoryTreeUtil.LINE_SEPARATOR, true); |
| 304 | + if (++addRowIndex == splitNumber) { |
| 305 | + newFile = new File(newFilePath + "-" + (saveFileIndex++)); |
| 306 | + newFile.delete(); |
| 307 | + addRowIndex = 0; |
| 308 | + } |
| 309 | + } |
| 310 | + lineIterator.close(); |
| 311 | + } |
| 312 | + |
275 | 313 | private static void saveFile(Workbook workbook, String filePath) { |
276 | 314 | if (workbook instanceof HSSFWorkbook) { |
277 | 315 | filePath = StringUtils.appendIfMissing(filePath, ".xls", ".xls", ".xlsx"); |
|
0 commit comments