Commit efe320d2 authored by yanzg's avatar yanzg

设置单元格的值

parent e3e05ae0
......@@ -18,6 +18,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.regex.Pattern;
/**
......@@ -66,7 +67,14 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
* 行数据
*/
private int rowData;
/**
* 开始数据行
*/
private int startDataRowIndex = Integer.MAX_VALUE;
/**
* 结束数据行
*/
private int endDataRowIndex = Integer.MIN_VALUE;
/**
* 缓存行数
*/
......@@ -213,6 +221,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
writeHead(head);
rowIndex += head.getTotalRow();
// 写入自定义行
writeDefineRow(this.config.getStartRows());
}
/**
......@@ -290,6 +300,22 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
return this;
}
private void writeDefineRow(List<ExcelDefineRow> rows) {
// 创建行
for (ExcelDefineRow row : rows) {
Row sheetRow = sheet.createRow(rowIndex);
sheetRow.setHeight(getUnit(row.getHeight()));
for (ExcelDefineCell cell : row.getCells()) {
cell.cellStyle = createCellStyle(cell);
cell.cell = createCell(sheetRow, cell.getColumn(), cell.getValue(), cell.cellStyle);
}
rowIndex++;
}
}
private void writeDefineFormula(List<ExcelDefineRow> rows) {
}
/**
* 循环处理单行数据
*
......@@ -297,6 +323,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/
@Override
public void handle(T t) {
// 数据开始行
this.startDataRowIndex = Math.min(this.startDataRowIndex, this.rowIndex);
// 数据结束行
this.endDataRowIndex = Math.max(this.endDataRowIndex, this.rowIndex);
ExcelRow<T> rowHandle = this.getRowHandle();
// 创建一行
Row row = sheet.createRow(rowIndex);
......@@ -417,6 +448,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
}
}
// 写入自定义行
writeDefineRow(this.config.getEndRows());
writeDefineFormula(this.config.getStartRows());
writeDefineFormula(this.config.getEndRows());
String fileNameTemp = this.getFileNameTemp();
// 保存为临时文件
try (OutputStream out = new FileOutputStream(fileNameTemp)) {
......@@ -538,8 +574,21 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
private CellStyle createColumnStyle(SXSSFWorkbook wb, ExportColumn column) {
CellStyle style = this.createStyle(wb, false);
setAlignment(style, column.getAlignment());
setVerticalAlignment(style, column.getVerticalAlignment());
return style;
}
private CellStyle createCellStyle(ExcelDefineCell cell) {
CellStyle style = this.createStyle(this.workbook, false);
setAlignment(style, cell.getAlignment());
setVerticalAlignment(style, cell.getVerticalAlignment());
return style;
}
private void setAlignment(CellStyle style, int alignment) {
// 左右居中
switch (column.getAlignment()) {
switch (alignment) {
case 1:
style.setAlignment(CellStyle.ALIGN_LEFT);
break;
......@@ -551,9 +600,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
style.setAlignment(CellStyle.ALIGN_CENTER);
break;
}
}
private void setVerticalAlignment(CellStyle style, int verticalAlignment) {
// 上下居中
switch (column.getVerticalAlignment()) {
switch (verticalAlignment) {
case 1:
style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
break;
......@@ -565,7 +616,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
break;
}
return style;
}
private void setDefaultStyle(CellRangeAddress region, CellStyle cellStyle) {
......
package com.yanzuoguang.excel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
/**
* 单元格
......@@ -12,7 +14,7 @@ public class ExcelDefineCell {
* 第几列
*/
@ApiModelProperty(notes = "第几列")
private short columnPos;
private short column;
/**
* 横合并
*/
......@@ -39,17 +41,25 @@ public class ExcelDefineCell {
@ApiModelProperty(notes = "文字内容")
private String value;
/**
* 公式,如:SUM(C2:C3),或者SUM(columnName),columnName=数据列名称,公式优先
* 公式,如:SUM(C2:C3),或者SUM({columnName}),columnName=数据列名称,公式优先
*/
@ApiModelProperty(notes = " 公式,如:SUM(C2:C3),或者SUM(columnName),columnName=数据列名称,公式优先.不能SUM(columnA/columnB)")
@ApiModelProperty(notes = " 公式,如:SUM(C2:C3),或者SUM({columnName}),columnName=数据列名称,公式优先.不能SUM(columnA/columnB),当前行={rowIndex}")
private String formula;
/**
* 当前单元格样式
*/
CellStyle cellStyle;
/**
* 单元格
*/
Cell cell;
public short getColumnPos() {
return columnPos;
public short getColumn() {
return column;
}
public void setColumnPos(short columnPos) {
this.columnPos = columnPos;
public void setColumn(short column) {
this.column = column;
}
public short getWidthSize() {
......
......@@ -7,11 +7,11 @@ import java.util.ArrayList;
import java.util.List;
/**
* 订单导出数据
* 订单导出数据配置信息,需要注意,多线程导出时配置不能共用同一个对象
*
* @author 颜佐光
*/
@ApiModel(description = "订单导出数据配置信息")
@ApiModel(description = "订单导出数据配置信息,需要注意,多线程导出时配置不能共用同一个对象")
public class ExportData {
@ApiModelProperty(notes = "普通导出模式")
......
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