From 35b41b652d64d1d9e20befb2448f43f89c61d837 Mon Sep 17 00:00:00 2001 From: jueyue Date: Wed, 14 Nov 2018 15:20:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=88=90=E5=8A=9F=E6=94=AF=E6=8C=81=E4=BA=86CS?= =?UTF-8?q?V=E6=A0=BC=E5=BC=8F,=E5=95=A6=E5=95=A6=E5=95=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../afterturn/easypoi/csv/CsvExportUtil.java | 68 ++++++++ .../easypoi/csv/entity/CsvExportParams.java | 110 +++++++++++++ .../easypoi/csv/export/CsvExportService.java | 154 ++++++++++++++++++ .../easypoi/csv/imports/CsvImportService.java | 2 + .../easypoi/excel/ExcelExportUtil.java | 2 +- 5 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java create mode 100644 easypoi-base/src/main/java/cn/afterturn/easypoi/csv/entity/CsvExportParams.java create mode 100644 easypoi-base/src/main/java/cn/afterturn/easypoi/csv/export/CsvExportService.java diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java new file mode 100644 index 0000000..9482dcd --- /dev/null +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/CsvExportUtil.java @@ -0,0 +1,68 @@ +package cn.afterturn.easypoi.csv; + +import cn.afterturn.easypoi.csv.entity.CsvExportParams; +import cn.afterturn.easypoi.csv.export.CsvExportService; +import cn.afterturn.easypoi.excel.entity.ExportParams; +import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; +import cn.afterturn.easypoi.excel.export.ExcelBatchExportService; +import org.apache.poi.ss.usermodel.Workbook; + +import java.io.OutputStream; +import java.util.Collection; +import java.util.List; + +/** + * Csv批量导出文件 + * + * @author by jueyue on 18-11-14. + */ +public final class CsvExportUtil { + + /** + * @param entity 表格标题属性 + * @param pojoClass Excel对象Class + * @param dataSet Excel对象数据List + */ + public static Workbook exportBigExcel(ExportParams entity, Class pojoClass, + Collection dataSet) { + ExcelBatchExportService batchService = ExcelBatchExportService + .getExcelBatchExportService(entity, pojoClass); + return batchService.appendData(dataSet); + } + + public static Workbook exportBigExcel(ExportParams entity, List excelParams, + Collection dataSet) { + ExcelBatchExportService batchService = ExcelBatchExportService + .getExcelBatchExportService(entity, excelParams); + return batchService.appendData(dataSet); + } + + public static void closeExportBigExcel() { + ExcelBatchExportService batchService = ExcelBatchExportService.getCurrentExcelBatchExportService(); + if (batchService != null) { + batchService.closeExportBigExcel(); + } + } + + /** + * @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); + } + + /** + * 根据Map创建对应的Excel + * + * @param params 表格标题属性 + * @param entityList Map对象列表 + * @param dataSet Excel对象数据List + */ + public static void exportCsv(CsvExportParams params, List entityList, + Collection dataSet, OutputStream outputStream) { + new CsvExportService().createCsvOfList(outputStream, params, entityList, dataSet); + } +} diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/entity/CsvExportParams.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/entity/CsvExportParams.java new file mode 100644 index 0000000..52286a6 --- /dev/null +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/entity/CsvExportParams.java @@ -0,0 +1,110 @@ +package cn.afterturn.easypoi.csv.entity; + +import cn.afterturn.easypoi.excel.entity.ExcelBaseParams; + +/** + * CSV 导入参数 + * + * @author by jueyue on 18-10-3. + */ +public class CsvExportParams extends ExcelBaseParams { + + public static final String UTF8 = "utf-8"; + public static final String GBK = "gbk"; + public static final String GB2312 = "gb2312"; + + private String encoding = UTF8; + + /** + * 分隔符 + */ + private String spiltMark = ","; + + /** + * 字符串标识符 + */ + private String textMark = "\""; + + /** + * 表格标题行数,默认0 + */ + private int titleRows = 0; + + /** + * 表头行数,默认1 + */ + private int headRows = 1; + /** + * 过滤的属性 + */ + private String[] exclusions; + + /** + * 是否创建表头 + */ + private boolean isCreateHeadRows = true; + + public CsvExportParams() { + + } + + public CsvExportParams(String encoding) { + this.encoding = encoding; + } + + public String getEncoding() { + return encoding; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + public String getSpiltMark() { + return spiltMark; + } + + public void setSpiltMark(String spiltMark) { + this.spiltMark = spiltMark; + } + + public String getTextMark() { + return textMark; + } + + public void setTextMark(String textMark) { + this.textMark = textMark; + } + + public int getTitleRows() { + return titleRows; + } + + public void setTitleRows(int titleRows) { + this.titleRows = titleRows; + } + + public int getHeadRows() { + return headRows; + } + + public void setHeadRows(int headRows) { + this.headRows = headRows; + } + + public String[] getExclusions() { + return exclusions; + } + + public void setExclusions(String[] exclusions) { + this.exclusions = exclusions; + } + + public boolean isCreateHeadRows() { + return isCreateHeadRows; + } + + public void setCreateHeadRows(boolean createHeadRows) { + isCreateHeadRows = createHeadRows; + } +} diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/export/CsvExportService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/export/CsvExportService.java new file mode 100644 index 0000000..b7f34d1 --- /dev/null +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/export/CsvExportService.java @@ -0,0 +1,154 @@ +package cn.afterturn.easypoi.csv.export; + +import cn.afterturn.easypoi.csv.entity.CsvExportParams; +import cn.afterturn.easypoi.excel.annotation.ExcelTarget; +import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; +import cn.afterturn.easypoi.excel.entity.vo.BaseEntityTypeConstants; +import cn.afterturn.easypoi.excel.export.base.BaseExportService; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedWriter; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.lang.reflect.Field; +import java.util.*; + +/** + * @author by jueyue on 18-11-14. + */ +public class CsvExportService extends BaseExportService { + + private static final Logger LOGGER = LoggerFactory.getLogger(CsvExportService.class); + + /** + * 导出Csv类文件 + * + * @param outputStream 输出流 + * @param params 输出参数 + * @param pojoClass 输出类 + * @param dataSet 集合 + */ + public void createCsv(OutputStream outputStream, CsvExportParams params, Class pojoClass, Collection dataSet) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("CSV export start ,class is {}", pojoClass); + } + if (params == null || pojoClass == null || dataSet == null) { + throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); + } + try { + List excelParams = new ArrayList(); + // 得到所有字段 + 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); + } catch (Exception e) { + LOGGER.error(e.getMessage(), e); + throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause()); + } + } + + /** + * @param outputStream 输出流 + * @param params 输出参数 + * @param excelParams 列参数 + * @param dataSet 集合参数 + */ + public void createCsvOfList(OutputStream outputStream, CsvExportParams params, List excelParams, Collection dataSet) { + try { + + Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, params.getEncoding())); + + dataHandler = params.getDataHandler(); + if (dataHandler != null && dataHandler.getNeedHandlerFields() != null) { + needHandlerList = Arrays.asList(dataHandler.getNeedHandlerFields()); + } + dictHandler = params.getDictHandler(); + i18nHandler = params.getI18nHandler(); + sortAllParams(excelParams); + if (params.isCreateHeadRows()) { + writer.write(createHeaderRow(params, excelParams)); + } + Iterator iterator = dataSet.iterator(); + String line = null; + int i = 0; + while (iterator.hasNext()) { + Object obj = iterator.next(); + line = createRow(excelParams, params, obj); + writer.write(line); + if (i % 10000 == 0) { + writer.flush(); + } + i++; + } + writer.flush(); + writer.close(); + } catch (Exception e) { + LOGGER.error(e.getMessage(), e); + throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause()); + } + } + + /** + * 创建行 + * + * @param excelParams + * @param params + * @param t + * @return + */ + private String createRow(List excelParams, CsvExportParams params, Object t) { + StringBuilder sb = new StringBuilder(); + try { + ExcelExportEntity entity; + for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) { + entity = excelParams.get(k); + Object value = getCellValue(entity, t); + if (entity.getType() == BaseEntityTypeConstants.STRING_TYPE) { + sb.append(params.getTextMark()); + sb.append(value.toString()); + sb.append(params.getTextMark()); + } else if (entity.getType() == BaseEntityTypeConstants.DOUBLE_TYPE) { + sb.append(value.toString()); + } + if (k < paramSize - 1) { + sb.append(params.getSpiltMark()); + } + } + return sb.append(getLineMark()).toString(); + } catch (Exception e) { + LOGGER.error("csv export error ,data is :{}", ReflectionToStringBuilder.toString(t)); + LOGGER.error(e.getMessage(), e); + throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); + } + } + + + /** + * 创建表头 + */ + private String createHeaderRow(CsvExportParams params, List excelParams) { + StringBuilder sb = new StringBuilder(); + for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { + ExcelExportEntity entity = excelParams.get(i); + sb.append(entity.getName()); + if (i < exportFieldTitleSize - 1) { + sb.append(params.getSpiltMark()); + } + } + return sb.append(getLineMark()).toString(); + + } + + private String getLineMark() { + return "\n"; + } +} diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/imports/CsvImportService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/imports/CsvImportService.java index a270064..5bf4d74 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/imports/CsvImportService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/csv/imports/CsvImportService.java @@ -26,6 +26,7 @@ import java.lang.reflect.Field; import java.util.*; /** + * Csv 导入服务 * @author by jueyue on 18-10-3. */ public class CsvImportService extends ImportBaseService { @@ -59,6 +60,7 @@ public class CsvImportService extends ImportBaseService { inputstream = new PushbackInputStream(inputstream, 3); byte[] head = new byte[3]; inputstream.read(head); + // 判断 UTF8 是不是有 BOM if (head[0] == -17 && head[1] == -69 && head[2] == -65) { ((PushbackInputStream) inputstream).unread(head, 0, 3); inputstream = new UnicodeInputStream(inputstream); diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelExportUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelExportUtil.java index a840da8..6a48912 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelExportUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelExportUtil.java @@ -39,7 +39,7 @@ import cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil; * @version 1.0 * 2013-10-17 */ -public class ExcelExportUtil { +public final class ExcelExportUtil { private ExcelExportUtil() { }