44import lombok .Getter ;
55import lombok .Setter ;
66import lombok .extern .slf4j .Slf4j ;
7+ import org .apache .commons .collections .IteratorUtils ;
8+ import org .apache .commons .csv .CSVFormat ;
9+ import org .apache .commons .csv .CSVParser ;
10+ import org .apache .commons .csv .CSVPrinter ;
11+ import org .apache .commons .csv .CSVRecord ;
712import org .apache .commons .lang3 .StringUtils ;
813import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
914import org .apache .poi .poifs .filesystem .POIFSFileSystem ;
1318import org .apache .poi .ss .usermodel .Workbook ;
1419import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
1520
16- import java .io .File ;
17- import java .io .FileInputStream ;
18- import java .io .FileOutputStream ;
19- import java .io .IOException ;
21+ import java .io .*;
2022import java .nio .file .Paths ;
2123import java .util .*;
2224
@@ -31,6 +33,17 @@ public ExcelSplitToolService(ExcelSplitToolController excelSplitToolController)
3133 }
3234
3335 public void splitAction () throws Exception { //拆分表格
36+ int fileTypeSelectIndex = excelSplitToolController .getFileTypeChoiceBox ().getSelectionModel ().getSelectedIndex ();
37+ if (fileTypeSelectIndex == 0 ) {
38+ splitExcel ();
39+ } else if (fileTypeSelectIndex == 1 ) {
40+ splitCsv ();
41+ } else if (fileTypeSelectIndex == 2 ) {
42+ splitFile ();
43+ }
44+ }
45+
46+ public void splitExcel () throws Exception { //拆分Excel表格
3447 String filePath = excelSplitToolController .getSelectFileTextField ().getText ();
3548 FileInputStream fileInputStream = new FileInputStream (filePath );
3649
@@ -67,40 +80,39 @@ public void splitAction() throws Exception { //拆分表格
6780 }
6881 saveSplitWorkbook (sheet , newFilePath , splitCellIndex );
6982 }
83+ }
84+
85+ public void splitCsv () throws Exception {
86+ String filePath = excelSplitToolController .getSelectFileTextField ().getText ();
87+ Reader fileReader = new FileReader (filePath );
88+ String newFilePath = StringUtils .appendIfMissing (filePath , "" , ".csv" , ".CSV" );
89+ if (StringUtils .isNotEmpty (excelSplitToolController .getSaveFilePathTextField ().getText ())) {
90+ newFilePath = StringUtils .appendIfMissing (excelSplitToolController .getSaveFilePathTextField ().getText (), "/" , "/" , "\\ " ) + Paths .get (newFilePath ).getFileName ();
91+ }
92+ CSVParser parser = CSVFormat .DEFAULT .parse (fileReader );
93+ int splitNumber = 0 ;
94+ if (excelSplitToolController .getSplitType1RadioButton ().isSelected ()) {
95+ List <CSVRecord > csvRecordList = parser .getRecords ();
96+ splitNumber = csvRecordList .size () / (excelSplitToolController .getSplitType1Spinner ().getValue () - 1 );
97+ saveSplitCsv (csvRecordList .iterator (), splitNumber , newFilePath );
98+ } else if (excelSplitToolController .getSplitType2RadioButton ().isSelected ()) {
99+ splitNumber = excelSplitToolController .getSplitType2Spinner ().getValue ();
100+ saveSplitCsv (parser .iterator (), splitNumber , newFilePath );
101+ } else if (excelSplitToolController .getSplitType3RadioButton ().isSelected ()) {
102+ String splitType3String = excelSplitToolController .getSplitType3TextField ().getText ();
103+ String [] splitCellIndex = null ;
104+ if (StringUtils .isEmpty (splitType3String )) {
105+ splitCellIndex = new String []{"0" };
106+ } else {
107+ splitCellIndex = splitType3String .split ("," );
108+ }
109+ saveSplitCsv (parser .iterator (), newFilePath , splitCellIndex );
110+ }
111+ fileReader .close ();
112+ }
113+
114+ public void splitFile () {
70115
71- // for (int i = 0; i <= lastRowIndex; i++) {
72- // Row row = sheet.getRow(i);
73- // if (row == null) {
74- // break;
75- // }
76- // short lastCellNum = row.getLastCellNum();// 列数
77- // for (int j = 0; j < lastCellNum; j++) {
78- // Cell cell = row.getCell(j);
79- // String str = null;
80- // switch (cell.getCellType()) {
81- // case BLANK:// 空值
82- // str = "";
83- // break;
84- // case BOOLEAN:
85- // str = String.valueOf(cell.getBooleanCellValue());// 布尔型
86- // break;
87- // case FORMULA:
88- // str = String.valueOf(cell.getCellFormula());// 公式型
89- // break;
90- // case NUMERIC:
91- // str = String.valueOf(cell.getNumericCellValue());// 数值型
92- // break;
93- // case STRING:
94- // str = String.valueOf(cell.getStringCellValue());// 字符型
95- // break;
96- // default:
97- // str = null;
98- // break;
99- // }
100- // System.out.print(str + "\t\t");
101- // }
102- // System.out.println();
103- // }
104116 }
105117
106118 private void saveSplitWorkbook (Sheet sheet , int splitNumber , String newFilePath ) throws Exception {
@@ -189,7 +201,73 @@ private void saveSplitWorkbook(Sheet sheet, String newFilePath, String[] splitCe
189201
190202 }
191203
192- public static void saveFile (Workbook workbook , String filePath ) {
204+ private void saveSplitCsv (Iterator <CSVRecord > iterator , int splitNumber , String newFilePath ) throws Exception {
205+ int addRowIndex = 0 ;
206+ int saveFileIndex = 0 ;
207+ boolean isAddHead = excelSplitToolController .getIncludeHandCheckBox ().isSelected ();
208+ CSVPrinter printer = null ;
209+ CSVRecord firstRecord = null ;
210+ while (iterator .hasNext ()) {
211+ CSVRecord record = iterator .next ();
212+ if (isAddHead && addRowIndex == 0 && saveFileIndex == 0 ) {
213+ firstRecord = record ;
214+ }
215+ if (printer == null ) {
216+ printer = CSVFormat .DEFAULT .print (new PrintWriter (newFilePath + "-" + (saveFileIndex ++) + ".csv" ));
217+ if (isAddHead && saveFileIndex != 0 ) {
218+ printer .printRecord (IteratorUtils .toList (record .iterator ()).toArray ());
219+ }
220+ }
221+ printer .printRecord (IteratorUtils .toList (record .iterator ()).toArray ());
222+ if ((addRowIndex ++ - splitNumber ) == (isAddHead ? 1 : 0 )) {
223+ printer .flush ();
224+ printer .close ();
225+ printer = null ;
226+ addRowIndex = 0 ;
227+ }
228+ }
229+ if (printer != null ) {
230+ printer .flush ();
231+ printer .close ();
232+ }
233+ }
234+
235+ private void saveSplitCsv (Iterator <CSVRecord > iterator , String newFilePath , String [] splitCellIndex ) throws Exception {
236+ CSVRecord firstRecord = null ;
237+ Map <String , List <CSVRecord >> rowMap = new HashMap <>();
238+ while (iterator .hasNext ()) {
239+ CSVRecord record = iterator .next ();
240+ if (firstRecord == null ) {
241+ firstRecord = record ;
242+ }
243+ String rowString = "" ;
244+ for (String cellIndex : splitCellIndex ) {
245+ rowString += record .get (Integer .valueOf (cellIndex ));
246+ }
247+ if (rowMap .containsKey (rowString )) {
248+ rowMap .get (rowString ).add (record );
249+ } else {
250+ List <CSVRecord > rowList = new ArrayList <>();
251+ rowList .add (record );
252+ rowMap .put (rowString , rowList );
253+ }
254+ }
255+ for (Map .Entry <String , List <CSVRecord >> stringListEntry : rowMap .entrySet ()) {
256+ String key = stringListEntry .getKey ();
257+ List <CSVRecord > rows = stringListEntry .getValue ();
258+ CSVPrinter printer = CSVFormat .DEFAULT .print (new PrintWriter (newFilePath + "-" + key + ".csv" ));
259+ if (excelSplitToolController .getIncludeHandCheckBox ().isSelected ()) {
260+ printer .printRecord (IteratorUtils .toList (firstRecord .iterator ()).toArray ());
261+ }
262+ for (CSVRecord row : rows ) {
263+ printer .printRecord (IteratorUtils .toList (row .iterator ()).toArray ());
264+ }
265+ printer .flush ();
266+ printer .close ();
267+ }
268+ }
269+
270+ private static void saveFile (Workbook workbook , String filePath ) {
193271 if (workbook instanceof HSSFWorkbook ) {
194272 filePath = StringUtils .appendIfMissing (filePath , ".xls" , ".xls" , ".xlsx" );
195273 } else if (workbook instanceof XSSFWorkbook ) {
@@ -212,7 +290,7 @@ public static void saveFile(Workbook workbook, String filePath) {
212290 }
213291 }
214292
215- public static void copyRow (Row row , Row newRow ) {
293+ private static void copyRow (Row row , Row newRow ) {
216294 try {
217295 newRow .setHeight (row .getHeight ());
218296 newRow .setHeightInPoints (row .getHeightInPoints ());
@@ -226,7 +304,7 @@ public static void copyRow(Row row, Row newRow) {
226304 });
227305 }
228306
229- public static void setCellValue (Cell cell , Cell newCell ) {
307+ private static void setCellValue (Cell cell , Cell newCell ) {
230308 try {
231309 newCell .setCellStyle (cell .getCellStyle ());
232310 } catch (Exception e ) {
@@ -254,7 +332,7 @@ public static void setCellValue(Cell cell, Cell newCell) {
254332 }
255333 }
256334
257- public static String getCellValue (Cell cell ) {
335+ private static String getCellValue (Cell cell ) {
258336 String str = null ;
259337 switch (cell .getCellType ()) {
260338 case BLANK :// 空值
0 commit comments