|
|
|
@ -12,6 +12,10 @@ import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
@ -60,17 +64,17 @@ public class ExcelExportUtil {
|
|
|
|
|
page = new Page(list, page.getPageNumber(), page.getPageSize(), page.getTotalPage(), page.getTotalRow());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
|
|
|
|
|
HSSFSheet hssfSheet = hssfWorkbook.createSheet(sheetName);
|
|
|
|
|
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
|
|
|
|
|
XSSFSheet xssfSheet = xssfWorkbook.createSheet(sheetName);
|
|
|
|
|
int count = 0;
|
|
|
|
|
HSSFRow row = hssfSheet.createRow(count++);
|
|
|
|
|
HSSFCell cell;
|
|
|
|
|
XSSFRow row = xssfSheet.createRow(count++);
|
|
|
|
|
XSSFCell cell;
|
|
|
|
|
|
|
|
|
|
//设置标题字体
|
|
|
|
|
Font fontTitle = hssfWorkbook.createFont();
|
|
|
|
|
Font fontTitle = xssfWorkbook.createFont();
|
|
|
|
|
fontTitle.setFontHeightInPoints((short) 18); //字体大小
|
|
|
|
|
fontTitle.setFontName("黑体"); //字体
|
|
|
|
|
CellStyle cellStyleTitle = hssfWorkbook.createCellStyle();
|
|
|
|
|
CellStyle cellStyleTitle = xssfWorkbook.createCellStyle();
|
|
|
|
|
cellStyleTitle.setFont(fontTitle);
|
|
|
|
|
cellStyleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
cellStyleTitle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
@ -82,15 +86,15 @@ public class ExcelExportUtil {
|
|
|
|
|
//合并前N列,写上标题
|
|
|
|
|
CellRangeAddress region = new CellRangeAddress(0, 0, 0, colInfo.size() - 1);// 下标从0开始 起始行号,终止行号, 起始列号,终止列号
|
|
|
|
|
//在sheet里增加合并单元格
|
|
|
|
|
hssfSheet.addMergedRegion(region);
|
|
|
|
|
xssfSheet.addMergedRegion(region);
|
|
|
|
|
//设置标题的高度
|
|
|
|
|
row.setHeight(titleHeight);
|
|
|
|
|
|
|
|
|
|
Font txtFont = hssfWorkbook.createFont();
|
|
|
|
|
Font txtFont = xssfWorkbook.createFont();
|
|
|
|
|
txtFont.setFontHeightInPoints((short) 14); //字体大小
|
|
|
|
|
txtFont.setFontName("宋体"); //字体
|
|
|
|
|
txtFont.setBold(true);
|
|
|
|
|
CellStyle cellStyleTxt = hssfWorkbook.createCellStyle();
|
|
|
|
|
CellStyle cellStyleTxt = xssfWorkbook.createCellStyle();
|
|
|
|
|
cellStyleTxt.setFont(txtFont);
|
|
|
|
|
cellStyleTxt.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
cellStyleTxt.setFillForegroundColor(IndexedColors.LIME.getIndex());
|
|
|
|
@ -102,10 +106,8 @@ public class ExcelExportUtil {
|
|
|
|
|
cellStyleTxt.setBorderTop(BorderStyle.THIN); // 上边边框
|
|
|
|
|
cellStyleTxt.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
|
|
cellStyleTxt.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//写入表头
|
|
|
|
|
row = hssfSheet.createRow(count++);
|
|
|
|
|
row = xssfSheet.createRow(count++);
|
|
|
|
|
for (int i = 0; i < colInfo.size(); i++) {
|
|
|
|
|
//创建传入进来的表头的个数
|
|
|
|
|
cell = row.createCell(i);
|
|
|
|
@ -116,13 +118,13 @@ public class ExcelExportUtil {
|
|
|
|
|
cell.setCellValue(jsonObject2.getString("show_column_name"));
|
|
|
|
|
cell.setCellStyle(cellStyleTxt);
|
|
|
|
|
//调转宽度
|
|
|
|
|
hssfSheet.setColumnWidth(i, 256 * width + 184);
|
|
|
|
|
xssfSheet.setColumnWidth(i, 256 * width + 184);
|
|
|
|
|
}
|
|
|
|
|
//正文与表头不是一个颜色
|
|
|
|
|
txtFont = hssfWorkbook.createFont();
|
|
|
|
|
txtFont = xssfWorkbook.createFont();
|
|
|
|
|
txtFont.setFontHeightInPoints((short) 14); //字体大小
|
|
|
|
|
txtFont.setFontName("宋体"); //字体
|
|
|
|
|
cellStyleTxt = hssfWorkbook.createCellStyle();
|
|
|
|
|
cellStyleTxt = xssfWorkbook.createCellStyle();
|
|
|
|
|
cellStyleTxt.setFont(txtFont);
|
|
|
|
|
cellStyleTxt.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
cellStyleTxt.setFillForegroundColor(IndexedColors.LIME.getIndex());
|
|
|
|
@ -134,7 +136,7 @@ public class ExcelExportUtil {
|
|
|
|
|
cellStyleTxt.setBorderTop(BorderStyle.THIN); // 上边边框
|
|
|
|
|
//导出数据
|
|
|
|
|
for (int i = 0; i < page.getList().size(); i++) {
|
|
|
|
|
row = hssfSheet.createRow(count++);
|
|
|
|
|
row = xssfSheet.createRow(count++);
|
|
|
|
|
Record record = (Record) page.getList().get(i);
|
|
|
|
|
for (int j = 0; j < colInfo.size(); j++) {
|
|
|
|
|
JSONObject jsonObject2 = colInfo.getJSONObject(j);
|
|
|
|
@ -149,7 +151,7 @@ public class ExcelExportUtil {
|
|
|
|
|
File file = new File(fileName);
|
|
|
|
|
try {
|
|
|
|
|
FileOutputStream fileOutputStreane = new FileOutputStream(file);
|
|
|
|
|
hssfWorkbook.write(fileOutputStreane);
|
|
|
|
|
xssfWorkbook.write(fileOutputStreane);
|
|
|
|
|
fileOutputStreane.flush();
|
|
|
|
|
fileOutputStreane.close();
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|