Commit 0004b5a5 authored by yanzg's avatar yanzg

执行时间

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