| @@ -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 cn.afterturn.easypoi.handler.inter.IExcelExportServer; | |||
| import cn.afterturn.easypoi.handler.inter.IWriter; | |||
| import java.io.OutputStream; | |||
| import java.util.Collection; | |||
| @@ -16,7 +17,8 @@ import java.util.List; | |||
| */ | |||
| public final class CsvExportUtil { | |||
| private CsvExportService cs; | |||
| private CsvExportUtil() { | |||
| } | |||
| /** | |||
| @@ -28,10 +30,9 @@ public final class CsvExportUtil { | |||
| * @param outputStream | |||
| */ | |||
| public static void exportCsv(CsvExportParams params, Class<?> pojoClass, Collection<?> dataSet, OutputStream outputStream) { | |||
| CsvExportUtil ce = new CsvExportUtil(); | |||
| ce.cs = new CsvExportService(outputStream, params, pojoClass); | |||
| ce.write(dataSet); | |||
| ce.close(); | |||
| IWriter writer = new CsvExportService(outputStream, params, pojoClass); | |||
| writer.writer(dataSet); | |||
| writer.close(); | |||
| } | |||
| /** | |||
| @@ -44,25 +45,23 @@ public final class CsvExportUtil { | |||
| * @param outputStream | |||
| */ | |||
| public static void exportCsv(CsvExportParams params, Class<?> pojoClass, IExcelExportServer server, Object queryParams, OutputStream outputStream) { | |||
| CsvExportUtil ce = new CsvExportUtil(); | |||
| ce.cs = new CsvExportService(outputStream, params, pojoClass); | |||
| int page = 1; | |||
| IWriter writer = new CsvExportService(outputStream, params, pojoClass); | |||
| int page = 1; | |||
| Collection dataSet; | |||
| while ((dataSet = server.selectListForExcelExport(queryParams, page)) != null && dataSet.size() > 0) { | |||
| page++; | |||
| ce.write(dataSet); | |||
| writer.writer(dataSet); | |||
| } | |||
| ce.close(); | |||
| writer.close(); | |||
| } | |||
| /** | |||
| * @param params 表格标题属性 | |||
| * @param pojoClass Excel对象Class | |||
| */ | |||
| public static CsvExportUtil exportCsv(CsvExportParams params, Class<?> pojoClass, OutputStream outputStream) { | |||
| public static IWriter<Void> exportCsv(CsvExportParams params, Class<?> pojoClass, OutputStream outputStream) { | |||
| CsvExportUtil ce = new CsvExportUtil(); | |||
| ce.cs = new CsvExportService(outputStream, params, pojoClass); | |||
| return ce; | |||
| return new CsvExportService(outputStream, params, pojoClass); | |||
| } | |||
| /** | |||
| @@ -71,18 +70,8 @@ public final class CsvExportUtil { | |||
| * @param params 表格标题属性 | |||
| * @param entityList Map对象列表 | |||
| */ | |||
| 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 static IWriter<Void> exportCsv(CsvExportParams params, List<ExcelExportEntity> entityList, OutputStream outputStream) { | |||
| return new CsvExportService(outputStream, params, entityList); | |||
| } | |||
| public CsvExportUtil write(Collection<?> dataSet) { | |||
| this.cs.write(dataSet); | |||
| return this; | |||
| } | |||
| public void close() { | |||
| this.cs.close(); | |||
| } | |||
| } | |||
| @@ -7,6 +7,7 @@ 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.handler.inter.IWriter; | |||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
| import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |||
| import org.apache.poi.util.IOUtils; | |||
| @@ -23,7 +24,7 @@ import java.util.*; | |||
| /** | |||
| * @author by jueyue on 18-11-14. | |||
| */ | |||
| public class CsvExportService extends BaseExportService { | |||
| public class CsvExportService extends BaseExportService implements IWriter<Void> { | |||
| private static final Logger LOGGER = LoggerFactory.getLogger(CsvExportService.class); | |||
| @@ -98,42 +99,6 @@ public class CsvExportService extends BaseExportService { | |||
| } | |||
| } | |||
| /** | |||
| * 导出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; | |||
| while (iterator.hasNext()) { | |||
| Object obj = iterator.next(); | |||
| line = createRow(excelParams, params, obj); | |||
| writer.write(line); | |||
| if (i % 10000 == 0) { | |||
| writer.flush(); | |||
| } | |||
| i++; | |||
| } | |||
| writer.flush(); | |||
| return this; | |||
| } catch (Exception e) { | |||
| LOGGER.error(e.getMessage(), e); | |||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause()); | |||
| } | |||
| } | |||
| /** | |||
| * 创建行 | |||
| * | |||
| @@ -189,7 +154,33 @@ public class CsvExportService extends BaseExportService { | |||
| return "\n"; | |||
| } | |||
| public void close() { | |||
| @Override | |||
| public IWriter writer(Collection data) { | |||
| try { | |||
| Iterator<?> iterator = data.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(); | |||
| return this; | |||
| } catch (Exception e) { | |||
| LOGGER.error(e.getMessage(), e); | |||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause()); | |||
| } | |||
| } | |||
| @Override | |||
| public Void close() { | |||
| IOUtils.closeQuietly(this.writer); | |||
| return null; | |||
| } | |||
| } | |||
| @@ -23,6 +23,7 @@ import cn.afterturn.easypoi.excel.export.ExcelBatchExportService; | |||
| import cn.afterturn.easypoi.excel.export.ExcelExportService; | |||
| import cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelExportServer; | |||
| import cn.afterturn.easypoi.handler.inter.IWriter; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.apache.poi.xssf.streaming.SXSSFWorkbook; | |||
| @@ -47,6 +48,31 @@ public final class ExcelExportUtil { | |||
| private ExcelExportUtil() { | |||
| } | |||
| /** | |||
| * 大数据量导出 | |||
| * | |||
| * @param entity 表格标题属性 | |||
| * @param pojoClass Excel对象Class | |||
| */ | |||
| public static IWriter<Workbook> exportBigExcel(ExportParams entity, Class<?> pojoClass) { | |||
| ExcelBatchExportService batchServer = new ExcelBatchExportService(); | |||
| batchServer.init(entity, pojoClass); | |||
| return batchServer; | |||
| } | |||
| /** | |||
| * 大数据量导出 | |||
| * | |||
| * @param entity | |||
| * @param excelParams | |||
| * @return | |||
| */ | |||
| public static IWriter<Workbook> exportBigExcel(ExportParams entity, List<ExcelExportEntity> excelParams) { | |||
| ExcelBatchExportService batchServer = new ExcelBatchExportService(); | |||
| batchServer.init(entity, excelParams); | |||
| return batchServer; | |||
| } | |||
| /** | |||
| * 大数据量导出 | |||
| * | |||
| @@ -8,6 +8,7 @@ import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; | |||
| import cn.afterturn.easypoi.exception.excel.ExcelExportException; | |||
| import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; | |||
| import cn.afterturn.easypoi.handler.inter.IExcelExportServer; | |||
| import cn.afterturn.easypoi.handler.inter.IWriter; | |||
| import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; | |||
| import cn.afterturn.easypoi.util.PoiPublicUtil; | |||
| import org.apache.poi.ss.usermodel.Drawing; | |||
| @@ -26,7 +27,7 @@ import static cn.afterturn.easypoi.excel.ExcelExportUtil.USE_SXSSF_LIMIT; | |||
| * @author JueYue | |||
| * 2016年8月29日 | |||
| */ | |||
| public class ExcelBatchExportService extends ExcelExportService { | |||
| public class ExcelBatchExportService extends ExcelExportService implements IWriter<Workbook> { | |||
| private Workbook workbook; | |||
| private Sheet sheet; | |||
| @@ -97,25 +98,6 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| } | |||
| } | |||
| public Workbook appendData(Collection<?> dataSet) { | |||
| if (sheet.getLastRowNum() + dataSet.size() > entity.getMaxNum()) { | |||
| sheet = workbook.createSheet(); | |||
| index = 0; | |||
| } | |||
| Iterator<?> its = dataSet.iterator(); | |||
| while (its.hasNext()) { | |||
| Object t = its.next(); | |||
| try { | |||
| index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight, 0)[0]; | |||
| } catch (Exception e) { | |||
| LOGGER.error(e.getMessage(), e); | |||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); | |||
| } | |||
| } | |||
| return workbook; | |||
| } | |||
| @Override | |||
| protected void insertDataToSheet(Workbook workbook, ExportParams entity, | |||
| List<ExcelExportEntity> entityList, Collection<?> dataSet, | |||
| @@ -150,25 +132,49 @@ public class ExcelBatchExportService extends ExcelExportService { | |||
| } | |||
| } | |||
| public Workbook closeExportBigExcel() { | |||
| if (entity.getFreezeCol() != 0) { | |||
| sheet.createFreezePane(entity.getFreezeCol(), titleHeight, entity.getFreezeCol(), titleHeight); | |||
| } | |||
| mergeCells(sheet, excelParams, titleHeight); | |||
| // 创建合计信息 | |||
| addStatisticsRow(getExcelExportStyler().getStyles(true, null), sheet); | |||
| return workbook; | |||
| } | |||
| public Workbook exportBigExcel(IExcelExportServer server, Object queryParams) { | |||
| int page = 1; | |||
| List<Object> list = server | |||
| .selectListForExcelExport(queryParams, page++); | |||
| while (list != null && list.size() > 0) { | |||
| appendData(list); | |||
| writer(list); | |||
| list = server.selectListForExcelExport(queryParams, page++); | |||
| } | |||
| return closeExportBigExcel(); | |||
| return close(); | |||
| } | |||
| @Override | |||
| public Workbook get() { | |||
| return this.workbook; | |||
| } | |||
| @Override | |||
| public IWriter<Workbook> writer(Collection data) { | |||
| if (sheet.getLastRowNum() + data.size() > entity.getMaxNum()) { | |||
| sheet = workbook.createSheet(); | |||
| index = 0; | |||
| } | |||
| Iterator<?> its = data.iterator(); | |||
| while (its.hasNext()) { | |||
| Object t = its.next(); | |||
| try { | |||
| index += createCells(patriarch, index, t, excelParams, sheet, workbook, rowHeight, 0)[0]; | |||
| } catch (Exception e) { | |||
| LOGGER.error(e.getMessage(), e); | |||
| throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e); | |||
| } | |||
| } | |||
| return this; | |||
| } | |||
| @Override | |||
| public Workbook close() { | |||
| if (entity.getFreezeCol() != 0) { | |||
| sheet.createFreezePane(entity.getFreezeCol(), titleHeight, entity.getFreezeCol(), titleHeight); | |||
| } | |||
| mergeCells(sheet, excelParams, titleHeight); | |||
| // 创建合计信息 | |||
| addStatisticsRow(getExcelExportStyler().getStyles(true, null), sheet); | |||
| return workbook; | |||
| } | |||
| } | |||
| @@ -289,7 +289,7 @@ public class ExcelToHtmlService { | |||
| FileOutputStream fos = null; | |||
| try { | |||
| fos = new FileOutputStream(savefile); | |||
| fos.write(data); | |||
| fos.writer(data); | |||
| } catch (Exception e) { | |||
| LOGGER.error(e.getMessage(), e); | |||
| } finally { | |||
| @@ -184,7 +184,7 @@ public class StyleHelper { | |||
| * Outputs the appropriate CSS style for the given cell style. | |||
| * | |||
| * @param style The cell style. | |||
| * @param out The place to write the output. | |||
| * @param out The place to writer the output. | |||
| */ | |||
| void colorStyles(CellStyle style, Formatter out); | |||
| @@ -0,0 +1,34 @@ | |||
| package cn.afterturn.easypoi.handler.inter; | |||
| import java.util.Collection; | |||
| /** | |||
| * 大数据写出服务接口 | |||
| * | |||
| * @author jueyue on 19-11-25. | |||
| */ | |||
| public interface IWriter<T> { | |||
| /** | |||
| * 获取输出对象 | |||
| * | |||
| * @return | |||
| */ | |||
| default public T get() { | |||
| return null; | |||
| } | |||
| /** | |||
| * 写入数据 | |||
| * | |||
| * @param data | |||
| * @return | |||
| */ | |||
| public IWriter<T> writer(Collection data); | |||
| /** | |||
| * 关闭流,完成业务 | |||
| * | |||
| * @return | |||
| */ | |||
| public T close(); | |||
| } | |||
| @@ -82,10 +82,10 @@ public class ParseWord07 { | |||
| if (text != null && text.contains(FOREACH) && text.startsWith(START_STR)) { | |||
| text = text.replace(FOREACH_NOT_CREATE, EMPTY).replace(FOREACH_AND_SHIFT, EMPTY) | |||
| .replace(FOREACH, EMPTY).replace(START_STR, EMPTY); | |||
| String[] keys = text.replaceAll("\\s{1,}", " ").trim().split(" "); | |||
| Object result=PoiPublicUtil.getParamsValue(keys[0], map); | |||
| String[] keys = text.replaceAll("\\s{1,}", " ").trim().split(" "); | |||
| Object result = PoiPublicUtil.getParamsValue(keys[0], map); | |||
| //添加list默认值,避免将{{$fe: list t.sn t.hoby t.remark}} 这类标签直接显示出来 | |||
| return Objects.nonNull(result)?result:new ArrayList<Map<String,Object>>(0); | |||
| return Objects.nonNull(result) ? result : new ArrayList<Map<String, Object>>(0); | |||
| } | |||
| return null; | |||
| } | |||
| @@ -120,16 +120,16 @@ public class ParseWord07 { | |||
| * 2013-11-16 | |||
| */ | |||
| private void parseThisParagraph(XWPFParagraph paragraph, Map<String, Object> map) throws Exception { | |||
| XWPFRun run; | |||
| XWPFRun run; | |||
| // 拿到的第一个run,用来set值,可以保存格式 | |||
| XWPFRun currentRun = null; | |||
| XWPFRun currentRun = null; | |||
| // 存放当前的text | |||
| String currentText = ""; | |||
| String text; | |||
| String currentText = ""; | |||
| String text; | |||
| // 判断是不是已经遇到{{ | |||
| Boolean isfinde = false; | |||
| Boolean isfinde = false; | |||
| // 存储遇到的run,把他们置空 | |||
| List<Integer> runIndex = new ArrayList<Integer>(); | |||
| List<Integer> runIndex = new ArrayList<Integer>(); | |||
| for (int i = 0; i < paragraph.getRuns().size(); i++) { | |||
| run = paragraph.getRuns().get(i); | |||
| text = run.getText(0); | |||
| @@ -172,7 +172,6 @@ public class ParseWord07 { | |||
| } | |||
| /** | |||
| * 解析这个表格 | |||
| * | |||
| @@ -225,10 +224,10 @@ public class ParseWord07 { | |||
| * @return | |||
| */ | |||
| public XWPFDocument parseWord(String url, List<Map<String, Object>> list) throws Exception { | |||
| if (list.size() == 1) { | |||
| return parseWord(url, list.get(0)); | |||
| } else if (list.size() == 0) { | |||
| if (list == null || list.size() == 0) { | |||
| return null; | |||
| } else if (list.size() == 1) { | |||
| return parseWord(url, list.get(0)); | |||
| } else { | |||
| MyXWPFDocument doc = WordCache.getXWPFDocument(url); | |||
| parseWordSetValue(doc, list.get(0)); | |||