44import lombok .Getter ;
55import lombok .Setter ;
66import lombok .extern .slf4j .Slf4j ;
7- import org .apache .poi .hssf .usermodel .HSSFCell ;
8- import org .apache .poi .hssf .usermodel .HSSFRow ;
9- import org .apache .poi .hssf .usermodel .HSSFSheet ;
107import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
118import org .apache .poi .poifs .filesystem .POIFSFileSystem ;
12- import java .io .BufferedInputStream ;
13- import java .io .FileInputStream ;
9+ import org .apache .poi .ss .usermodel .Cell ;
10+ import org .apache .poi .ss .usermodel .Row ;
11+ import org .apache .poi .ss .usermodel .Sheet ;
12+ import org .apache .poi .ss .usermodel .Workbook ;
13+ import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
1414
15- import static org .apache .poi .ss .usermodel .CellType .BLANK ;
15+ import java .io .File ;
16+ import java .io .FileInputStream ;
17+ import java .io .FileOutputStream ;
18+ import java .io .IOException ;
1619
1720@ Getter
1821@ Setter
@@ -27,48 +30,133 @@ public ExcelSplitToolService(ExcelSplitToolController excelSplitToolController)
2730 public void splitAction () throws Exception { //拆分表格
2831 String filePath = excelSplitToolController .getSelectFileTextField ().getText ();
2932 FileInputStream fileInputStream = new FileInputStream (filePath );
30- BufferedInputStream bufferedInputStream = new BufferedInputStream (fileInputStream );
31-
32- POIFSFileSystem fileSystem = new POIFSFileSystem (bufferedInputStream );
33- HSSFWorkbook workbook = new HSSFWorkbook (fileSystem );
34- // HSSFSheet sheet = workbook.getSheet("Sheet1");
35- HSSFSheet sheet = workbook .getSheetAt (0 );
3633
34+ Workbook workbook = null ;
35+ if (filePath .endsWith (".xlsx" )) {
36+ workbook = new XSSFWorkbook (fileInputStream );
37+ } else if (filePath .endsWith (".xls" )) {
38+ POIFSFileSystem fileSystem = new POIFSFileSystem (fileInputStream );
39+ workbook = new HSSFWorkbook (fileSystem );
40+ } else {
41+ throw new RuntimeException ("错误提示: 您设置的Excel文件名不合法!" );
42+ }
43+ Sheet sheet = workbook .getSheetAt (0 );
3744 int lastRowIndex = sheet .getLastRowNum ();// 行数
38- System .out .println ("行数:" + lastRowIndex );
45+ log .info ("读取到Excel文件行数:" + lastRowIndex );
46+ int splitNumber = 0 ;
47+ if (excelSplitToolController .getSplitType1RadioButton ().isSelected ()) {
48+ splitNumber = lastRowIndex / (excelSplitToolController .getSplitType1Spinner ().getValue () - 1 );
49+ } else if (excelSplitToolController .getSplitType2RadioButton ().isSelected ()) {
50+ splitNumber = excelSplitToolController .getSplitType2Spinner ().getValue ();
51+ } else if (excelSplitToolController .getSplitType3RadioButton ().isSelected ()) {
52+
53+ }
54+ Workbook newWorkbook = null ;
55+ if (excelSplitToolController .getOutputType1RadioButton ().isSelected ()) {
56+ newWorkbook = new HSSFWorkbook ();
57+ } else {
58+ newWorkbook = new XSSFWorkbook ();
59+ }
60+ Sheet newSheet = newWorkbook .createSheet ();
61+ int addRowIndex = 0 ;
3962 for (int i = 0 ; i <= lastRowIndex ; i ++) {
40- HSSFRow row = sheet .getRow (i );
41- if (row == null ) {
42- break ;
43- }
44- short lastCellNum = row .getLastCellNum ();// 列数
45- for (int j = 0 ; j < lastCellNum ; j ++) {
46- HSSFCell cell = row .getCell (j );
47- String str = null ;
48- switch (cell .getCellType ()) {
49- case BLANK :// 空值
50- str = "" ;
51- break ;
52- case BOOLEAN :
53- str = String .valueOf (cell .getBooleanCellValue ());// 布尔型
54- break ;
55- case FORMULA :
56- str = String .valueOf (cell .getCellFormula ());// 公式型
57- break ;
58- case NUMERIC :
59- str = String .valueOf (cell .getNumericCellValue ());// 数值型
60- break ;
61- case STRING :
62- str = String .valueOf (cell .getStringCellValue ());// 字符型
63- break ;
64- default :
65- str = null ;
66- break ;
63+ Row row = newSheet .createRow (addRowIndex ++);
64+ sheet .getRow (i ).cellIterator ().forEachRemaining (cell -> {
65+ setCellValue (cell , row .createCell (cell .getColumnIndex (), cell .getCellType ()));
66+ });
67+ if (addRowIndex == splitNumber ) {
68+ FileOutputStream out = null ;
69+ try {
70+ out = new FileOutputStream (new File (new File (filePath ).getParent (), "newExcel" + Math .random () + ".xls" ));
71+ newWorkbook .write (out );
72+ } catch (IOException e ) {
73+ System .out .println (e .toString ());
74+ } finally {
75+ try {
76+ out .close ();
77+ } catch (IOException e ) {
78+ System .out .println (e .toString ());
79+ }
6780 }
68- System .out .print (str + "\t \t " );
81+ addRowIndex = 0 ;
82+ if (excelSplitToolController .getOutputType1RadioButton ().isSelected ()) {
83+ newWorkbook = new HSSFWorkbook ();
84+ } else {
85+ newWorkbook = new XSSFWorkbook ();
86+ }
87+ newSheet = newWorkbook .createSheet ();
88+ }
89+ }
90+
91+ FileOutputStream out = null ;
92+ try {
93+ out = new FileOutputStream (new File (new File (filePath ).getParent (), "newExcel" + Math .random () + ".xls" ));
94+ newWorkbook .write (out );
95+ } catch (IOException e ) {
96+ System .out .println (e .toString ());
97+ } finally {
98+ try {
99+ out .close ();
100+ } catch (IOException e ) {
101+ System .out .println (e .toString ());
69102 }
70- System .out .println ();
71103 }
72- bufferedInputStream .close ();
104+ // for (int i = 0; i <= lastRowIndex; i++) {
105+ // Row row = sheet.getRow(i);
106+ // if (row == null) {
107+ // break;
108+ // }
109+ // short lastCellNum = row.getLastCellNum();// 列数
110+ // for (int j = 0; j < lastCellNum; j++) {
111+ // Cell cell = row.getCell(j);
112+ // String str = null;
113+ // switch (cell.getCellType()) {
114+ // case BLANK:// 空值
115+ // str = "";
116+ // break;
117+ // case BOOLEAN:
118+ // str = String.valueOf(cell.getBooleanCellValue());// 布尔型
119+ // break;
120+ // case FORMULA:
121+ // str = String.valueOf(cell.getCellFormula());// 公式型
122+ // break;
123+ // case NUMERIC:
124+ // str = String.valueOf(cell.getNumericCellValue());// 数值型
125+ // break;
126+ // case STRING:
127+ // str = String.valueOf(cell.getStringCellValue());// 字符型
128+ // break;
129+ // default:
130+ // str = null;
131+ // break;
132+ // }
133+ // System.out.print(str + "\t\t");
134+ // }
135+ // System.out.println();
136+ // }
137+ }
138+
139+ public static void setCellValue (Cell cell , Cell newCell ) {
140+ // 以下是判断数据的类型
141+ switch (cell .getCellType ()) {
142+ case BLANK :// 空值
143+ newCell .setBlank ();
144+ break ;
145+ case BOOLEAN :
146+ newCell .setCellValue (cell .getBooleanCellValue ());// 布尔型
147+ break ;
148+ case FORMULA :
149+ newCell .setCellValue (cell .getCellFormula ());// 公式型
150+ break ;
151+ case NUMERIC :
152+ newCell .setCellValue (cell .getNumericCellValue ());// 数值型
153+ break ;
154+ case STRING :
155+ newCell .setCellValue (cell .getStringCellValue ());// 字符型
156+ break ;
157+ default :
158+ newCell .setBlank ();
159+ break ;
160+ }
73161 }
74162}
0 commit comments