|
|
|
@ -0,0 +1,121 @@
|
|
|
|
|
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);
|
|
|
|
|
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\\Desktop\\DataBase\\Sql";
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
for (String file : files) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|