Commit 769ad805 authored by yanzg's avatar yanzg

Excel导出功能

parent dc139fcf
......@@ -28,6 +28,7 @@
<spring.boot.version>2.0.2.RELEASE</spring.boot.version>
<fastjson.version>1.2.47</fastjson.version>
<mysql.version>6.0.6</mysql.version>
<poi.version>3.9</poi.version>
<yzg.version>1.0-SNAPSHOT</yzg.version>
</properties>
......@@ -95,6 +96,11 @@
<version>${yzg.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
......
......@@ -76,6 +76,12 @@
<groupId>com.yanzuoguang</groupId>
<artifactId>yzg-util-base</artifactId>
</dependency>
<dependency>
<groupId>com.yanzuoguang</groupId>
<artifactId>yzg-util-db</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.yanzuoguang.cloud.excel;
import com.yanzuoguang.cloud.helper.HttpFileHelper;
import com.yanzuoguang.excel.ExcelConsole;
import com.yanzuoguang.excel.ExcelRow;
import com.yanzuoguang.excel.ExportData;
import com.yanzuoguang.util.helper.StringHelper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* HTTP方式下载Excel文件
*
* @param <T> 行数据的类型
* @author 颜佐光
*/
public class ExcelHttp<T extends Object> extends ExcelConsole<T> {
/**
* 导出成Excel
*
* @param exportData 导出信息
*/
public ExcelHttp(ExportData exportData) {
super(exportData);
}
/**
* 导出成Excel
*
* @param config 导出信息
* @param rowHandle 导出下载信息
*/
public ExcelHttp(ExportData config, ExcelRow<T> rowHandle) {
super(config, rowHandle);
}
/**
* 下载文件
*
* @param response 输出流
*/
public ExcelHttp down(HttpServletResponse response) throws IOException {
return this.down(response, StringHelper.EMPTY);
}
/**
* 下载文件
*
* @param response 输出流
* @param downFileName 下载文件名
*/
public ExcelHttp down(HttpServletResponse response, String downFileName) throws IOException {
if(StringHelper.isEmpty(downFileName)){
downFileName = this.getConfig().getFileName();
}
HttpFileHelper.localToDown(this.getFileName(),downFileName,response);
return this;
}
}
......@@ -49,5 +49,10 @@
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.yanzuoguang.excel;
import com.yanzuoguang.util.helper.StringHelper;
/**
* 数据合并处理
* @author 颜佐光
*/
public class ExcelMergerData {
/**
* 上次合并的数据
*/
private String data;
/**
* 合并行号
*/
private int rowIndex = -1;
/**
* 数据合并行数
*/
private int rowCell;
/**
* 写入新数据
*
* @param rowIndex 行号
* @param nowCellData 新数据
*/
public void updateMerger(int rowIndex, String nowCellData) {
if (this.rowIndex == -1 || !StringHelper.compare(this.data, nowCellData)) {
this.rowIndex = rowIndex;
this.data = nowCellData;
this.rowCell = 1;
} else {
this.rowCell++;
}
}
/**
* 写入数据,并且判断是否需要创建单元格
*
* @param nowCellData 需要写入的单元格的数据
* @return
*/
public boolean isMergerHistory(String nowCellData) {
return isMergerHistory() && !StringHelper.compare(this.data, nowCellData);
}
/**
* 是否合并单元格
*
* @return 合并标记
*/
public boolean isMergerHistory() {
return rowCell > 1 && rowIndex >= 0;
}
public String getData() {
return data;
}
public int getRowIndex() {
return rowIndex;
}
public int getRowCell() {
return rowCell;
}
}
package com.yanzuoguang.excel;
/**
* 获取行中的某列数据
* @param <T>
* @author 颜佐光
*/
public interface ExcelRow<T> {
/**
* 获取行里面的某列数据
*
* @param row  行
* @param field  列
* @return
*/
String get(T row, String field);
}
package com.yanzuoguang.excel;
import com.yanzuoguang.util.base.ObjectHelper;
/**
* 行数据默认处理
* @author 颜佐光
*/
public class ExcelRowDefault implements ExcelRow<Object> {
/**
* 获取行里面的某列数据
*
* @param row  行
* @param field  列
* @return
*/
@Override
public String get(Object row, String field) {
return ObjectHelper.getString(row, field);
}
private static ExcelRowDefault my = new ExcelRowDefault();
/**
* 获取默认实例
*
* @return
*/
public static ExcelRowDefault getInstance() {
return my;
}
}
package com.yanzuoguang.excel;
/**
* 导出数据
* @param <T>
* @author 颜佐光
*/
public class ExportBase<T extends Object> {
/**
* 导出格式
*/
private ExportData config;
/**
* 查询条件
*/
private T cond;
public ExportData getConfig() {
return config;
}
public void setConfig(ExportData config) {
this.config = config;
}
public T getCond() {
return cond;
}
public void setCond(T cond) {
this.cond = cond;
}
}
package com.yanzuoguang.excel;
/**
* 列设置
* @author 颜佐光
*/
public class ExportColumn {
/**
* 列标题,如 A.B
*/
private String title;
/**
* 数据单元格
*/
private String name;
/**
* 数据是否合并
*/
private boolean merger;
/**
* 高度
*/
private short width;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isMerger() {
return merger;
}
public void setMerger(boolean merger) {
this.merger = merger;
}
public short getWidth() {
if(width < 1){
return ExportData.COLUMN_WIDTH;
}
return width;
}
public void setWidth(short width) {
this.width = width;
}
}
package com.yanzuoguang.excel;
import java.util.ArrayList;
import java.util.List;
/**
* 订单导出数据
*
* @author 颜佐光
*/
public class ExportData {
public static final short ROW_HEIGHT_UNIT = 10;
public static final short TITLE_HEIGHT = 80;
public static final short SUB_TITLE_HEIGHT = 40;
public static final short HEAD_HEIGHT = 20;
public static final short ROW_HEIGHT = 20;
public static final short COLUMN_WIDTH = 120;
/**
* 服务器保存路径
*/
private String serverPath;
/**
* 文件名
*/
private String fileName;
/**
* 标题
*/
private String title;
/**
* 标题高度
*/
private short titleHeight;
/**
* 子标题
*/
private String subTitle;
/**
* 子标题高度
*/
private short subTitleHeight;
/**
* 列标题高度
*/
private short headHeight;
/**
* 行高度
*/
private short rowHeight;
/**
* 包含列
*/
private List<ExportColumn> columns = new ArrayList<>();
public String getServerPath() {
return serverPath;
}
public void setServerPath(String serverPath) {
this.serverPath = serverPath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public short getTitleHeight() {
if (titleHeight < 1) {
return TITLE_HEIGHT;
}
return titleHeight;
}
public void setTitleHeight(short titleHeight) {
this.titleHeight = titleHeight;
}
public String getSubTitle() {
return subTitle;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public short getSubTitleHeight() {
if (subTitleHeight < 1) {
return SUB_TITLE_HEIGHT;
}
return subTitleHeight;
}
public void setSubTitleHeight(short subTitleHeight) {
this.subTitleHeight = subTitleHeight;
}
public short getHeadHeight() {
if (headHeight < 1) {
return HEAD_HEIGHT;
}
return headHeight;
}
public void setHeadHeight(short headHeight) {
this.headHeight = headHeight;
}
public short getRowHeight() {
if (rowHeight < 1) {
return ROW_HEIGHT;
}
return rowHeight;
}
public void setRowHeight(short rowHeight) {
this.rowHeight = rowHeight;
}
public List<ExportColumn> getColumns() {
return columns;
}
public void setColumns(List<ExportColumn> columns) {
this.columns = 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