@@ -56,50 +56,53 @@ public void splitAction() throws Exception { //拆分表格
5656 public void splitExcel () throws Exception { //拆分Excel表格
5757 String filePath = excelSplitToolController .getSelectFileTextField ().getText ();
5858 FileInputStream fileInputStream = new FileInputStream (filePath );
59-
60- Workbook workbook = null ;
61- if (filePath .endsWith (".xls" )) {
62- POIFSFileSystem fileSystem = new POIFSFileSystem (fileInputStream );
63- workbook = new HSSFWorkbook (fileSystem );
64- } else if (filePath .endsWith (".xlsx" )) {
65- workbook = new XSSFWorkbook (fileInputStream );
66- } else {
67- throw new RuntimeException ("错误提示: 您设置的Excel文件名不合法!" );
68- }
69- String sheetSelectString = excelSplitToolController .getSheetSelectTextField ().getText ();
70- Sheet [] sheets = null ;
71- if (StringUtils .isEmpty (sheetSelectString )) {
72- sheets = new Sheet []{workbook .getSheetAt (0 )};
73- } else {
74- String [] sheetSelectStrings = sheetSelectString .split ("," );
75- sheets = new Sheet [sheetSelectStrings .length ];
76- for (int i = 0 ; i < sheetSelectStrings .length ; i ++) {
77- sheets [i ] = workbook .getSheetAt (Integer .valueOf (sheetSelectStrings [i ]));
78- }
79- }
80- String newFilePath = StringUtils .appendIfMissing (filePath , "" , ".xls" , ".xlsx" );
81- if (StringUtils .isNotEmpty (excelSplitToolController .getSaveFilePathTextField ().getText ())) {
82- newFilePath = StringUtils .appendIfMissing (excelSplitToolController .getSaveFilePathTextField ().getText (), "/" , "/" , "\\ " ) + Paths .get (newFilePath ).getFileName ();
83- }
84- if (excelSplitToolController .getSplitType1RadioButton ().isSelected ()) {
85- int allRowNumber = 0 ;
86- for (Sheet sheet : sheets ) {
87- allRowNumber += sheet .getLastRowNum ();// 行数
59+ try {
60+ Workbook workbook = null ;
61+ if (filePath .endsWith (".xls" )) {
62+ POIFSFileSystem fileSystem = new POIFSFileSystem (fileInputStream );
63+ workbook = new HSSFWorkbook (fileSystem );
64+ } else if (filePath .endsWith (".xlsx" )) {
65+ workbook = new XSSFWorkbook (fileInputStream );
66+ } else {
67+ throw new RuntimeException ("错误提示: 您设置的Excel文件名不合法!" );
8868 }
89- int splitNumber = (int ) Math .ceil ((double ) allRowNumber / excelSplitToolController .getSplitType1Spinner ().getValue ());
90- saveSplitWorkbook (sheets , splitNumber , newFilePath );
91- } else if (excelSplitToolController .getSplitType2RadioButton ().isSelected ()) {
92- int splitNumber = excelSplitToolController .getSplitType2Spinner ().getValue ();
93- saveSplitWorkbook (sheets , splitNumber , newFilePath );
94- } else if (excelSplitToolController .getSplitType3RadioButton ().isSelected ()) {
95- String splitType3String = excelSplitToolController .getSplitType3TextField ().getText ();
96- String [] splitCellIndex = null ;
97- if (StringUtils .isEmpty (splitType3String )) {
98- splitCellIndex = new String []{"0" };
69+ String sheetSelectString = excelSplitToolController .getSheetSelectTextField ().getText ();
70+ Sheet [] sheets = null ;
71+ if (StringUtils .isEmpty (sheetSelectString )) {
72+ sheets = new Sheet []{workbook .getSheetAt (0 )};
9973 } else {
100- splitCellIndex = splitType3String .split ("," );
74+ String [] sheetSelectStrings = sheetSelectString .split ("," );
75+ sheets = new Sheet [sheetSelectStrings .length ];
76+ for (int i = 0 ; i < sheetSelectStrings .length ; i ++) {
77+ sheets [i ] = workbook .getSheetAt (Integer .valueOf (sheetSelectStrings [i ]));
78+ }
10179 }
102- saveSplitWorkbook (sheets , newFilePath , splitCellIndex );
80+ String newFilePath = StringUtils .appendIfMissing (filePath , "" , ".xls" , ".xlsx" );
81+ if (StringUtils .isNotEmpty (excelSplitToolController .getSaveFilePathTextField ().getText ())) {
82+ newFilePath = StringUtils .appendIfMissing (excelSplitToolController .getSaveFilePathTextField ().getText (), "/" , "/" , "\\ " ) + Paths .get (newFilePath ).getFileName ();
83+ }
84+ if (excelSplitToolController .getSplitType1RadioButton ().isSelected ()) {
85+ int allRowNumber = 0 ;
86+ for (Sheet sheet : sheets ) {
87+ allRowNumber += sheet .getLastRowNum ();// 行数
88+ }
89+ int splitNumber = (int ) Math .ceil ((double ) allRowNumber / excelSplitToolController .getSplitType1Spinner ().getValue ());
90+ saveSplitWorkbook (sheets , splitNumber , newFilePath );
91+ } else if (excelSplitToolController .getSplitType2RadioButton ().isSelected ()) {
92+ int splitNumber = excelSplitToolController .getSplitType2Spinner ().getValue ();
93+ saveSplitWorkbook (sheets , splitNumber , newFilePath );
94+ } else if (excelSplitToolController .getSplitType3RadioButton ().isSelected ()) {
95+ String splitType3String = excelSplitToolController .getSplitType3TextField ().getText ();
96+ String [] splitCellIndex = null ;
97+ if (StringUtils .isEmpty (splitType3String )) {
98+ splitCellIndex = new String []{"0" };
99+ } else {
100+ splitCellIndex = splitType3String .split ("," );
101+ }
102+ saveSplitWorkbook (sheets , newFilePath , splitCellIndex );
103+ }
104+ } finally {
105+ fileInputStream .close ();
103106 }
104107 }
105108
0 commit comments