Skip to content

Commit bba2e58

Browse files
committed
添加Excel拆分工具。
1 parent 4b8c151 commit bba2e58

8 files changed

Lines changed: 230 additions & 2 deletions

File tree

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,21 @@
200200
<artifactId>jsch</artifactId>
201201
<version>0.1.55</version>
202202
</dependency>
203+
204+
<!--zookeeper连接工具-->
203205
<dependency>
204206
<groupId>com.101tec</groupId>
205207
<artifactId>zkclient</artifactId>
206208
<version>0.10</version>
207209
<scope>compile</scope>
208210
</dependency>
209211

212+
<!--excel操作工具-->
213+
<dependency>
214+
<groupId>org.apache.poi</groupId>
215+
<artifactId>poi</artifactId>
216+
<version>4.1.0</version>
217+
</dependency>
210218
<!-- 目录文件监控工具包 -->
211219
<!-- <dependency> <groupId>net.sf.jpathwatch</groupId> <artifactId>jpathwatch</artifactId>
212220
<version>0.95</version> </dependency> -->
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.xwintop.xJavaFxTool.controller.littleTools;
2+
3+
import com.xwintop.xJavaFxTool.services.littleTools.ExcelSplitToolService;
4+
import com.xwintop.xJavaFxTool.utils.JavaFxViewUtil;
5+
import com.xwintop.xJavaFxTool.view.littleTools.ExcelSplitToolView;
6+
import com.xwintop.xcore.util.javafx.FileChooserUtil;
7+
import com.xwintop.xcore.util.javafx.TooltipUtil;
8+
import javafx.event.ActionEvent;
9+
import javafx.fxml.FXML;
10+
import lombok.Getter;
11+
import lombok.Setter;
12+
import lombok.extern.slf4j.Slf4j;
13+
14+
import java.io.File;
15+
import java.net.URL;
16+
import java.util.ResourceBundle;
17+
18+
@Getter
19+
@Setter
20+
@Slf4j
21+
public class ExcelSplitToolController extends ExcelSplitToolView {
22+
private ExcelSplitToolService excelSplitToolService = new ExcelSplitToolService(this);
23+
24+
@Override
25+
public void initialize(URL location, ResourceBundle resources) {
26+
initView();
27+
initEvent();
28+
initService();
29+
}
30+
31+
private void initView() {
32+
JavaFxViewUtil.setSpinnerValueFactory(splitType1Spinner, 1, Integer.MAX_VALUE, 3);
33+
JavaFxViewUtil.setSpinnerValueFactory(splitType2Spinner, 1, Integer.MAX_VALUE, 10);
34+
}
35+
36+
private void initEvent() {
37+
selectFileTextField.setText("C:\\Users\\5FDSJ068\\Desktop\\address list20180411.xls");
38+
FileChooserUtil.setOnDrag(selectFileTextField, FileChooserUtil.FileType.FILE);
39+
}
40+
41+
private void initService() {
42+
}
43+
44+
@FXML
45+
private void selectFileAction(ActionEvent event) {
46+
File file = FileChooserUtil.chooseFile();
47+
if (file != null) {
48+
selectFileTextField.setText(file.getPath());
49+
}
50+
}
51+
52+
@FXML
53+
private void splitAction(ActionEvent event) {
54+
try {
55+
excelSplitToolService.splitAction();
56+
} catch (Exception e) {
57+
TooltipUtil.showToast("解析失败:" + e.getMessage());
58+
log.error("解析失败:", e);
59+
}
60+
}
61+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.xwintop.xJavaFxTool.view.littleTools;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.fxml.Initializable;
5+
import javafx.scene.control.*;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
@Getter
10+
@Setter
11+
public abstract class ExcelSplitToolView implements Initializable {
12+
@FXML
13+
protected TextField selectFileTextField;
14+
@FXML
15+
protected Button selectFileButton;
16+
@FXML
17+
protected CheckBox includeHandCheckBox;
18+
@FXML
19+
protected RadioButton splitType1RadioButton;
20+
@FXML
21+
protected ToggleGroup splitTypeToggleGroup;
22+
@FXML
23+
protected Spinner<Integer> splitType1Spinner;
24+
@FXML
25+
protected RadioButton splitType2RadioButton;
26+
@FXML
27+
protected Spinner<Integer> splitType2Spinner;
28+
@FXML
29+
protected RadioButton outputType1RadioButton;
30+
@FXML
31+
protected ToggleGroup outputTypeToggleGroup;
32+
@FXML
33+
protected RadioButton outputType2RadioButton;
34+
@FXML
35+
protected Button splitButton;
36+
@FXML
37+
protected RadioButton splitType3RadioButton;
38+
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.Button?>
4+
<?import javafx.scene.control.CheckBox?>
5+
<?import javafx.scene.control.Label?>
6+
<?import javafx.scene.control.RadioButton?>
7+
<?import javafx.scene.control.Spinner?>
8+
<?import javafx.scene.control.TextField?>
9+
<?import javafx.scene.control.ToggleGroup?>
10+
<?import javafx.scene.layout.AnchorPane?>
11+
12+
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.xwintop.xJavaFxTool.controller.littleTools.ExcelSplitToolController">
13+
<children>
14+
<Label layoutX="31.0" layoutY="24.0" text="文件选择:" />
15+
<TextField fx:id="selectFileTextField" layoutX="99.0" layoutY="20.0" />
16+
<Button fx:id="selectFileButton" layoutX="256.0" layoutY="20.0" mnemonicParsing="false" onAction="#selectFileAction" text="选择" />
17+
<Label layoutX="31.0" layoutY="66.0" text="文件分隔方式:" />
18+
<CheckBox fx:id="includeHandCheckBox" layoutX="31.0" layoutY="100.0" mnemonicParsing="false" text="Excel包含标题行" />
19+
<RadioButton fx:id="splitType1RadioButton" layoutX="34.0" layoutY="131.0" mnemonicParsing="false" selected="true" text="按平均分成">
20+
<toggleGroup>
21+
<ToggleGroup fx:id="splitTypeToggleGroup" />
22+
</toggleGroup>
23+
</RadioButton>
24+
<Spinner fx:id="splitType1Spinner" layoutX="126.0" layoutY="128.0" prefHeight="23.0" prefWidth="60.0" />
25+
<RadioButton fx:id="splitType2RadioButton" layoutX="248.0" layoutY="132.0" mnemonicParsing="false" text="按每份" toggleGroup="$splitTypeToggleGroup" />
26+
<Label layoutX="198.0" layoutY="132.0" text="" />
27+
<Spinner fx:id="splitType2Spinner" layoutX="314.0" layoutY="129.0" prefHeight="23.0" prefWidth="60.0" />
28+
<Label layoutX="384.0" layoutY="132.0" text="" />
29+
<Label layoutX="31.0" layoutY="193.0" text="选择输出格式:" />
30+
<RadioButton fx:id="outputType1RadioButton" layoutX="29.0" layoutY="212.0" mnemonicParsing="false" selected="true" text="xls格式">
31+
<toggleGroup>
32+
<ToggleGroup fx:id="outputTypeToggleGroup" />
33+
</toggleGroup>
34+
</RadioButton>
35+
<RadioButton fx:id="outputType2RadioButton" layoutX="125.0" layoutY="212.0" mnemonicParsing="false" text="xlsx格式" toggleGroup="$outputTypeToggleGroup" />
36+
<Button fx:id="splitButton" layoutX="29.0" layoutY="247.0" mnemonicParsing="false" onAction="#splitAction" text="开始分隔" />
37+
<RadioButton fx:id="splitType3RadioButton" layoutX="436.0" layoutY="133.0" mnemonicParsing="false" text="按列分类拆分" toggleGroup="$splitTypeToggleGroup" />
38+
</children>
39+
</AnchorPane>

src/main/resources/config/toolFxmlLoaderConfiguration.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@
249249
<url>/com/xwintop/xJavaFxTool/fxmlView/debugTools/ZookeeperTool.fxml</url>
250250
<title>ZookeeperTool</title>
251251
<menuParentId>debugTools</menuParentId>
252+
</ToolFxmlLoaderConfiguration>
253+
<ToolFxmlLoaderConfiguration>
254+
<url>/com/xwintop/xJavaFxTool/fxmlView/littleTools/ExcelSplitTool.fxml</url>
255+
<title>ExcelSplitTool</title>
256+
<menuParentId>littleTools</menuParentId>
252257
<isDefaultShow>true</isDefaultShow>
253258
</ToolFxmlLoaderConfiguration>
254259

src/main/resources/locale/Menu.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ SocketTool=Socket\u8C03\u8BD5\u5DE5\u5177
7878
ImageAnalysisTool=\u56FE\u7247\u62C6\u5206\u5DE5\u5177
7979
DecompilerWxApkgTool=\u53CD\u7F16\u8BD1\u5FAE\u4FE1\u5C0F\u7A0B\u5E8F\u5305\u5DE5\u5177
8080
GatewayConfTool=Gateway\u914D\u7F6E\u5DE5\u5177
81-
ZookeeperTool=Zookeeper\u5DE5\u5177
81+
ZookeeperTool=Zookeeper\u5DE5\u5177
82+
ExcelSplitTool=Excel\u62C6\u5206\u5DE5\u5177

src/main/resources/locale/Menu_en_US.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ SocketTool=SocketTool
7878
ImageAnalysisTool=ImageAnalysisTool
7979
DecompilerWxApkgTool=DecompilerWxApkgTool
8080
GatewayConfTool=GatewayConfTool
81-
ZookeeperTool=ZookeeperTool
81+
ZookeeperTool=ZookeeperTool
82+
ExcelSplitTool=ExcelSplitTool

0 commit comments

Comments
 (0)