浏览代码

修改csv导出方式,用于支持分段查询

4.1.3.A
jueyue 5 年前
父节点
当前提交
22262387f6
共有 2 个文件被更改,包括 74 次插入21 次删除
  1. +20
    -8
      easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java
  2. +54
    -13
      easypoi-base/src/main/java/cn/afterturn/easypoi/csv/export/CsvExportService.java

+ 20
- 8
easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java 查看文件

@@ -4,6 +4,7 @@ import cn.afterturn.easypoi.csv.entity.CsvExportParams;
import cn.afterturn.easypoi.csv.export.CsvExportService;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
@@ -15,14 +16,16 @@ import java.util.List;
*/
public final class CsvExportUtil {

private CsvExportService cs;

/**
* @param params 表格标题属性
* @param pojoClass Excel对象Class
* @param dataSet Excel对象数据List
*/
public static void exportCsv(CsvExportParams params, Class<?> pojoClass,
Collection<?> dataSet, OutputStream outputStream) {
new CsvExportService().createCsv(outputStream, params, pojoClass, dataSet);
public static CsvExportUtil exportCsv(CsvExportParams params, Class<?> pojoClass, OutputStream outputStream) {
CsvExportUtil ce = new CsvExportUtil();
ce.cs = new CsvExportService(outputStream, params, pojoClass);
return ce;
}

/**
@@ -30,10 +33,19 @@ public final class CsvExportUtil {
*
* @param params 表格标题属性
* @param entityList Map对象列表
* @param dataSet Excel对象数据List
*/
public static void exportCsv(CsvExportParams params, List<ExcelExportEntity> entityList,
Collection<?> dataSet, OutputStream outputStream) {
new CsvExportService().createCsvOfList(outputStream, params, entityList, dataSet);
public static CsvExportUtil exportCsv(CsvExportParams params, List<ExcelExportEntity> entityList, OutputStream outputStream) {
CsvExportUtil ce = new CsvExportUtil();
ce.cs = new CsvExportService(outputStream, params, entityList);
return ce;
}

public CsvExportUtil write(Collection<?> dataSet) {
this.cs.write(dataSet);
return this;
}

public void close() {
this.cs.close();
}
}

+ 54
- 13
easypoi-base/src/main/java/cn/afterturn/easypoi/csv/export/CsvExportService.java 查看文件

@@ -9,6 +9,7 @@ import cn.afterturn.easypoi.exception.excel.ExcelExportException;
import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -26,30 +27,33 @@ public class CsvExportService extends BaseExportService {

private static final Logger LOGGER = LoggerFactory.getLogger(CsvExportService.class);

private CsvExportParams params;
private List<ExcelExportEntity> excelParams;
private Writer writer;

/**
* 导出Csv类文件
*
* @param outputStream 输出流
* @param params 输出参数
* @param pojoClass 输出类
* @param dataSet 集合
*/
public void createCsv(OutputStream outputStream, CsvExportParams params, Class<?> pojoClass, Collection<?> dataSet) {
public CsvExportService(OutputStream outputStream, CsvExportParams params, Class<?> pojoClass) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("CSV export start ,class is {}", pojoClass);
}
if (params == null || pojoClass == null || dataSet == null) {
if (params == null || pojoClass == null) {
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
}
try {
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
// 得到所有字段
Field[] fields = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
String targetId = etarget == null ? null : etarget.value();
Field[] fields = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
String targetId = etarget == null ? null : etarget.value();
getAllExcelField(params.getExclusions(), targetId, fields, excelParams, pojoClass,
null, null);
createCsvOfList(outputStream, params, excelParams, dataSet);
createCsvOfList(outputStream, params, excelParams);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
@@ -60,13 +64,23 @@ public class CsvExportService extends BaseExportService {
* @param outputStream 输出流
* @param params 输出参数
* @param excelParams 列参数
* @param dataSet 集合参数
*/
public void createCsvOfList(OutputStream outputStream, CsvExportParams params, List<ExcelExportEntity> excelParams, Collection<?> dataSet) {
public CsvExportService(OutputStream outputStream, CsvExportParams params, List<ExcelExportEntity> excelParams) {
createCsvOfList(outputStream, params, excelParams);
}

/**
* @param outputStream 输出流
* @param params 输出参数
* @param excelParams 列参数
*/
private CsvExportService createCsvOfList(OutputStream outputStream, CsvExportParams params, List<ExcelExportEntity> excelParams) {
try {

Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, params.getEncoding()));

this.params = params;
this.excelParams = excelParams;
this.writer = writer;
dataHandler = params.getDataHandler();
if (dataHandler != null && dataHandler.getNeedHandlerFields() != null) {
needHandlerList = Arrays.asList(dataHandler.getNeedHandlerFields());
@@ -77,9 +91,32 @@ public class CsvExportService extends BaseExportService {
if (params.isCreateHeadRows()) {
writer.write(createHeaderRow(params, excelParams));
}
return this;
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
}
}

/**
* 导出Csv类文件
*
* @param dataSet 集合
*/
public CsvExportService write(Collection<?> dataSet) {
try {
return writerData(dataSet);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
}
}

private CsvExportService writerData(Collection<?> dataSet) {
try {
Iterator<?> iterator = dataSet.iterator();
String line = null;
int i = 0;
String line = null;
int i = 0;
while (iterator.hasNext()) {
Object obj = iterator.next();
line = createRow(excelParams, params, obj);
@@ -90,7 +127,7 @@ public class CsvExportService extends BaseExportService {
i++;
}
writer.flush();
writer.close();
return this;
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
@@ -151,4 +188,8 @@ public class CsvExportService extends BaseExportService {
private String getLineMark() {
return "\n";
}

public void close() {
IOUtils.closeQuietly(this.writer);
}
}

正在加载...
取消
保存