|
|
|
@ -8,8 +8,10 @@ import java.io.IOException;
|
|
|
|
|
import java.sql.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
|
|
public class GenerateExcel {
|
|
|
|
|
|
|
|
|
|
public static void exportResultSetToExcel(ResultSet resultSet, String excelFilePath) throws SQLException, IOException {
|
|
|
|
@ -76,6 +78,7 @@ public class GenerateExcel {
|
|
|
|
|
workbook.close();
|
|
|
|
|
resultSet.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<String> getFiles(String path) {
|
|
|
|
|
List<String> files = new ArrayList<>();
|
|
|
|
|
File file = new File(path);
|
|
|
|
@ -91,7 +94,7 @@ public class GenerateExcel {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws ClassNotFoundException, SQLException {
|
|
|
|
|
// 指定目录路径
|
|
|
|
|
String directoryPath = "C:\\Users\\Administrator\\Desktop\\DataBase\\Sql";
|
|
|
|
|
String directoryPath = "C:\\Users\\Administrator\\Documents\\Navicat\\SQLite\\Servers\\长春市教育数据统计2024\\main";
|
|
|
|
|
// SQLite数据库的URL
|
|
|
|
|
String url = "jdbc:sqlite:C:/Users/Administrator/Desktop/DataBase/edudb_gather_220100000000_full_fullreport.db";
|
|
|
|
|
|
|
|
|
@ -103,19 +106,28 @@ public class GenerateExcel {
|
|
|
|
|
Statement statement = connection.createStatement();
|
|
|
|
|
// 遍历所有文件
|
|
|
|
|
List<String> files = getFiles(directoryPath);
|
|
|
|
|
|
|
|
|
|
int cnt = 0;//有问题的查询个数
|
|
|
|
|
for (String file : files) {
|
|
|
|
|
if(!file.contains(".sql")) continue;
|
|
|
|
|
String sql = FileUtil.readUtf8String(file);
|
|
|
|
|
try {
|
|
|
|
|
String excelFilePath = file.replace(".sql", ".xlsx");
|
|
|
|
|
ResultSet resultSet = statement.executeQuery(sql);
|
|
|
|
|
exportResultSetToExcel(resultSet, excelFilePath);
|
|
|
|
|
|
|
|
|
|
System.out.println("数据已导出到Excel文件:" + excelFilePath);
|
|
|
|
|
}catch (Exception err){
|
|
|
|
|
System.out.println(file+" "+err);
|
|
|
|
|
ResultSet rs = statement.executeQuery(sql);
|
|
|
|
|
//如果数据集是空的,返回
|
|
|
|
|
if(!rs.isBeforeFirst()) continue;
|
|
|
|
|
//否则导出
|
|
|
|
|
cnt++;
|
|
|
|
|
exportResultSetToExcel(rs, excelFilePath);
|
|
|
|
|
} catch (Exception err) {
|
|
|
|
|
System.out.println(err);
|
|
|
|
|
System.out.println(file + "无法执行,请检查原因!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
connection.close();
|
|
|
|
|
if (cnt > 0) {
|
|
|
|
|
System.out.println("导出问题表" + cnt + "个!");
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("全部成功!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|