parent
bb12c397a7
commit
eb32dc24c5
@ -0,0 +1,130 @@
|
||||
package UnitTest;
|
||||
|
||||
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
|
||||
import com.jfinal.plugin.activerecord.Db;
|
||||
import com.jfinal.plugin.activerecord.Record;
|
||||
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
|
||||
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TestExportExcel {
|
||||
public static void main(String[] args) {
|
||||
//读取库
|
||||
HikariCpPlugin hp = new HikariCpPlugin("jdbc:postgresql://10.10.14.71:5432/szjz_db", "postgres",
|
||||
"DsideaL147258369", "org.postgresql.Driver");
|
||||
hp.start();
|
||||
|
||||
ActiveRecordPlugin arp = new ActiveRecordPlugin(hp);
|
||||
arp.setDialect(new PostgreSqlDialect());
|
||||
arp.start();
|
||||
//表名
|
||||
String table_name = "ds_job_5_0";
|
||||
//读取表的结构信息
|
||||
String sql = "select column_name,excel_column_idx,original_name from t_collect_mapping where table_name= ?";
|
||||
List<Record> list = Db.find(sql, table_name);
|
||||
Map<Integer, Record> _map = new HashMap<>();
|
||||
for (Record record : list) {
|
||||
Record r = new Record();
|
||||
r.set("column_name", record.getStr("column_name"));
|
||||
r.set("original_name", record.getStr("original_name"));
|
||||
_map.put(record.getInt("excel_column_idx") + 1, r);
|
||||
}
|
||||
//读取表的注释
|
||||
sql = "SELECT obj_description(?::regclass, 'pg_class') as memo";
|
||||
String memo = Db.findFirst(sql, table_name).getStr("memo");
|
||||
|
||||
//获取数据
|
||||
sql = "select t1.*,t2.org_name as bureau_name from " + table_name + " as t1 inner join t_base_organization as t2 on t1.bureau_id=t2.bureau_id";
|
||||
List<Record> data = Db.find(sql);
|
||||
|
||||
|
||||
// 创建工作簿和工作表
|
||||
Workbook workbook = new XSSFWorkbook();
|
||||
Sheet sheet = workbook.createSheet(memo);
|
||||
|
||||
// 创建单元格样式对象
|
||||
CellStyle headerStyle = workbook.createCellStyle();
|
||||
// 设置水平居中
|
||||
headerStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
// 设置垂直居中
|
||||
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
// 设置边框样式
|
||||
headerStyle.setBorderTop(BorderStyle.THIN);
|
||||
headerStyle.setBorderBottom(BorderStyle.THIN);
|
||||
headerStyle.setBorderLeft(BorderStyle.THIN);
|
||||
headerStyle.setBorderRight(BorderStyle.THIN);
|
||||
|
||||
// 创建字体对象并设置字体颜色为白色
|
||||
Font font = workbook.createFont();
|
||||
font.setFontHeightInPoints((short) 14);
|
||||
font.setBold(true);
|
||||
font.setFontName("黑体");
|
||||
font.setColor(IndexedColors.BLACK.getIndex());
|
||||
// 设置背景色为浅蓝色 #d2f4f2
|
||||
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
|
||||
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
// 将字体应用于样式
|
||||
headerStyle.setFont(font);
|
||||
|
||||
// 在第一行创建单元格并设置样式
|
||||
Row headerRow = sheet.createRow(0);
|
||||
Cell headerCell = headerRow.createCell(0);
|
||||
headerCell.setCellValue("单位");
|
||||
headerCell.setCellStyle(headerStyle);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
headerCell = headerRow.createCell(i + 1);
|
||||
headerCell.setCellValue(_map.get(i + 1).getStr("original_name"));
|
||||
headerCell.setCellStyle(headerStyle);
|
||||
}
|
||||
|
||||
// 创建字体对象并设置字体大小为12
|
||||
font = workbook.createFont();
|
||||
font.setFontHeightInPoints((short) 12);
|
||||
|
||||
// 创建单元格样式对象并将字体应用于样式
|
||||
CellStyle style = workbook.createCellStyle();
|
||||
style.setFont(font);
|
||||
// 设置行高度为28
|
||||
sheet.setDefaultRowHeightInPoints(28);
|
||||
// 设置每个单元格的宽度为30
|
||||
sheet.setDefaultColumnWidth(30);
|
||||
// 设置边框样式
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
// 设置水平居中
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
// 设置垂直居中
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
|
||||
// 填充数据集的其余行
|
||||
for (int i = 1; i <= data.size(); i++) {
|
||||
Row row = sheet.createRow(i);
|
||||
Record record = data.get(i - 1);
|
||||
Cell cell = row.createCell(0);
|
||||
cell.setCellValue(record.getStr("bureau_name"));
|
||||
cell.setCellStyle(style);
|
||||
for (int j = 0; j < list.size(); j++) {
|
||||
cell = row.createCell(j + 1);
|
||||
cell.setCellValue(record.getStr(_map.get(j + 1).getStr("column_name")));
|
||||
cell.setCellStyle(style);
|
||||
}
|
||||
}
|
||||
|
||||
// 保存Excel文件
|
||||
try (FileOutputStream outputStream = new FileOutputStream("c:/example.xlsx")) {
|
||||
workbook.write(outputStream);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue