Skip to content

Commit 042d73a

Browse files
author
wangwenhua
committed
命令行参数支持
1 parent 7628361 commit 042d73a

File tree

4 files changed

+129
-10
lines changed

4 files changed

+129
-10
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
#### CSV文件转Excel
1+
### CSV文件转Excel
22

33
将csv文件拖拽到可爱的初音MM上即会在相同的目录生成Excle文件
44

5-
可执行jar包
5+
#### 双击执行
6+
可执行jar包,双击即可显示图形界面
67

7-
自定义窗口
8+
**自定义窗口**
89
图片要是透明背景的
910
- images/frame.png -- 主窗口
1011
- images/icon.png -- 任务栏小图标
1112
- images/success.png -- 弹出提示框
1213

1314

15+
#### 命令行执行
16+
17+
**命令行使用说明:**
18+
> 参数1:输入的CSV文件
19+
> 参数2:输出的Excel文件,默认输出为原文件相同的目录下
20+
> 参数3:CSV文件编码,如:GBK,默认为UTF-8
21+
22+
如:
23+
```
24+
$ java -jar CSV转Excel工具.jar /opt/export/device.csv
25+
$ java -jar CSV转Excel工具.jar /opt/export/device.csv /opt/download/device.xlsx
26+
$ java -jar CSV转Excel工具.jar /opt/export/device.csv /opt/download/device.xlsx UTF-8
27+
```
28+

src/main/java/com/wwh/excel/tools/Main.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,62 @@
11
package com.wwh.excel.tools;
22

33
import java.awt.EventQueue;
4+
import java.awt.GraphicsEnvironment;
45

56
import javax.swing.ImageIcon;
67

78
import com.wwh.excel.tools.swing.ImageFrame;
89
import com.wwh.excel.tools.swing.PopupDialog;
10+
import com.wwh.excel.tools.worker.Csv2Xlsx;
911

1012
public class Main {
1113
public static void main(String[] args) {
1214

15+
try {
16+
boolean b = false;
17+
if (args.length == 0) {
18+
// 测试此环境是否支持显示器、键盘和鼠标
19+
if (GraphicsEnvironment.isHeadless()) {
20+
System.out.println("无桌面环境,以无头模式运行!");
21+
} else {
22+
launchFrame();
23+
b = true;
24+
}
25+
} else if (args.length == 1) { // 参数化运行
26+
b = Csv2Xlsx.convert(args[0], null, null);
27+
} else if (args.length == 2) {
28+
b = Csv2Xlsx.convert(args[0], args[1], null);
29+
} else if (args.length == 3) {
30+
b = Csv2Xlsx.convert(args[0], args[1], args[2]);
31+
}
32+
33+
if (!b) {
34+
printUseage();
35+
}
36+
} catch (Exception e) {
37+
e.printStackTrace();
38+
printUseage();
39+
}
40+
41+
}
42+
43+
private static void printUseage() {
44+
System.out.println("");
45+
System.out.println("########################################################################");
46+
System.out.println("");
47+
System.out.println("命令行使用说明:");
48+
System.out.println(" 参数1:输入的CSV文件");
49+
System.out.println(" 参数2:输出的Excel文件,默认输出为原文件相同的目录下");
50+
System.out.println(" 参数3:CSV文件编码,如:GBK,默认为UTF-8");
51+
System.out.println("如:");
52+
System.out.println("$ java -jar CSV转Excel工具.jar /opt/export/device.csv");
53+
System.out.println("$ java -jar CSV转Excel工具.jar /opt/export/device.csv /opt/download/device.xlsx");
54+
System.out.println("$ java -jar CSV转Excel工具.jar /opt/export/device.csv /opt/download/device.xlsx UTF-8");
55+
System.out.println("");
56+
57+
}
58+
59+
private static void launchFrame() {
1360
EventQueue.invokeLater(new Runnable() {
1461
public void run() {
1562
try {
@@ -25,6 +72,7 @@ public void run() {
2572
frame.setVisible(true);
2673
} catch (Exception e) {
2774
e.printStackTrace();
75+
printUseage();
2876
}
2977
}
3078
});

src/main/java/com/wwh/excel/tools/swing/ImageFrame.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import javax.swing.JLabel;
2323
import javax.swing.JOptionPane;
2424

25-
import com.wwh.excel.tools.worker.Csv2Xls;
25+
import com.wwh.excel.tools.worker.Csv2Xlsx;
2626

2727
public class ImageFrame extends JFrame {
2828

@@ -119,7 +119,7 @@ public void drop(DropTargetDropEvent dtde)// 重写适配器的drop方法
119119
return;
120120
}
121121

122-
Csv2Xls.convert(list.get(0));
122+
Csv2Xlsx.convert(list.get(0));
123123

124124
Point losPoint = ImageFrame.this.getLocationOnScreen();
125125
popupDialog.showDelayHide(losPoint.x + dtde.getLocation().x, losPoint.y + dtde.getLocation().y);

src/main/java/com/wwh/excel/tools/worker/Csv2Xls.java renamed to src/main/java/com/wwh/excel/tools/worker/Csv2Xlsx.java

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import java.io.FileInputStream;
66
import java.io.FileOutputStream;
77
import java.io.InputStreamReader;
8+
import java.nio.charset.Charset;
89

10+
import org.apache.commons.lang3.StringUtils;
911
import org.apache.poi.ss.usermodel.Cell;
1012
import org.apache.poi.ss.usermodel.Row;
1113
import org.apache.poi.ss.usermodel.Sheet;
@@ -14,23 +16,75 @@
1416

1517
import com.opencsv.CSVReader;
1618

17-
public class Csv2Xls {
19+
/**
20+
* 文件转换工具类
21+
*
22+
* @author wwh
23+
*
24+
*/
25+
public class Csv2Xlsx {
1826

19-
public static final String CHARSET = "UTF-8";
27+
public static final String DEFAULT_CHARSET = "UTF-8";
28+
29+
/**
30+
* @param inputFile 输入的CSV文件
31+
* @param outputFile 输出文件
32+
* @param charset 原文件编码格式
33+
* @throws Exception
34+
*/
35+
public static boolean convert(String inputFile, String outputFile, String charset) throws Exception {
36+
if (StringUtils.isBlank(inputFile)) {
37+
System.err.println("原文件不能为空!");
38+
return false;
39+
}
40+
41+
int lastIndexOf = inputFile.lastIndexOf(".");
42+
if (lastIndexOf == -1) {
43+
System.err.println("输入文件必须是.CSV格式的文件!");
44+
return false;
45+
}
46+
String suffix = inputFile.substring(lastIndexOf);
47+
if ("CSV".equalsIgnoreCase(suffix)) {
48+
System.err.println("输入文件必须是.CSV格式的文件!");
49+
return false;
50+
}
51+
52+
// 判断文件是否存在
53+
File input = new File(inputFile);
54+
if (!input.exists()) {
55+
System.err.println("原文件【" + inputFile + "】不存在!");
56+
return false;
57+
}
58+
// 输出文件为空时默认在在当前目录生成同名的.xlsx文件
59+
if (StringUtils.isBlank(outputFile)) {
60+
outputFile = inputFile.subSequence(0, lastIndexOf) + ".xlsx";
61+
}
62+
63+
if (StringUtils.isBlank(charset)) {
64+
charset = DEFAULT_CHARSET;
65+
}
66+
67+
return ConvertCsvToXlsx(inputFile, outputFile, charset);
68+
}
2069

2170
public static void convert(File inputFile) throws Exception {
2271
String sourceFile = inputFile.getAbsolutePath();
2372
String outFile = sourceFile.subSequence(0, sourceFile.lastIndexOf(".")) + ".xlsx";
24-
ConvertCSVToXLS(sourceFile, outFile);
73+
ConvertCsvToXlsx(sourceFile, outFile, DEFAULT_CHARSET);
2574
}
2675

27-
public static void ConvertCSVToXLS(String file, String destFile) throws Exception {
76+
public static boolean ConvertCsvToXlsx(String file, String destFile, String charset) throws Exception {
77+
if (!Charset.isSupported(charset)) {
78+
System.out.println("不支持的字符集:" + charset);
79+
return false;
80+
}
81+
2882
SXSSFWorkbook wb = null;
2983
CSVReader reader = null;
3084
try {
3185
wb = new SXSSFWorkbook(200);
3286
Sheet sh = wb.createSheet();
33-
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), CHARSET));
87+
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
3488
// reader = new CSVReader(new FileReader(file));
3589
reader = new CSVReader(in);
3690

@@ -56,6 +110,7 @@ public static void ConvertCSVToXLS(String file, String destFile) throws Exceptio
56110
wb.write(fileOut);
57111
fileOut.close();
58112
wb.dispose();
113+
59114
} catch (Exception e) {
60115
e.printStackTrace();
61116
throw e;
@@ -67,5 +122,6 @@ public static void ConvertCSVToXLS(String file, String destFile) throws Exceptio
67122
wb.dispose();
68123
}
69124
}
125+
return true;
70126
}
71127
}

0 commit comments

Comments
 (0)