Commit 67573421 authored by yanzg's avatar yanzg

1.3版本修改,增加删除功能

parent 442276c3
...@@ -11,6 +11,7 @@ import com.yanzuoguang.util.table.TableHead; ...@@ -11,6 +11,7 @@ import com.yanzuoguang.util.table.TableHead;
import com.yanzuoguang.util.table.TableHeadHelper; import com.yanzuoguang.util.table.TableHeadHelper;
import com.yanzuoguang.util.table.TableHeadItem; import com.yanzuoguang.util.table.TableHeadItem;
import com.yanzuoguang.util.thread.ThreadHelper; import com.yanzuoguang.util.thread.ThreadHelper;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
...@@ -247,7 +248,7 @@ public class ExcelConsole<T> implements DbRow<T> { ...@@ -247,7 +248,7 @@ public class ExcelConsole<T> implements DbRow<T> {
createCell(row, 0, content, defaultStyle); createCell(row, 0, content, defaultStyle);
// 这里是合并excel中多列为1列 // 这里是合并excel中多列为1列
CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, 0, columnLength); CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, 0, columnLength);
sheet.addMergedRegion(region); addMergedRegion(region);
setDefaultStyle(region, defaultStyle); setDefaultStyle(region, defaultStyle);
} }
...@@ -277,7 +278,7 @@ public class ExcelConsole<T> implements DbRow<T> { ...@@ -277,7 +278,7 @@ public class ExcelConsole<T> implements DbRow<T> {
int columnEnd = columnStart + headItem.getColumnCell() - 1; int columnEnd = columnStart + headItem.getColumnCell() - 1;
CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnStart, columnEnd); CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnStart, columnEnd);
sheet.addMergedRegion(region); addMergedRegion(region);
setDefaultStyle(region, defaultStyle); setDefaultStyle(region, defaultStyle);
} }
} }
...@@ -309,7 +310,7 @@ public class ExcelConsole<T> implements DbRow<T> { ...@@ -309,7 +310,7 @@ public class ExcelConsole<T> implements DbRow<T> {
int columnEnd = columnStart + cell.getWidthSize() - 1; int columnEnd = columnStart + cell.getWidthSize() - 1;
CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnStart, columnEnd); CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnStart, columnEnd);
sheet.addMergedRegion(region); addMergedRegion(region);
setDefaultStyle(region, cell.cellStyle); setDefaultStyle(region, cell.cellStyle);
} }
} }
...@@ -317,6 +318,27 @@ public class ExcelConsole<T> implements DbRow<T> { ...@@ -317,6 +318,27 @@ public class ExcelConsole<T> implements DbRow<T> {
} }
} }
private void addMergedRegion(CellRangeAddress region) {
for (int r = region.getFirstRow(); r <= region.getLastRow(); r++) {
for (int c = region.getFirstColumn(); c <= region.getLastColumn(); c++) {
if (r == region.getFirstRow() && c == region.getFirstColumn()) {
continue;
}
// 合并时清除非第一行第一列的值
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
Cell cell = row.getCell(c);
if (cell == null) {
continue;
}
cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
}
}
sheet.addMergedRegion(region);
}
private void writeDefineFormula(List<ExcelDefineRow> rows) { private void writeDefineFormula(List<ExcelDefineRow> rows) {
Map<String, Object> map = new HashMap<>(this.config.getColumns().size() * 2 + 3); Map<String, Object> map = new HashMap<>(this.config.getColumns().size() * 2 + 3);
// 设置列别名 // 设置列别名
...@@ -476,7 +498,7 @@ public class ExcelConsole<T> implements DbRow<T> { ...@@ -476,7 +498,7 @@ public class ExcelConsole<T> implements DbRow<T> {
} }
CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnPos, columnPos); CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnPos, columnPos);
sheet.addMergedRegion(region); addMergedRegion(region);
setDefaultStyle(region, column.cellStyle); setDefaultStyle(region, column.cellStyle);
} }
......
...@@ -84,6 +84,20 @@ public class ExcelDefineCell { ...@@ -84,6 +84,20 @@ public class ExcelDefineCell {
*/ */
Cell cell; Cell cell;
public ExcelDefineCell() {
}
public ExcelDefineCell(short column, String formula) {
this.column = column;
this.formula = formula;
}
public ExcelDefineCell(short column, Object value, String formula) {
this.column = column;
this.value = value;
this.formula = formula;
}
public short getColumn() { public short getColumn() {
return column; return column;
} }
......
...@@ -3,6 +3,7 @@ package com.yanzuoguang.excel; ...@@ -3,6 +3,7 @@ package com.yanzuoguang.excel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
...@@ -22,6 +23,27 @@ public class ExcelDefineRow extends ExcelRowIndex { ...@@ -22,6 +23,27 @@ public class ExcelDefineRow extends ExcelRowIndex {
@ApiModelProperty(notes = "单元格") @ApiModelProperty(notes = "单元格")
private List<ExcelDefineCell> cells = new ArrayList<>(); private List<ExcelDefineCell> cells = new ArrayList<>();
public ExcelDefineRow() {
}
public ExcelDefineRow(List<ExcelDefineCell> cells) {
this.cells = cells;
}
public ExcelDefineRow(ExcelDefineCell... cells) {
this.cells.addAll(Arrays.asList(cells));
}
public ExcelDefineRow(short height, List<ExcelDefineCell> cells) {
this.height = height;
this.cells = cells;
}
public ExcelDefineRow(short height, ExcelDefineCell... cells) {
this.height = height;
this.cells.addAll(Arrays.asList(cells));
}
public short getHeight() { public short getHeight() {
return height; return height;
} }
......
...@@ -78,6 +78,12 @@ public class ExportColumn { ...@@ -78,6 +78,12 @@ public class ExportColumn {
this.width = width; this.width = width;
} }
public ExportColumn(String title, String name, boolean merger) {
this.title = title;
this.name = name;
this.merger = merger;
}
public String getTitle() { public String getTitle() {
return title; return title;
} }
......
import com.yanzuoguang.excel.*;
import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.helper.StringHelper;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TestExcel {
@Test
public void testExcel() {
List<TextExcelRow> list = new ArrayList<>();
list.add(new TextExcelRow("订单1", "长江索道往返票", 3, 60, "颜佐光", "18641143400"));
list.add(new TextExcelRow("订单1", "长江索道往返票", 3, 60, "颜佐光1", "18641143401"));
list.add(new TextExcelRow("订单1", "长江索道往返票", 3, 60, "颜佐光2", "18641143402"));
list.add(new TextExcelRow("订单2", "长江索道单程票", 2, 40, "颜佐", "18651143401"));
list.add(new TextExcelRow("订单2", "长江索道单程票", 2, 40, "颜佐1", "18651143402"));
ExportData config = new ExportData();
config.setServerPath("./target/");
config.setFileName("测试文件.xlsx");
config.setTitle("测试文件");
config.setSubTitle("子标题");
config.setLine(true);
config.setDownFileName(String.format("%s-%s.xlsx", config.getTitle(), config.getSubTitle()));
config.getColumns().add(new ExportColumn("订单编号", "orderId", true, (short) 250));
config.getColumns().add(new ExportColumn("产品名称", "productName", true, (short) 250));
config.getColumns().add(new ExportColumn("购买数量", "buyNum", true, (short) 250));
config.getColumns().add(new ExportColumn("购买金额", "buyMoney", true, (short) 250));
config.getColumns().add(new ExportColumn("游客名称", "visitorName", false, (short) 250));
config.getColumns().add(new ExportColumn("游客手机", "visitorMobile", false, (short) 250));
FileHelper.checkFolder(config.getServerPath());
FileHelper.checkFolder(config.getFileName());
FileHelper.checkFolder(config.getDownFileName());
config.getEndRows().add(new ExcelDefineRow(
(short) 40,
new ExcelDefineCell((short) 0, "合计", StringHelper.EMPTY),
new ExcelDefineCell((short) 2, "SUM({buyNum})"),
new ExcelDefineCell((short) 3, "SUM({buyMoney})")
));
ExcelConsole<TextExcelRow> excel = new ExcelConsole<>(config, new ExcelStatus<TextExcelRow>() {
@Override
public void excelInit(ExcelConsole<TextExcelRow> excel) {
}
@Override
public void excelRow(TextExcelRow excel, int rowIndex) {
}
@Override
public void excelFinish(ExcelConsole<TextExcelRow> excel) {
}
});
excel.open();
list.forEach(excel::handle);
excel.save();
}
}
public class TextExcelRow {
private String orderId;
private String productName;
private int buyNum;
private double buyMoney;
private String visitorName;
private String visitorMobile;
public TextExcelRow() {
}
public TextExcelRow(String orderId, String productName, int buyNum, double buyMoney, String visitorName, String visitorMobile) {
this.orderId = orderId;
this.productName = productName;
this.buyNum = buyNum;
this.buyMoney = buyMoney;
this.visitorName = visitorName;
this.visitorMobile = visitorMobile;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getBuyNum() {
return buyNum;
}
public void setBuyNum(int buyNum) {
this.buyNum = buyNum;
}
public double getBuyMoney() {
return buyMoney;
}
public void setBuyMoney(double buyMoney) {
this.buyMoney = buyMoney;
}
public String getVisitorName() {
return visitorName;
}
public void setVisitorName(String visitorName) {
this.visitorName = visitorName;
}
public String getVisitorMobile() {
return visitorMobile;
}
public void setVisitorMobile(String visitorMobile) {
this.visitorMobile = visitorMobile;
}
}
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