Commit 0004b5a5 authored by yanzg's avatar yanzg

执行时间

parent 63cf9379
...@@ -30,6 +30,7 @@ import java.util.regex.Pattern; ...@@ -30,6 +30,7 @@ import java.util.regex.Pattern;
* @author 颜佐光 * @author 颜佐光
*/ */
public class ExcelConsole<T extends Object> implements DbRow<T> { public class ExcelConsole<T extends Object> implements DbRow<T> {
private final Pattern chinese = Pattern.compile("[^x00-xff]"); private final Pattern chinese = Pattern.compile("[^x00-xff]");
/** /**
* 配置信息 * 配置信息
...@@ -84,11 +85,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -84,11 +85,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/ */
private Map<String, ExcelMergerData> mergerGroupData; private Map<String, ExcelMergerData> mergerGroupData;
/**
* 最大字节长度
*/
private Map<Integer, Integer> columnBytes;
/** /**
* 控制台输出Excel * 控制台输出Excel
* *
...@@ -111,10 +107,20 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -111,10 +107,20 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
this.rowHandle = rowHandle; this.rowHandle = rowHandle;
} }
/**
* 缓存行
*
* @return 行号
*/
public int getCacheRow() { public int getCacheRow() {
return cacheRow; return cacheRow;
} }
/**
* 缓存行
*
* @param cacheRow 行号
*/
public void setCacheRow(int cacheRow) { public void setCacheRow(int cacheRow) {
this.cacheRow = cacheRow; this.cacheRow = cacheRow;
} }
...@@ -131,9 +137,15 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -131,9 +137,15 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
/** /**
* 行处理方式 * 行处理方式
* *
* @return * @return 当前处理行
*/ */
public ExcelRow<T> getRowHandle() { public ExcelRow<T> getRowHandle() {
ExcelRow rowHandle;
if (this.rowHandle == null) {
rowHandle = ExcelRowDefault.getInstance();
} else {
rowHandle = this.rowHandle;
}
return rowHandle; return rowHandle;
} }
...@@ -150,19 +162,10 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -150,19 +162,10 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
/** /**
* 检测参数是否异常 * 检测参数是否异常
*/ */
public ExcelConsole check() { public ExcelConsole<T> check() {
CheckerHelper check = CheckerHelper.newInstance() CheckerHelper check = CheckerHelper.newInstance().notBlankCheck("导出xls配置", this.config).notBlankCheck("导出xls.标题", this.config.getTitle()).notBlankCheck("导出xls.子标题", this.config.getSubTitle()).notBlankCheck("导出xls.服务器路径", this.config.getServerPath()).notBlankCheck("导出xls.文件名", this.config.getFileName()).notBlankListCheck("导出xls.列", this.config.getColumns()).checkException();
.notBlankCheck("导出xls配置", this.config)
.notBlankCheck("导出xls.标题", this.config.getTitle())
.notBlankCheck("导出xls.子标题", this.config.getSubTitle())
.notBlankCheck("导出xls.服务器路径", this.config.getServerPath())
.notBlankCheck("导出xls.文件名", this.config.getFileName())
.notBlankListCheck("导出xls.列", this.config.getColumns())
.checkException();
for (ExportColumn column : this.config.getColumns()) { for (ExportColumn column : this.config.getColumns()) {
check.notBlankCheck("导出xls.列名", column.getName()) check.notBlankCheck("导出xls.列名", column.getName()).notBlankCheck("导出xls.标题", column.getTitle()).checkException();
.notBlankCheck("导出xls.标题", column.getTitle())
.checkException();
} }
return this; return this;
...@@ -193,9 +196,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -193,9 +196,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
throw YzgError.getRuntimeException("034"); throw YzgError.getRuntimeException("034");
} }
// 创建合并对象数据检测 // 创建合并对象数据检测
mergerGroup = new HashMap<>(); mergerGroup = new HashMap<>(20);
mergerGroupData = new HashMap<>(); mergerGroupData = new HashMap<>(20);
columnBytes = new HashMap<>();
if (this.cacheRow < 1) { if (this.cacheRow < 1) {
this.cacheRow = 1000; this.cacheRow = 1000;
...@@ -206,7 +208,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -206,7 +208,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
workbook.setCompressTempFiles(true); workbook.setCompressTempFiles(true);
sheet = workbook.createSheet(); sheet = workbook.createSheet();
style = initColumnCenterstyle(workbook); style = initColumnCenterStyle(workbook);
// 行和列都是从0开始计数,且起始结束都会合并 // 行和列都是从0开始计数,且起始结束都会合并
...@@ -218,7 +220,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -218,7 +220,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
writeHead(head); writeHead(head);
rowIndex += head.getTotalRow(); rowIndex += head.getTotalRow();
} }
/** /**
...@@ -233,7 +234,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -233,7 +234,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 创建一行 // 创建一行
Row row = sheet.createRow(rowIndex); Row row = sheet.createRow(rowIndex);
row.setHeight(getUnit(rowHeight)); row.setHeight(getUnit(rowHeight));
Cell cell = createCell(row, 0, content, false); createCell(row, 0, content);
// 这里是合并excel中多列为1列 // 这里是合并excel中多列为1列
CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, 0, columnLength); CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, 0, columnLength);
sheet.addMergedRegion(region); sheet.addMergedRegion(region);
...@@ -255,7 +256,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -255,7 +256,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 写入列头 // 写入列头
for (TableHeadItem headItem : head.getColumns()) { for (TableHeadItem headItem : head.getColumns()) {
Row row = rows[headItem.getRow()]; Row row = rows[headItem.getRow()];
Cell cell = createCell(row, headItem.getColumn(), headItem.getName(), true); createCell(row, headItem.getColumn(), headItem.getName());
// 判断是否需要合并列头 // 判断是否需要合并列头
if (headItem.getColumnCell() > 1 || headItem.getRowCell() > 1) { if (headItem.getColumnCell() > 1 || headItem.getRowCell() > 1) {
...@@ -284,12 +285,21 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -284,12 +285,21 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 当前合并組中添加需要合并的列 // 当前合并組中添加需要合并的列
mergerGroup.get(group).add(column.getName()); mergerGroup.get(group).add(column.getName());
} }
// 设置列宽度
int columnPos = 0;
for (ExportColumn column : this.config.getColumns()) {
sheet.setColumnWidth(columnPos, getUnit(column.getWidth()));
columnPos++;
}
} }
/** /**
* 开始生成Excel文件 * 开始生成Excel文件
*
* @return 当前对象
*/ */
public ExcelConsole open() { public ExcelConsole<T> open() {
this.check(); this.check();
TableHead head = this.initHead(); TableHead head = this.initHead();
initExcel(head); initExcel(head);
...@@ -304,15 +314,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -304,15 +314,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/ */
@Override @Override
public void handle(T t) { public void handle(T t) {
ExcelRow rowHandle = this.rowHandle; ExcelRow<T> rowHandle = this.getRowHandle();
if (rowHandle == null) {
rowHandle = ExcelRowDefault.getInstance();
}
// 创建一行 // 创建一行
Row row = sheet.createRow(rowIndex); Row row = sheet.createRow(rowIndex);
row.setHeight(getUnit(this.config.getRowHeight())); // 高度
int height = getUnit(this.config.getRowHeight());
// 合并組数据处理 // 合并組数据处理
for (Map.Entry<String, List<String>> groupKvp : mergerGroup.entrySet()) { for (Map.Entry<String, List<String>> groupKvp : mergerGroup.entrySet()) {
// 将当前組生成值密钥 // 将当前組生成值密钥
...@@ -333,13 +339,23 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -333,13 +339,23 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
for (ExportColumn column : this.config.getColumns()) { for (ExportColumn column : this.config.getColumns()) {
String columnName = column.getName(); String columnName = column.getName();
String value = StringHelper.getFirst(rowHandle.get(t, columnName)); String value = StringHelper.getFirst(rowHandle.get(t, columnName));
// 列宽度
int columnWidth = getUnit(column.getWidth());
// 实际内容宽度
int chinaWidth = (int) Math.round(value.length() * 1.5);
// 当前单元格实际行高
int nowCellHeight = StringHelper.getPage(chinaWidth, columnWidth) * getUnit(this.config.getRowHeight());
// 当前行行高
height = Math.max(nowCellHeight, height);
// 当不需要合并历史记录时,则创建新的内容 // 当不需要合并历史记录时,则创建新的内容
Cell cell = createCell(row, columnPos, value, true); createCell(row, columnPos, value);
mergerColumn(column, columnPos, false); mergerColumn(column, columnPos, false);
columnPos++; columnPos++;
} }
row.setHeight((short) Math.min(height, Short.MAX_VALUE));
rowIndex++; rowIndex++;
rowData++; rowData++;
...@@ -384,71 +400,59 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -384,71 +400,59 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnPos, columnPos); CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnPos, columnPos);
sheet.addMergedRegion(region); sheet.addMergedRegion(region);
Cell cell = sheet.getRow(rowStart).getCell(columnPos);
} }
/** /**
* 会自动在生成完毕调用该函数 * 会自动在生成完毕调用该函数
*
* @return 当前对象
*/ */
public ExcelConsole save() { public ExcelConsole<T> save() {
if (this.excelStatus != null) { if (this.excelStatus != null) {
excelStatus.excelFinish(this); excelStatus.excelFinish(this);
} }
if (workbook == null) { if (workbook == null) {
return this; return this;
} }
// 合并数据配置 // 合并数据配置
if (this.rowData > 0) { if (this.rowData > 0) {
int columnPos = 0; int columnPos = 0;
for (ExportColumn column : this.config.getColumns()) { for (ExportColumn column : this.config.getColumns()) {
mergerColumn(column, columnPos, true); mergerColumn(column, columnPos, true);
if (column.getWidth() < 1) {
//设置单元格长度, 这里要乘上256
int maxBytes = columnBytes.get(columnPos);
sheet.setColumnWidth(columnPos, maxBytes * 256);
} else {
sheet.setColumnWidth(columnPos, getUnit(column.getWidth()));
}
columnPos++; columnPos++;
} }
} }
try { String fileNameTemp = this.getFileNameTemp();
String fileNameTemp = this.getFileNameTemp(); // 保存为临时文件
// 保存为临时文件 try (OutputStream out = new FileOutputStream(fileNameTemp)) {
OutputStream out = new FileOutputStream(fileNameTemp); workbook.write(out);
try { out.flush();
workbook.write(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
throw YzgError.getRuntimeException("035");
} finally {
out.close();
}
// 判断临时文件是否存在
File file = new File(fileNameTemp);
if (!file.exists()) {
throw YzgError.getRuntimeException("036", this.getFileName());
}
// 重命名成正式文件
String fileName = this.getFileName();
file.renameTo(new File(fileName));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
throw YzgError.getRuntimeException("035"); throw YzgError.getRuntimeException("035");
} }
// 判断临时文件是否存在
File file = new File(fileNameTemp);
if (!file.exists()) {
throw YzgError.getRuntimeException("036", this.getFileName());
}
// 重命名成正式文件
String fileName = this.getFileName();
boolean b = file.renameTo(new File(fileName));
if (!b) {
throw YzgError.getRuntimeException("036", fileName);
}
return this; return this;
} }
/** /**
* 删除生成的Excel文件 * 删除生成的Excel文件
* *
* @return * @return 当前文件
*/ */
public ExcelConsole remove() { public ExcelConsole<T> remove() {
if (this.workbook != null) { if (this.workbook != null) {
workbook = null; workbook = null;
} }
...@@ -468,8 +472,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -468,8 +472,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/ */
public String getFileName() { public String getFileName() {
FileHelper.createDirectory(this.config.getServerPath()); FileHelper.createDirectory(this.config.getServerPath());
String fileName = String.format("%s/%s", this.config.getServerPath(), this.config.getFileName()); return String.format("%s/%s", this.config.getServerPath(), this.config.getFileName());
return fileName;
} }
/** /**
...@@ -484,19 +487,13 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -484,19 +487,13 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
/** /**
* 创建单元格 * 创建单元格
* *
* @param row * @param row
* @param column * @param column
* @param content * @param content 内容
* @return * @return 单元格
*/ */
private Cell createCell(Row row, int column, String content, boolean isMax) { private Cell createCell(Row row, int column, String content) {
// 获取字节数、用于设置最大宽度 // 获取字节数、用于设置最大宽度
if (isMax) {
int chinaCount = (int) Math.round(content.length() * 1.5);
int maxBytes = Math.max(columnBytes.getOrDefault(column, 0), chinaCount);
columnBytes.put(column, maxBytes);
}
Cell cell = row.createCell(column); Cell cell = row.createCell(column);
cell.setCellStyle(style); cell.setCellStyle(style);
cell.setCellValue(content); cell.setCellValue(content);
...@@ -510,7 +507,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -510,7 +507,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
* <b>@param wb * <b>@param wb
* <b>@return</b> * <b>@return</b>
*/ */
public CellStyle initColumnCenterstyle(Workbook wb) { public CellStyle initColumnCenterStyle(Workbook wb) {
CellStyle cellStyle = wb.createCellStyle(); CellStyle cellStyle = wb.createCellStyle();
......
...@@ -39,7 +39,7 @@ public class ExportData { ...@@ -39,7 +39,7 @@ public class ExportData {
public static final short ROW_HEIGHT = 30; public static final short ROW_HEIGHT = 30;
@ApiModelProperty(notes = "列默认宽度") @ApiModelProperty(notes = "列默认宽度")
public static final short COLUMN_WIDTH = 0; public static final short COLUMN_WIDTH = 100;
/** /**
* 服务器保存路径 * 服务器保存路径
*/ */
...@@ -91,6 +91,12 @@ public class ExportData { ...@@ -91,6 +91,12 @@ public class ExportData {
@ApiModelProperty(notes = "行高度") @ApiModelProperty(notes = "行高度")
private short rowHeight; private short rowHeight;
/**
* 自动换行
*/
@ApiModelProperty(notes = "自动换行")
private boolean line;
/** /**
* 包含列 * 包含列
*/ */
...@@ -181,6 +187,14 @@ public class ExportData { ...@@ -181,6 +187,14 @@ public class ExportData {
this.rowHeight = rowHeight; this.rowHeight = rowHeight;
} }
public boolean isLine() {
return line;
}
public void setLine(boolean line) {
this.line = line;
}
public List<ExportColumn> getColumns() { public List<ExportColumn> getColumns() {
return columns; return columns;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment