main
黄海 9 months ago
parent e03e3e8b96
commit e925cc3148

@ -352,6 +352,18 @@ public class DataEaseModel {
Db.use(DataEaseModel.DB_NAME).update(sql);
}
public void fillDefaultXzqh(int identity_id,String tableName){
//补全默认行政区划
if (identity_id == 2) {
String sql = "update `" + tableName + "` set `行政区划`='昆明市',`上级行政区划`='云南省' where `行政区划` is null or `上级行政区划` is null";
Db.use(DB_NAME).update(sql);
}
if (identity_id == 3) {
String sql = "update `" + tableName + "` set `行政区划`='寻甸县',`上级行政区划`='昆明市' where `行政区划` is null or `上级行政区划` is null";
Db.use(DB_NAME).update(sql);
}
}
/**
* Excel
*
@ -362,25 +374,21 @@ public class DataEaseModel {
* @throws IOException
*/
public String exportExcel(int identity_id, String tableName, String exportPath, String area_name) throws IOException {
//补全默认行政区划
if (identity_id == 2) {
String sql = "update `" + tableName + "` set `行政区划`='昆明市',`上级行政区划`='云南省' where `行政区划` is null or `上级行政区划` is null";
Db.use(DB_NAME).update(sql);
}
if (identity_id == 3) {
String sql = "update `" + tableName + "` set `行政区划`='寻甸县',`上级行政区划`='昆明市' where `行政区划` is null or `上级行政区划` is null";
Db.use(DB_NAME).update(sql);
}
//填充默认的行政区划
fillDefaultXzqh(identity_id, tableName);
//对此表中的数据进行直接导出EXCEL
String sql = "select * from `" + tableName + "`";
if (identity_id > 1) {
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(tableData, filePath, true);
ExcelCommonUtil.writeExcel(columnNames,tableData, filePath, true);
return filePath;
}

@ -852,81 +852,76 @@ public class ExcelCommonUtil {
* @param filePath
* @throws IOException
*/
public static void writeExcel(List<Record> tableData, String filePath, boolean killIdColumns) throws IOException {
public static void writeExcel(List<String> columnNames, 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");
if(tableData!=null && !tableData.isEmpty() ){
// 获取第一个Record的元数据以确定列名
Record firstRecord = tableData.getFirst();
String[] headers = firstRecord.getColumnNames();
int columnCount = headers.length;
// 创建标题行
Row titleRow = sheet.createRow(0);
// 设置标题行的样式
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setFontHeightInPoints((short) 14);
titleStyle.setFont(font);
titleStyle.setBorderBottom(BorderStyle.THIN);
titleStyle.setBorderLeft(BorderStyle.THIN);
titleStyle.setBorderRight(BorderStyle.THIN);
titleStyle.setBorderTop(BorderStyle.THIN);
// 填充标题行
// 获取第一个Record的元数据以确定列名
int columnCount = columnNames.size();
// 创建标题行
Row titleRow = sheet.createRow(0);
// 设置标题行的样式
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setFontHeightInPoints((short) 14);
titleStyle.setFont(font);
titleStyle.setBorderBottom(BorderStyle.THIN);
titleStyle.setBorderLeft(BorderStyle.THIN);
titleStyle.setBorderRight(BorderStyle.THIN);
titleStyle.setBorderTop(BorderStyle.THIN);
// 填充标题行
for (int i = 1; i <= columnCount; i++) {
Cell cell = titleRow.createCell(i - 1);
cell.setCellValue(columnNames.get(i - 1));
cell.setCellStyle(titleStyle);
}
// 创建数据行的样式
CellStyle dataStyle = workbook.createCellStyle();
font = workbook.createFont();
font.setFontHeightInPoints((short) 14);
dataStyle.setFont(font);
dataStyle.setBorderBottom(BorderStyle.THIN);
dataStyle.setBorderLeft(BorderStyle.THIN);
dataStyle.setBorderRight(BorderStyle.THIN);
dataStyle.setBorderTop(BorderStyle.THIN);
// 填充数据行
int rowNum = 1;
for (Record record : tableData) {
Row row = sheet.createRow(rowNum++);
int cellNum = 0;
for (int i = 1; i <= columnCount; i++) {
Cell cell = titleRow.createCell(i - 1);
cell.setCellValue(headers[i - 1]);
cell.setCellStyle(titleStyle);
}
// 创建数据行的样式
CellStyle dataStyle = workbook.createCellStyle();
font = workbook.createFont();
font.setFontHeightInPoints((short) 14);
dataStyle.setFont(font);
dataStyle.setBorderBottom(BorderStyle.THIN);
dataStyle.setBorderLeft(BorderStyle.THIN);
dataStyle.setBorderRight(BorderStyle.THIN);
dataStyle.setBorderTop(BorderStyle.THIN);
// 填充数据行
int rowNum = 1;
for (Record record : tableData) {
Row row = sheet.createRow(rowNum++);
int cellNum = 0;
for (int i = 1; i <= columnCount; i++) {
Cell cell = row.createCell(cellNum++);
Object value = record.get(headers[i - 1]);
cell.setCellValue(value == null ? "" : value.toString());
cell.setCellStyle(dataStyle);
}
Cell cell = row.createCell(cellNum++);
Object value = record.get(columnNames.get(i - 1));
cell.setCellValue(value == null ? "" : value.toString());
cell.setCellStyle(dataStyle);
}
}
// 设置最小列宽
int minColumnWidth = 100 * 256 / 7; // 将像素转换为字符单位Excel的单位是1/256个字符宽度
for (int i = 0; i < headers.length; i++) {
sheet.setColumnWidth(i, Math.max(minColumnWidth, sheet.getColumnWidth(i)));
}
// 设置最小列宽
int minColumnWidth = 100 * 256 / 7; // 将像素转换为字符单位Excel的单位是1/256个字符宽度
for (int i = 0; i < columnNames.size(); i++) {
sheet.setColumnWidth(i, Math.max(minColumnWidth, sheet.getColumnWidth(i)));
}
// 自动调整列宽
for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i);
int columnWidth = sheet.getColumnWidth(i);
int newColumnWidth = Math.max(minColumnWidth, columnWidth);
sheet.setColumnWidth(i, newColumnWidth);
}
// 自动调整列宽
for (int i = 0; i < columnNames.size(); i++) {
sheet.autoSizeColumn(i);
int columnWidth = sheet.getColumnWidth(i);
int newColumnWidth = Math.max(minColumnWidth, columnWidth);
sheet.setColumnWidth(i, newColumnWidth);
}
// 将工作簿写入文件

Loading…
Cancel
Save