@@ -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<ExcelExportEntity> 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<ExcelExportEntity> entityList, | |||||
Collection<?> dataSet, OutputStream outputStream) { | |||||
new CsvExportService().createCsvOfList(outputStream, params, entityList, dataSet); | |||||
} | |||||
} |
@@ -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; | |||||
} | |||||
} |
@@ -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<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | |||||
// 得到所有字段 | |||||
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<ExcelExportEntity> 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<ExcelExportEntity> 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<ExcelExportEntity> 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"; | |||||
} | |||||
} |
@@ -26,6 +26,7 @@ import java.lang.reflect.Field; | |||||
import java.util.*; | import java.util.*; | ||||
/** | /** | ||||
* Csv 导入服务 | |||||
* @author by jueyue on 18-10-3. | * @author by jueyue on 18-10-3. | ||||
*/ | */ | ||||
public class CsvImportService extends ImportBaseService { | public class CsvImportService extends ImportBaseService { | ||||
@@ -59,6 +60,7 @@ public class CsvImportService extends ImportBaseService { | |||||
inputstream = new PushbackInputStream(inputstream, 3); | inputstream = new PushbackInputStream(inputstream, 3); | ||||
byte[] head = new byte[3]; | byte[] head = new byte[3]; | ||||
inputstream.read(head); | inputstream.read(head); | ||||
// 判断 UTF8 是不是有 BOM | |||||
if (head[0] == -17 && head[1] == -69 && head[2] == -65) { | if (head[0] == -17 && head[1] == -69 && head[2] == -65) { | ||||
((PushbackInputStream) inputstream).unread(head, 0, 3); | ((PushbackInputStream) inputstream).unread(head, 0, 3); | ||||
inputstream = new UnicodeInputStream(inputstream); | inputstream = new UnicodeInputStream(inputstream); | ||||
@@ -39,7 +39,7 @@ import cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil; | |||||
* @version 1.0 | * @version 1.0 | ||||
* 2013-10-17 | * 2013-10-17 | ||||
*/ | */ | ||||
public class ExcelExportUtil { | |||||
public final class ExcelExportUtil { | |||||
private ExcelExportUtil() { | private ExcelExportUtil() { | ||||
} | } | ||||