Skip to content

Commit 2e74246

Browse files
author
pengyu
committed
增加输出类型:excel文件
1 parent 587ff71 commit 2e74246

File tree

4 files changed

+79
-18
lines changed

4 files changed

+79
-18
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ build/
3232

3333
output*
3434
*.log
35+
*.jar
36+
application.properties

application.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

pom.xml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>py</groupId>
1212
<artifactId>java-sql-cli</artifactId>
13-
<version>1.1</version>
13+
<version>1.2</version>
1414
<name>sql client written in java</name>
1515
<description>sql helper</description>
1616

@@ -34,10 +34,15 @@
3434
<artifactId>spring-shell-starter</artifactId>
3535
<version>2.0.1.BUILD-SNAPSHOT</version>
3636
</dependency>
37+
<!-- <dependency>-->
38+
<!-- <groupId>ojdbc6</groupId>-->
39+
<!-- <artifactId>ojdbc6</artifactId>-->
40+
<!-- <version>11.2.0.4.0</version>-->
41+
<!-- </dependency>-->
3742
<dependency>
38-
<groupId>ojdbc6</groupId>
43+
<groupId>oracle</groupId>
3944
<artifactId>ojdbc6</artifactId>
40-
<version>11.2.0.4.0</version>
45+
<version>19.3</version>
4146
</dependency>
4247
<dependency>
4348
<groupId>mysql</groupId>
@@ -47,7 +52,12 @@
4752
<dependency>
4853
<groupId>com.alibaba</groupId>
4954
<artifactId>fastjson</artifactId>
50-
<version>1.2.57</version>
55+
<version>1.2.72</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.apache.poi</groupId>
59+
<artifactId>poi-ooxml</artifactId>
60+
<version>3.17</version>
5161
</dependency>
5262
</dependencies>
5363

src/main/java/py/sqlcli/SqlCliCommands.java

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import com.alibaba.fastjson.JSONArray;
44
import com.alibaba.fastjson.JSONObject;
5+
import org.apache.poi.xssf.usermodel.XSSFCell;
6+
import org.apache.poi.xssf.usermodel.XSSFRow;
7+
import org.apache.poi.xssf.usermodel.XSSFSheet;
8+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
59
import org.springframework.beans.factory.annotation.Value;
610
import org.springframework.shell.standard.ShellComponent;
711
import org.springframework.shell.standard.ShellMethod;
@@ -10,15 +14,11 @@
1014

1115
import javax.annotation.PostConstruct;
1216
import javax.annotation.PreDestroy;
13-
import java.io.File;
14-
import java.io.FileWriter;
15-
import java.io.IOException;
16-
import java.io.PrintWriter;
17+
import java.io.*;
1718
import java.sql.*;
18-
import java.util.ArrayList;
19-
import java.util.HashMap;
20-
import java.util.List;
21-
import java.util.Map;
19+
import java.text.SimpleDateFormat;
20+
import java.util.Date;
21+
import java.util.*;
2222

2323
/**
2424
* @author pengyu
@@ -72,11 +72,12 @@ public void init() {
7272
@ShellMethod("execute sql")
7373
public String exec(
7474
@ShellOption() String sql,
75-
@ShellOption() boolean json) {
75+
@ShellOption() boolean json,
76+
@ShellOption() boolean excel) {
7677

7778
sql = sql.trim();
7879
if (StringUtils.startsWithIgnoreCase(sql, "select")) {
79-
return execQuery(sql, json);
80+
return execQuery(sql, json, excel);
8081
} else {
8182
return execUpdate(sql);
8283
}
@@ -86,14 +87,14 @@ public String exec(
8687
private String execUpdate(String sql) {
8788
try {
8889
int rows = stmt.executeUpdate(sql);
89-
return "rows: "+rows;
90+
return "rows: " + rows;
9091
} catch (SQLException e) {
9192
e.printStackTrace();
9293
return "error: " + e.getMessage();
9394
}
9495
}
9596

96-
private String execQuery(String sql, boolean json) {
97+
private String execQuery(String sql, boolean json, boolean excel) {
9798
try (ResultSet rs = stmt.executeQuery(sql)) {
9899
final ResultSetMetaData metaData = rs.getMetaData();
99100
final int columnCount = metaData.getColumnCount();
@@ -113,6 +114,8 @@ private String execQuery(String sql, boolean json) {
113114
final String output;
114115
if (json) {
115116
output = createJsonString(columnNames, rows);
117+
} else if (excel) {
118+
output = createExcelFile(columnNames, rows);
116119
} else {
117120
final Table table = createTable(columnNames, rows);
118121
output = table.toString();
@@ -156,7 +159,54 @@ private String createJsonString(String[] columnNames, List<String[]> rows) {
156159
}
157160
jsonArray.add(field);
158161
}
159-
return JSONArray.toJSONString(jsonArray, true);
162+
return "";
163+
}
164+
165+
private String createExcelFile(String[] columnNames, List<String[]> rows) {
166+
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");
167+
String filename = sdf.format(new Date());
168+
final String pathname = "/tmp/java-sql-cli/";
169+
filename = pathname + filename + ".xlsx";
170+
XSSFWorkbook workbook = new XSSFWorkbook();
171+
XSSFSheet sheet = workbook.createSheet("Result");
172+
final XSSFRow firstRow = sheet.createRow(0);
173+
// 表头
174+
for (int i = 0; i < columnNames.length; i++) {
175+
final XSSFCell cell = firstRow.createCell(i);
176+
cell.setCellValue(columnNames[i]);
177+
}
178+
179+
for (int i = 0; i < rows.size(); i++) {
180+
final XSSFRow row = sheet.createRow(i + 1);
181+
for (int j = 0; j < columnNames.length; j++) {
182+
final XSSFCell cell = row.createCell(j);
183+
cell.setCellValue(rows.get(i)[j]);
184+
}
185+
}
186+
File path = new File(pathname);
187+
if (!path.exists()) {
188+
final boolean ok = path.mkdirs();
189+
if (!ok) {
190+
return "Make Directories Error: " + pathname;
191+
}
192+
}
193+
File file = new File(filename);
194+
try {
195+
final boolean ok = file.createNewFile();
196+
if (!ok) {
197+
return "Create File Error: " + filename;
198+
}
199+
} catch (IOException e) {
200+
e.printStackTrace();
201+
return "Create File Error: " + filename;
202+
}
203+
try (FileOutputStream outputStream = new FileOutputStream(file)) {
204+
workbook.write(outputStream);
205+
return "Written to file: " + filename;
206+
} catch (IOException e) {
207+
e.printStackTrace();
208+
return "Write data error: " + filename;
209+
}
160210
}
161211

162212
private void appendFile(String sql, String result) throws IOException {

0 commit comments

Comments
 (0)