main
黄海 9 months ago
parent 72780d342e
commit efc039cf69

@ -256,7 +256,9 @@ public class DataEaseModel {
public List<String> getColumns(String tableName) {
String sql = "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT, IS_NULLABLE, COLUMN_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, DATETIME_PRECISION, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?";
List<Record> list = Db.find(sql, DataEaseModel.DB_NAME, tableName);
return list.stream().map(record -> record.getStr("COLUMN_NAME")).collect(Collectors.toList());
List<String> res = list.stream().map(record -> record.getStr("COLUMN_NAME")).collect(Collectors.toList());
System.out.println(res);
return res;
}
// 检查表是否存在主键
@ -448,13 +450,13 @@ public class DataEaseModel {
sql = sql + "where `行政区划`='" + area_name + "'";
}
//获取一下表有哪些列
List<String> columnNames = getColumns(tableName);
//List<String> columnNames = getColumns(tableName);
List<Record> tableData = Db.use(DataEaseModel.DB_NAME).find(sql);
String excelFileName = UUID.randomUUID().toString().toUpperCase() + ".xlsx";
String filePath = exportPath + excelFileName;
//导出
ExcelCommonUtil.writeExcel(columnNames, tableData, filePath, true);
ExcelCommonUtil.writeExcel( tableData, filePath, true);
return filePath;
}
@ -680,14 +682,12 @@ public class DataEaseModel {
//对此表中的数据进行直接导出EXCEL
String sql = "select * from `" + tableName + "`";
sql = sql + "where `上级行政区划`='" + area_name + "'";
//获取一下表有哪些列
List<String> columnNames = getColumns(tableName);
List<Record> tableData = Db.use(DataEaseModel.DB_NAME).find(sql);
String excelFileName = UUID.randomUUID().toString().toUpperCase() + ".xlsx";
String filePath = exportPath + excelFileName;
//导出
ExcelCommonUtil.writeExcel(columnNames, tableData, filePath, true);
ExcelCommonUtil.writeExcel(tableData, filePath, true);
return filePath;
}

@ -852,21 +852,19 @@ public class ExcelCommonUtil {
* @param filePath
* @throws IOException
*/
public static void writeExcel(List<String> columnNames, List<Record> tableData, String filePath, boolean killIdColumns) throws IOException {
public static void writeExcel(List<Record> tableData, String filePath, boolean killIdColumns) throws IOException {
if (killIdColumns) {
//表tableData中如果有一列叫id,那么去掉这一列
for (Record tableDatum : tableData) {
tableDatum.remove("id");
}
columnNames.remove("id");
}
// 创建Excel工作簿
Workbook workbook = new XSSFWorkbook();
// 创建一个Excel工作表
Sheet sheet = workbook.createSheet("Sheet1");
// 获取第一个Record的元数据以确定列名
int columnCount = columnNames.size();
int columnCount = tableData.getFirst().size();
// 创建标题行
Row titleRow = sheet.createRow(0);
// 设置标题行的样式
@ -883,7 +881,7 @@ public class ExcelCommonUtil {
// 填充标题行
for (int i = 1; i <= columnCount; i++) {
Cell cell = titleRow.createCell(i - 1);
cell.setCellValue(columnNames.get(i - 1));
cell.setCellValue(tableData.getFirst().getColumnNames()[i - 1]);
cell.setCellStyle(titleStyle);
}
@ -904,7 +902,7 @@ public class ExcelCommonUtil {
int cellNum = 0;
for (int i = 1; i <= columnCount; i++) {
Cell cell = row.createCell(cellNum++);
Object value = record.get(columnNames.get(i - 1));
Object value = record.get(tableData.getFirst().getColumnNames()[i - 1]);
cell.setCellValue(value == null ? "" : value.toString());
cell.setCellStyle(dataStyle);
}
@ -912,12 +910,12 @@ public class ExcelCommonUtil {
// 设置最小列宽
int minColumnWidth = 100 * 256 / 7; // 将像素转换为字符单位Excel的单位是1/256个字符宽度
for (int i = 0; i < columnNames.size(); i++) {
for (int i = 0; i < tableData.getFirst().getColumnNames().length; i++) {
sheet.setColumnWidth(i, Math.max(minColumnWidth, sheet.getColumnWidth(i)));
}
// 自动调整列宽
for (int i = 0; i < columnNames.size(); i++) {
for (int i = 0; i < tableData.getFirst().getColumnNames().length; i++) {
sheet.autoSizeColumn(i);
int columnWidth = sheet.getColumnWidth(i);
int newColumnWidth = Math.max(minColumnWidth, columnWidth);

Loading…
Cancel
Save