1+ package com .xwintop .xJavaFxTool .services .littleTools ;
2+
3+ import com .xwintop .xJavaFxTool .controller .littleTools .ExcelSplitToolController ;
4+ import lombok .Getter ;
5+ import lombok .Setter ;
6+ import 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 ;
10+ import org .apache .poi .hssf .usermodel .HSSFWorkbook ;
11+ import org .apache .poi .poifs .filesystem .POIFSFileSystem ;
12+ import java .io .BufferedInputStream ;
13+ import java .io .FileInputStream ;
14+
15+ import static org .apache .poi .ss .usermodel .CellType .BLANK ;
16+
17+ @ Getter
18+ @ Setter
19+ @ Slf4j
20+ public class ExcelSplitToolService {
21+ private ExcelSplitToolController excelSplitToolController ;
22+
23+ public ExcelSplitToolService (ExcelSplitToolController excelSplitToolController ) {
24+ this .excelSplitToolController = excelSplitToolController ;
25+ }
26+
27+ public void splitAction () throws Exception { //拆分表格
28+ String filePath = excelSplitToolController .getSelectFileTextField ().getText ();
29+ 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 );
36+
37+ int lastRowIndex = sheet .getLastRowNum ();// 行数
38+ System .out .println ("行数:" + lastRowIndex );
39+ 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 ;
67+ }
68+ System .out .print (str + "\t \t " );
69+ }
70+ System .out .println ();
71+ }
72+ bufferedInputStream .close ();
73+ }
74+ }
0 commit comments