main
黄海 2 years ago
parent 5b97799d69
commit c5b32563ac

@ -5,11 +5,30 @@ 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.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFColor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestExportExcel {
/**
*
*
* @param table_name
* @return
*/
public static List<Record> getTableStructInfo(String table_name) {
String sql = "select col.column_name, col.ordinal_position as o, d.description as column_description from information_schema.columns col join pg_class c on c.relname = col.table_name left join pg_description d on d.objoid = c.oid and d.objsubid = col.ordinal_position where col.table_schema = 'public' and table_name=? order by col.ordinal_position";
return Db.find(sql, table_name);
}
public static void main(String[] args) throws IOException {
//读取库
HikariCpPlugin hp = new HikariCpPlugin("jdbc:postgresql://10.10.14.71:5432/szjz_db", "postgres",
@ -24,10 +43,115 @@ public class TestExportExcel {
String sql = "select * from t_collect_job where job_id=?";
Record record = Db.findFirst(sql, job_id);
String job_name = record.getStr("job_name");
int job_type = record.getInt("job_type");//任务类型1表单2EXCEL模板
String table_name = record.getStr("form_table_name");//表格名称
if (job_type == 1) {
System.out.println(table_name);
Map<Integer, Record> _map = new HashMap<>();
List<Record> listStruct = getTableStructInfo(table_name);
int idx = 0;
for (Record r : listStruct) {
String column_name = r.getStr("column_name");
//准备第几列用哪一个字段
if (column_name.equals("id")) continue;
if (column_name.equals("bureau_id")) continue;
if (column_name.equals("person_id")) continue;
if (column_name.equals("class_id")) continue;
if (column_name.equals("job_id")) continue;
_map.put(idx++, r);
}
//获取数据
sql = "select * from " + table_name + " where job_id=?";
List<Record> data = Db.find(sql, job_id);
//创建Excel
SXSSFWorkbook workbook = new SXSSFWorkbook();//默认100行超100行将写入临时文件
workbook.setCompressTempFiles(false); //是否压缩临时文件,否则写入速度更快,但更占磁盘,但程序最后是会将临时文件删掉的
//开始生成EXCEL
String sheet_name = "填报情况";
//获取数据
Sheet sheet = workbook.createSheet(sheet_name);
// 创建单元格样式对象
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
//颜色
String str = "#d2f4f2";
String sr = str.substring(1, 3);
String sg = str.substring(3, 5);
String sb = str.substring(5, 7);
//16进制的字符串转为int
int r = Integer.parseInt(sr, 16);
int g = Integer.parseInt(sg, 16);
int b = Integer.parseInt(sb, 16);
XSSFColor rbg = new XSSFColor(new java.awt.Color(r, g, b), new DefaultIndexedColorMap());
headerStyle.setFillForegroundColor(rbg);
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 将字体应用于样式
headerStyle.setFont(font);
// 在第一行创建单元格并设置样式
Row headerRow = sheet.createRow(0);
Cell headerCell = headerRow.createCell(0);
headerCell.setCellStyle(headerStyle);
for (int i = 0; i < _map.size(); i++) {
headerCell = headerRow.createCell(i);
headerCell.setCellValue(_map.get(i).getStr("column_description"));
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 r2 = data.get(i - 1);
for (int j = 0; j < _map.size(); j++) {
Cell cell = row.createCell(j);
cell.setCellValue(r2.getStr(_map.get(j).getStr("column_name")));
cell.setCellStyle(style);
}
}
String filePath = "c:/2.xlsx";
// 保存Excel文件
FileOutputStream outputStream = new FileOutputStream(filePath);
workbook.write(outputStream);
//
System.out.println("恭喜,所有操作成功完成!");
} else {
System.out.println("不是 Form填报任务无法执行");

Loading…
Cancel
Save