parent
ce2695a177
commit
9387daf1a9
@ -1,133 +0,0 @@
|
||||
package com.dsideal.base.Tools;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
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 {
|
||||
// 创建一个新的Excel工作簿
|
||||
Workbook workbook = new XSSFWorkbook();
|
||||
// 创建一个工作表
|
||||
Sheet sheet = workbook.createSheet("查询结果");
|
||||
|
||||
// 创建表头单元格
|
||||
Row headerRow = sheet.createRow(0);
|
||||
ResultSetMetaData metaData = resultSet.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
|
||||
// 设置表头单元格样式
|
||||
CellStyle headerStyle = workbook.createCellStyle();
|
||||
headerStyle.setBorderTop(BorderStyle.THIN);
|
||||
headerStyle.setBorderBottom(BorderStyle.THIN);
|
||||
headerStyle.setBorderLeft(BorderStyle.THIN);
|
||||
headerStyle.setBorderRight(BorderStyle.THIN);
|
||||
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
|
||||
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
headerStyle.setFont(workbook.createFont());
|
||||
//headerStyle.getFont().setBold(true);
|
||||
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
Cell cell = headerRow.createCell(i - 1);
|
||||
cell.setCellValue(metaData.getColumnName(i));
|
||||
cell.setCellStyle(headerStyle);
|
||||
}
|
||||
|
||||
headerRow.setHeightInPoints(28); // 设置表头行高为28
|
||||
|
||||
// 填充数据
|
||||
int rowNumber = 1;
|
||||
while (resultSet.next()) {
|
||||
Row row = sheet.createRow(rowNumber++);
|
||||
row.setHeightInPoints(28); // 设置数据行高为28
|
||||
|
||||
// 设置数据单元格样式
|
||||
CellStyle dataStyle = workbook.createCellStyle();
|
||||
dataStyle.setBorderTop(BorderStyle.THIN);
|
||||
dataStyle.setBorderBottom(BorderStyle.THIN);
|
||||
dataStyle.setBorderLeft(BorderStyle.THIN);
|
||||
dataStyle.setBorderRight(BorderStyle.THIN);
|
||||
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
Cell cell = row.createCell(i - 1);
|
||||
Object value = resultSet.getObject(i);
|
||||
if (value != null) cell.setCellValue(value.toString());
|
||||
cell.setCellStyle(dataStyle);
|
||||
}
|
||||
}
|
||||
|
||||
// 自动调整列宽
|
||||
for (int i = 0; i < columnCount; i++) {
|
||||
sheet.autoSizeColumn(i);
|
||||
}
|
||||
|
||||
// 将工作簿写入文件
|
||||
try (FileOutputStream fileOut = new FileOutputStream(excelFilePath)) {
|
||||
workbook.write(fileOut);
|
||||
}
|
||||
// 关闭工作簿和结果集
|
||||
workbook.close();
|
||||
resultSet.close();
|
||||
}
|
||||
|
||||
public static List<String> getFiles(String path) {
|
||||
List<String> files = new ArrayList<>();
|
||||
File file = new File(path);
|
||||
File[] tempList = file.listFiles();
|
||||
|
||||
for (int i = 0; i < tempList.length; i++) {
|
||||
if (tempList[i].isFile()) {
|
||||
files.add(tempList[i].toString());
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ClassNotFoundException, SQLException {
|
||||
// 指定目录路径
|
||||
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";
|
||||
|
||||
// 加载SQLite的JDBC驱动
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
|
||||
// 建立连接
|
||||
Connection connection = DriverManager.getConnection(url);
|
||||
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 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("全部成功!");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package com.dsideal.base.Tools;
|
||||
|
||||
import com.dsideal.base.Plugin.YamlProp;
|
||||
import com.jfinal.kit.Prop;
|
||||
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
||||
import com.jfinal.plugin.activerecord.Db;
|
||||
import com.jfinal.plugin.activerecord.Record;
|
||||
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
|
||||
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public class addDataEaseExcelPrimary {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
//加载配置文件
|
||||
String configFile = "application_dev.yaml";
|
||||
Prop PropKit = new YamlProp(configFile);
|
||||
HikariCpPlugin arpPlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl"), PropKit.get("mysql.user"),
|
||||
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
|
||||
arpPlugin.start();
|
||||
|
||||
HikariCpPlugin dataEasePlugin = new HikariCpPlugin(PropKit.get("mysql.jdbcUrl").replace("ds_db", "dataease"), PropKit.get("mysql.user"),
|
||||
PropKit.get("mysql.password").trim(), PropKit.get("mysql.driverClassName"));
|
||||
dataEasePlugin.start();
|
||||
|
||||
// 配置ActiveRecord插件
|
||||
ActiveRecordPlugin arp = new ActiveRecordPlugin("master", arpPlugin);
|
||||
arp.setDialect(new MysqlDialect());
|
||||
|
||||
ActiveRecordPlugin arpDataEase = new ActiveRecordPlugin("dataease", dataEasePlugin);
|
||||
arpDataEase.setDialect(new MysqlDialect());
|
||||
|
||||
//遍历sql目录下所有的sql文件
|
||||
File sqlDir;
|
||||
String basePath = addDataEaseExcelPrimary.class.getResource("/").getPath();
|
||||
sqlDir = new File(basePath + "/Sql");
|
||||
File[] sqlFiles = sqlDir.listFiles();
|
||||
for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) {
|
||||
//只加载.sql文件
|
||||
if (sqlFile.getName().indexOf(".sql") > 0) {
|
||||
arp.addSqlTemplate("/Sql/" + sqlFile.getName());
|
||||
arpDataEase.addSqlTemplate("/Sql/" + sqlFile.getName());
|
||||
}
|
||||
}
|
||||
arp.start();
|
||||
arpDataEase.start();
|
||||
|
||||
//处理表,增加列
|
||||
// 查询所有以 excel_ 开头的表
|
||||
List<Record> tables = Db.use("dataease").find("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dataease' AND TABLE_NAME LIKE 'excel\\_%'");
|
||||
|
||||
for (Record table : tables) {
|
||||
String tableName = table.getStr("TABLE_NAME");
|
||||
if (!hasPrimaryKey(tableName)) {
|
||||
addPrimaryKeyAndTimestamp(tableName);
|
||||
System.out.println("Processed table: " + tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 检查表是否存在主键
|
||||
public static boolean hasPrimaryKey(String tableName) {
|
||||
String sql = "SELECT COUNT(*) as c FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND CONSTRAINT_TYPE = 'PRIMARY KEY'";
|
||||
return Db.use("dataease").queryInt(sql, "dataease", tableName) > 0;
|
||||
}
|
||||
|
||||
// 添加主键列和时间戳列,并设置为主键
|
||||
public static void addPrimaryKeyAndTimestamp(String tableName) {
|
||||
// 添加 id 列和 update_ts 列
|
||||
String alterTableSql = "ALTER TABLE `" + tableName + "` ADD COLUMN `id` CHAR(36) NULL, ADD COLUMN `update_ts` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP";
|
||||
Db.use("dataease").update(alterTableSql);
|
||||
|
||||
|
||||
// 生成 UUID 并更新 id 列和 update_ts 列
|
||||
String updateSql = "UPDATE `" + tableName + "` SET `id` = UUID(), `update_ts` = CURRENT_TIMESTAMP";
|
||||
Db.use("dataease").update(updateSql);
|
||||
|
||||
//设置主键
|
||||
String sql="ALTER TABLE `" + tableName +"` MODIFY COLUMN `id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,ADD PRIMARY KEY (`ID`)";
|
||||
System.out.println(sql);
|
||||
Db.use("dataease").update(sql);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue