| @@ -10,6 +10,7 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook; | |||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |||
| import org.jeecgframework.poi.excel.entity.ExportParams; | |||
| import org.jeecgframework.poi.excel.entity.TemplateExportParams; | |||
| import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| import org.jeecgframework.poi.excel.export.ExcelExportServer; | |||
| @@ -35,14 +36,14 @@ public final class ExcelExportUtil { | |||
| public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass, | |||
| Collection<?> dataSet) { | |||
| Workbook workbook; | |||
| if (entity.getType().equals(PoiBaseConstants.HSSF)) { | |||
| if (entity.getType().equals(ExcelType.HSSF)) { | |||
| workbook = new HSSFWorkbook(); | |||
| } else if (dataSet.size() < 1000) { | |||
| workbook = new XSSFWorkbook(); | |||
| } else { | |||
| workbook = new SXSSFWorkbook(); | |||
| } | |||
| new ExcelExportServer().createSheet(workbook, entity, pojoClass, dataSet, entity.getType()); | |||
| new ExcelExportServer().createSheet(workbook, entity, pojoClass, dataSet); | |||
| return workbook; | |||
| } | |||
| @@ -58,15 +59,14 @@ public final class ExcelExportUtil { | |||
| public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, | |||
| Collection<? extends Map<?, ?>> dataSet) { | |||
| Workbook workbook; | |||
| if (entity.getType().equals(PoiBaseConstants.HSSF)) { | |||
| if (entity.getType().equals(ExcelType.HSSF)) { | |||
| workbook = new HSSFWorkbook(); | |||
| } else if (dataSet.size() < 1000) { | |||
| workbook = new XSSFWorkbook(); | |||
| } else { | |||
| workbook = new SXSSFWorkbook(); | |||
| } | |||
| new ExcelExportServer().createSheetForMap(workbook, entity, entityList, dataSet, | |||
| entity.getType()); | |||
| new ExcelExportServer().createSheetForMap(workbook, entity, entityList, dataSet); | |||
| return workbook; | |||
| } | |||
| @@ -80,7 +80,7 @@ public final class ExcelExportUtil { | |||
| */ | |||
| public static Workbook exportExcel(List<Map<String, Object>> list, String type) { | |||
| Workbook workbook; | |||
| if (type.equals(PoiBaseConstants.HSSF)) { | |||
| if (type.equals(ExcelType.HSSF)) { | |||
| workbook = new HSSFWorkbook(); | |||
| } else { | |||
| workbook = new XSSFWorkbook(); | |||
| @@ -88,7 +88,7 @@ public final class ExcelExportUtil { | |||
| ExcelExportServer server = new ExcelExportServer(); | |||
| for (Map<String, Object> map : list) { | |||
| server.createSheet(workbook, (ExportParams) map.get("title"), | |||
| (Class<?>) map.get("entity"), (Collection<?>) map.get("data"), type); | |||
| (Class<?>) map.get("entity"), (Collection<?>) map.get("data")); | |||
| } | |||
| return workbook; | |||
| } | |||
| @@ -9,14 +9,6 @@ import org.jeecgframework.poi.handler.inter.IExcelDataHandler; | |||
| */ | |||
| public class ExcelBaseParams { | |||
| /** | |||
| * 03版本Excel | |||
| */ | |||
| public static final String HSSF = "HSSF"; | |||
| /** | |||
| * 07版本Excel | |||
| */ | |||
| public static final String XSSF = "XSSF"; | |||
| /** | |||
| * 数据处理接口,以此为主,replace,format都在这后面 | |||
| */ | |||
| @@ -1,7 +1,7 @@ | |||
| package org.jeecgframework.poi.excel.entity; | |||
| import org.apache.poi.hssf.util.HSSFColor; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||
| /** | |||
| * Excel 导出参数 | |||
| @@ -14,46 +14,50 @@ public class ExportParams extends ExcelBaseParams { | |||
| /** | |||
| * 表格名称 | |||
| */ | |||
| private String title; | |||
| private String title; | |||
| /** | |||
| * 表格名称 | |||
| */ | |||
| private short titleHeight = 20; | |||
| private short titleHeight = 20; | |||
| /** | |||
| * 第二行名称 | |||
| */ | |||
| private String secondTitle; | |||
| private String secondTitle; | |||
| /** | |||
| * 表格名称 | |||
| */ | |||
| private short secondTitleHeight = 8; | |||
| private short secondTitleHeight = 8; | |||
| /** | |||
| * sheetName | |||
| */ | |||
| private String sheetName; | |||
| private String sheetName; | |||
| /** | |||
| * 过滤的属性 | |||
| */ | |||
| private String[] exclusions; | |||
| private String[] exclusions; | |||
| /** | |||
| * 是否添加需要需要 | |||
| */ | |||
| private boolean addIndex; | |||
| private boolean addIndex; | |||
| /** | |||
| * 是否添加需要需要 | |||
| */ | |||
| private String indexName = "序号"; | |||
| /** | |||
| * 表头颜色 | |||
| */ | |||
| private short color = HSSFColor.WHITE.index; | |||
| private short color = HSSFColor.WHITE.index; | |||
| /** | |||
| * 属性说明行的颜色 例如:HSSFColor.SKY_BLUE.index 默认 | |||
| */ | |||
| private short headerColor = HSSFColor.SKY_BLUE.index; | |||
| private short headerColor = HSSFColor.SKY_BLUE.index; | |||
| /** | |||
| * Excel 导出版本 | |||
| */ | |||
| private String type = PoiBaseConstants.HSSF; | |||
| private ExcelType type = ExcelType.HSSF; | |||
| public ExportParams() { | |||
| @@ -64,6 +68,12 @@ public class ExportParams extends ExcelBaseParams { | |||
| this.sheetName = sheetName; | |||
| } | |||
| public ExportParams(String title, String sheetName, ExcelType type) { | |||
| this.title = title; | |||
| this.sheetName = sheetName; | |||
| this.type = type; | |||
| } | |||
| public ExportParams(String title, String secondTitle, String sheetName) { | |||
| this.title = title; | |||
| this.secondTitle = secondTitle; | |||
| @@ -142,11 +152,20 @@ public class ExportParams extends ExcelBaseParams { | |||
| this.titleHeight = titleHeight; | |||
| } | |||
| public String getType() { | |||
| public ExcelType getType() { | |||
| return type; | |||
| } | |||
| public void setType(String type) { | |||
| public void setType(ExcelType type) { | |||
| this.type = type; | |||
| } | |||
| public String getIndexName() { | |||
| return indexName; | |||
| } | |||
| public void setIndexName(String indexName) { | |||
| this.indexName = indexName; | |||
| } | |||
| } | |||
| @@ -0,0 +1,12 @@ | |||
| package org.jeecgframework.poi.excel.entity.enmus; | |||
| /** | |||
| * Cell 值得类型 | |||
| * @author JueYue | |||
| * @date 2014年12月29日 下午10:20:49 | |||
| */ | |||
| public enum CellValueType { | |||
| String, Number, Boolean, Date, TElement, Null, None; | |||
| } | |||
| @@ -0,0 +1,12 @@ | |||
| package org.jeecgframework.poi.excel.entity.enmus; | |||
| /** | |||
| * Excel Type | |||
| * @author JueYue | |||
| * @date 2014年12月29日 下午9:08:21 | |||
| */ | |||
| public enum ExcelType { | |||
| HSSF, XSSF; | |||
| } | |||
| @@ -0,0 +1,41 @@ | |||
| package org.jeecgframework.poi.excel.entity.sax; | |||
| import org.jeecgframework.poi.excel.entity.enmus.CellValueType; | |||
| /** | |||
| * Cell 对象 | |||
| * @author JueYue | |||
| * @date 2014年12月29日 下午10:12:57 | |||
| */ | |||
| public class SaxReadCellEntity { | |||
| /** | |||
| * 值类型 | |||
| */ | |||
| private CellValueType cellType; | |||
| /** | |||
| * 值 | |||
| */ | |||
| private Object value; | |||
| public SaxReadCellEntity(CellValueType cellType, Object value) { | |||
| this.cellType = cellType; | |||
| this.value = value; | |||
| } | |||
| public CellValueType getCellType() { | |||
| return cellType; | |||
| } | |||
| public void setCellType(CellValueType cellType) { | |||
| this.cellType = cellType; | |||
| } | |||
| public Object getValue() { | |||
| return value; | |||
| } | |||
| public void setValue(Object value) { | |||
| this.value = value; | |||
| } | |||
| } | |||
| @@ -21,13 +21,5 @@ public interface PoiBaseConstants { | |||
| * 是否增加属性列 | |||
| */ | |||
| public static String IS_ADD_INDEX = "isAddIndex"; | |||
| /** | |||
| * 03版本Excel | |||
| */ | |||
| public static String HSSF = "HSSF"; | |||
| /** | |||
| * 07版本Excel | |||
| */ | |||
| public static String XSSF = "XSSF"; | |||
| } | |||
| @@ -19,6 +19,7 @@ import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.apache.poi.ss.util.CellRangeAddress; | |||
| import org.jeecgframework.poi.excel.annotation.ExcelTarget; | |||
| import org.jeecgframework.poi.excel.entity.ExportParams; | |||
| import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| import org.jeecgframework.poi.excel.export.base.ExcelExportBase; | |||
| @@ -80,18 +81,19 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| } | |||
| public void createSheet(Workbook workbook, ExportParams entity, Class<?> pojoClass, | |||
| Collection<?> dataSet, String type) { | |||
| Collection<?> dataSet) { | |||
| if (logger.isDebugEnabled()) { | |||
| logger.debug("Excel export start ,class is {}", pojoClass); | |||
| logger.debug("Excel version is {}", type.equals(PoiBaseConstants.HSSF) ? "03" : "07"); | |||
| logger.debug("Excel version is {}", entity.getType().equals(ExcelType.HSSF) ? "03" | |||
| : "07"); | |||
| } | |||
| if (workbook == null || entity == null || pojoClass == null || dataSet == null) { | |||
| throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | |||
| } | |||
| if (type.equals(PoiBaseConstants.XSSF)) { | |||
| super.type = entity.getType(); | |||
| if (type.equals(ExcelType.XSSF)) { | |||
| MAX_NUM = 1000000; | |||
| } | |||
| super.type = type; | |||
| Sheet sheet = null; | |||
| try { | |||
| sheet = workbook.createSheet(entity.getSheetName()); | |||
| @@ -109,7 +111,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| Drawing patriarch = sheet.createDrawingPatriarch(); | |||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | |||
| if (entity.isAddIndex()) { | |||
| excelParams.add(indexExcelEntity()); | |||
| excelParams.add(indexExcelEntity(entity)); | |||
| } | |||
| // 得到所有字段 | |||
| Field fileds[] = POIPublicUtil.getClassFields(pojoClass); | |||
| @@ -140,11 +142,11 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| its.remove(); | |||
| } | |||
| // 创建合计信息 | |||
| addStatisticsRow(styles.get("one"),sheet); | |||
| addStatisticsRow(styles.get("one"), sheet); | |||
| // 发现还有剩余list 继续循环创建Sheet | |||
| if (dataSet.size() > 0) { | |||
| createSheet(workbook, entity, pojoClass, dataSet, type); | |||
| createSheet(workbook, entity, pojoClass, dataSet); | |||
| } | |||
| } catch (Exception e) { | |||
| @@ -156,17 +158,18 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| public void createSheetForMap(Workbook workbook, ExportParams entity, | |||
| List<ExcelExportEntity> entityList, | |||
| Collection<? extends Map<?, ?>> dataSet, String type) { | |||
| Collection<? extends Map<?, ?>> dataSet) { | |||
| if (logger.isDebugEnabled()) { | |||
| logger.debug("Excel version is {}", type.equals(PoiBaseConstants.HSSF) ? "03" : "07"); | |||
| logger.debug("Excel version is {}", entity.getType().equals(ExcelType.HSSF) ? "03" | |||
| : "07"); | |||
| } | |||
| if (workbook == null || entity == null || entityList == null || dataSet == null) { | |||
| throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | |||
| } | |||
| if (type.equals(PoiBaseConstants.XSSF)) { | |||
| super.type = entity.getType(); | |||
| if (type.equals(ExcelType.XSSF)) { | |||
| MAX_NUM = 1000000; | |||
| } | |||
| super.type = type; | |||
| Sheet sheet = null; | |||
| try { | |||
| sheet = workbook.createSheet(entity.getSheetName()); | |||
| @@ -184,7 +187,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| Drawing patriarch = sheet.createDrawingPatriarch(); | |||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | |||
| if (entity.isAddIndex()) { | |||
| excelParams.add(indexExcelEntity()); | |||
| excelParams.add(indexExcelEntity(entity)); | |||
| } | |||
| excelParams.addAll(entityList); | |||
| sortAllParams(excelParams); | |||
| @@ -212,7 +215,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| } | |||
| // 发现还有剩余list 继续循环创建Sheet | |||
| if (dataSet.size() > 0) { | |||
| createSheetForMap(workbook, entity, entityList, dataSet, type); | |||
| createSheetForMap(workbook, entity, entityList, dataSet); | |||
| } | |||
| } catch (Exception e) { | |||
| @@ -364,13 +367,13 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| return style; | |||
| } | |||
| private ExcelExportEntity indexExcelEntity() { | |||
| ExcelExportEntity entity = new ExcelExportEntity(); | |||
| entity.setOrderNum(0); | |||
| entity.setName("序号"); | |||
| entity.setWidth(10); | |||
| entity.setFormat(PoiBaseConstants.IS_ADD_INDEX); | |||
| return entity; | |||
| private ExcelExportEntity indexExcelEntity(ExportParams entity) { | |||
| ExcelExportEntity exportEntity = new ExcelExportEntity(); | |||
| exportEntity.setOrderNum(0); | |||
| exportEntity.setName(entity.getIndexName()); | |||
| exportEntity.setWidth(10); | |||
| exportEntity.setFormat(PoiBaseConstants.IS_ADD_INDEX); | |||
| return exportEntity; | |||
| } | |||
| } | |||
| @@ -28,6 +28,7 @@ import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.apache.poi.ss.util.CellRangeAddress; | |||
| import org.apache.poi.xssf.usermodel.XSSFClientAnchor; | |||
| import org.apache.poi.xssf.usermodel.XSSFRichTextString; | |||
| import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||
| import org.jeecgframework.poi.excel.entity.params.MergeEntity; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| @@ -43,7 +44,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| private int currentIndex = 0; | |||
| protected String type = PoiBaseConstants.HSSF; | |||
| protected ExcelType type = ExcelType.HSSF; | |||
| private Map<Integer, Double> statistics = new HashMap<Integer, Double>(); | |||
| @@ -150,7 +151,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| row.setHeight((short) (50 * entity.getHeight())); | |||
| row.createCell(i); | |||
| ClientAnchor anchor; | |||
| if (type.equals(PoiBaseConstants.HSSF)) { | |||
| if (type.equals(ExcelType.HSSF)) { | |||
| anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) i, row.getRowNum(), (short) (i + 1), | |||
| row.getRowNum() + 1); | |||
| } else { | |||
| @@ -255,7 +256,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| ExcelExportEntity entity) { | |||
| Cell cell = row.createCell(index); | |||
| RichTextString Rtext; | |||
| if (type.equals(PoiBaseConstants.HSSF)) { | |||
| if (type.equals(ExcelType.HSSF)) { | |||
| Rtext = new HSSFRichTextString(text); | |||
| } else { | |||
| Rtext = new XSSFRichTextString(text); | |||
| @@ -18,8 +18,8 @@ import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |||
| import org.jeecgframework.poi.cache.ExcelCache; | |||
| import org.jeecgframework.poi.excel.annotation.ExcelTarget; | |||
| import org.jeecgframework.poi.excel.entity.ExcelBaseParams; | |||
| import org.jeecgframework.poi.excel.entity.TemplateExportParams; | |||
| import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||
| import org.jeecgframework.poi.excel.export.base.ExcelExportBase; | |||
| import org.jeecgframework.poi.exception.excel.ExcelExportException; | |||
| @@ -52,7 +52,7 @@ public final class ExcelExportOfTemplateUtil extends ExcelExportBase { | |||
| throws Exception { | |||
| if (workbook instanceof XSSFWorkbook) { | |||
| super.type = ExcelBaseParams.XSSF; | |||
| super.type = ExcelType.XSSF; | |||
| } | |||
| // 获取表头数据 | |||
| Map<String, Integer> titlemap = getTitleMap(params, sheet); | |||
| @@ -0,0 +1,61 @@ | |||
| package org.jeecgframework.poi.excel.imports.sax; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.Iterator; | |||
| import java.util.List; | |||
| import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | |||
| import org.apache.poi.openxml4j.exceptions.OpenXML4JException; | |||
| import org.apache.poi.openxml4j.opc.OPCPackage; | |||
| import org.apache.poi.xssf.eventusermodel.XSSFReader; | |||
| import org.apache.poi.xssf.model.SharedStringsTable; | |||
| import org.jeecgframework.poi.excel.entity.ImportParams; | |||
| import org.jeecgframework.poi.excel.imports.sax.parse.ISaxRowRead; | |||
| import org.jeecgframework.poi.excel.imports.sax.parse.SaxRowRead; | |||
| import org.xml.sax.ContentHandler; | |||
| import org.xml.sax.InputSource; | |||
| import org.xml.sax.SAXException; | |||
| import org.xml.sax.XMLReader; | |||
| import org.xml.sax.helpers.XMLReaderFactory; | |||
| /** | |||
| * 基于SAX Excel大数据读取,读取Excel 07版本,不支持图片读取 | |||
| * @author JueYue | |||
| * @date 2014年12月29日 下午9:41:38 | |||
| * @version 1.0 | |||
| */ | |||
| public class SaxReadExcel { | |||
| public <T> List<T> readExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params) { | |||
| try { | |||
| OPCPackage opcPackage = OPCPackage.open(inputstream); | |||
| XSSFReader xssfReader = new XSSFReader(opcPackage); | |||
| SharedStringsTable sst = xssfReader.getSharedStringsTable(); | |||
| ISaxRowRead rowRead = new SaxRowRead(); | |||
| XMLReader parser = fetchSheetParser(sst, rowRead); | |||
| Iterator<InputStream> sheets = xssfReader.getSheetsData(); | |||
| int sheetIndex = 0; | |||
| while (sheets.hasNext() && sheetIndex < params.getSheetNum()) { | |||
| sheetIndex++; | |||
| InputStream sheet = sheets.next(); | |||
| InputSource sheetSource = new InputSource(sheet); | |||
| parser.parse(sheetSource); | |||
| sheet.close(); | |||
| } | |||
| return rowRead.getList(); | |||
| } catch (Exception e) { | |||
| } | |||
| return null; | |||
| } | |||
| public XMLReader fetchSheetParser(SharedStringsTable sst, ISaxRowRead rowRead) | |||
| throws SAXException { | |||
| XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); | |||
| ContentHandler handler = new SheetHandler(sst, rowRead); | |||
| parser.setContentHandler(handler); | |||
| return parser; | |||
| } | |||
| } | |||
| @@ -0,0 +1,122 @@ | |||
| package org.jeecgframework.poi.excel.imports.sax; | |||
| import java.math.BigDecimal; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| import org.apache.poi.hssf.usermodel.HSSFDateUtil; | |||
| import org.apache.poi.xssf.model.SharedStringsTable; | |||
| import org.apache.poi.xssf.usermodel.XSSFRichTextString; | |||
| import org.jeecgframework.poi.excel.entity.enmus.CellValueType; | |||
| import org.jeecgframework.poi.excel.entity.sax.SaxReadCellEntity; | |||
| import org.jeecgframework.poi.excel.imports.sax.parse.ISaxRowRead; | |||
| import org.xml.sax.Attributes; | |||
| import org.xml.sax.SAXException; | |||
| import org.xml.sax.helpers.DefaultHandler; | |||
| import com.google.common.collect.Lists; | |||
| /** | |||
| * 回调接口 | |||
| * @author JueYue | |||
| * @date 2014年12月29日 下午9:50:09 | |||
| */ | |||
| public class SheetHandler extends DefaultHandler { | |||
| private SharedStringsTable sst; | |||
| private String lastContents; | |||
| //当前行 | |||
| private int curRow = 0; | |||
| //当前列 | |||
| private int curCol = 0; | |||
| private CellValueType type; | |||
| private ISaxRowRead read; | |||
| //存储行记录的容器 | |||
| private List<SaxReadCellEntity> rowlist = Lists.newArrayList(); | |||
| public SheetHandler(SharedStringsTable sst, ISaxRowRead rowRead) { | |||
| this.sst = sst; | |||
| this.read = rowRead; | |||
| } | |||
| @Override | |||
| public void startElement(String uri, String localName, String name, Attributes attributes) | |||
| throws SAXException { | |||
| // 置空 | |||
| lastContents = ""; | |||
| // c => 单元格 | |||
| if ("c".equals(name)) { | |||
| // 如果下一个元素是 SST 的索引,则将nextIsString标记为true | |||
| String cellType = attributes.getValue("t"); | |||
| if ("s".equals(cellType)) { | |||
| type = CellValueType.String; | |||
| return; | |||
| } | |||
| //日期格式 | |||
| cellType = attributes.getValue("s"); | |||
| if ("1".equals(cellType)) { | |||
| type = CellValueType.Date; | |||
| } else if ("2".equals(cellType)) { | |||
| type = CellValueType.Number; | |||
| } | |||
| } else if ("t".equals(name)) {//当元素为t时 | |||
| type = CellValueType.TElement; | |||
| } | |||
| } | |||
| @Override | |||
| public void endElement(String uri, String localName, String name) throws SAXException { | |||
| // 根据SST的索引值的到单元格的真正要存储的字符串 | |||
| // 这时characters()方法可能会被调用多次 | |||
| if (CellValueType.String.equals(type)) { | |||
| try { | |||
| int idx = Integer.parseInt(lastContents); | |||
| lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString(); | |||
| } catch (Exception e) { | |||
| } | |||
| } | |||
| //t元素也包含字符串 | |||
| if (CellValueType.TElement.equals(type)) { | |||
| String value = lastContents.trim(); | |||
| rowlist.add(curCol, new SaxReadCellEntity(CellValueType.String, value)); | |||
| curCol++; | |||
| type = CellValueType.None; | |||
| // v => 单元格的值,如果单元格是字符串则v标签的值为该字符串在SST中的索引 | |||
| // 将单元格内容加入rowlist中,在这之前先去掉字符串前后的空白符 | |||
| } else if ("v".equals(name)) { | |||
| String value = lastContents.trim(); | |||
| value = value.equals("") ? " " : value; | |||
| if (CellValueType.Date.equals(type)) { | |||
| Date date = HSSFDateUtil.getJavaDate(Double.valueOf(value)); | |||
| rowlist.add(curCol, new SaxReadCellEntity(CellValueType.Date, date)); | |||
| } else if (CellValueType.Number.equals(type)) { | |||
| BigDecimal bd = new BigDecimal(value); | |||
| rowlist.add(curCol, new SaxReadCellEntity(CellValueType.Number, bd)); | |||
| } | |||
| curCol++; | |||
| } else { | |||
| //如果标签名称为 row ,这说明已到行尾,调用 optRows() 方法 | |||
| if (name.equals("row")) { | |||
| read.parse(curRow, rowlist); | |||
| rowlist.clear(); | |||
| curRow++; | |||
| curCol = 0; | |||
| } | |||
| } | |||
| } | |||
| @Override | |||
| public void characters(char[] ch, int start, int length) throws SAXException { | |||
| //得到单元格内容的值 | |||
| lastContents += new String(ch, start, length); | |||
| } | |||
| } | |||
| @@ -0,0 +1,22 @@ | |||
| package org.jeecgframework.poi.excel.imports.sax.parse; | |||
| import java.util.List; | |||
| import org.jeecgframework.poi.excel.entity.sax.SaxReadCellEntity; | |||
| public interface ISaxRowRead { | |||
| /** | |||
| * 获取返回数据 | |||
| * @param <T> | |||
| * @return | |||
| */ | |||
| public <T> List<T> getList(); | |||
| /** | |||
| * 解析数据 | |||
| * @param index | |||
| * @param datas | |||
| */ | |||
| public void parse(int index, List<SaxReadCellEntity> datas); | |||
| } | |||
| @@ -0,0 +1,34 @@ | |||
| package org.jeecgframework.poi.excel.imports.sax.parse; | |||
| import java.util.List; | |||
| import org.jeecgframework.poi.excel.entity.sax.SaxReadCellEntity; | |||
| import com.google.common.collect.Lists; | |||
| /** | |||
| * 当行读取数据 | |||
| * @author JueYue | |||
| * @param <T> | |||
| * @date 2015年1月1日 下午7:59:39 | |||
| */ | |||
| @SuppressWarnings("rawtypes") | |||
| public class SaxRowRead implements ISaxRowRead { | |||
| private List list; | |||
| public SaxRowRead() { | |||
| list = Lists.newArrayList(); | |||
| } | |||
| @Override | |||
| public <T> List<T> getList() { | |||
| return list; | |||
| } | |||
| @Override | |||
| public void parse(int index, List<SaxReadCellEntity> datas) { | |||
| } | |||
| } | |||
| @@ -12,6 +12,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.jeecgframework.poi.excel.ExcelExportUtil; | |||
| import org.jeecgframework.poi.excel.entity.ExportParams; | |||
| import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||
| import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| import org.junit.Test; | |||
| @@ -65,8 +66,7 @@ public class ExcelExportForMap { | |||
| } | |||
| list.add(map); | |||
| } | |||
| ExportParams params = new ExportParams("测试", "测试"); | |||
| params.setType(PoiBaseConstants.XSSF); | |||
| ExportParams params = new ExportParams("测试", "测试", ExcelType.HSSF); | |||
| Workbook workbook = ExcelExportUtil.exportExcel(params, entity, list); | |||
| FileOutputStream fos = new FileOutputStream("d:/tt.xlsx"); | |||
| workbook.write(fos); | |||
| @@ -15,6 +15,7 @@ import org.jeecgframework.poi.entity.MsgClient; | |||
| import org.jeecgframework.poi.entity.MsgClientGroup; | |||
| import org.jeecgframework.poi.excel.ExcelExportUtil; | |||
| import org.jeecgframework.poi.excel.entity.ExportParams; | |||
| import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| import org.junit.Test; | |||
| /** | |||
| @@ -42,8 +43,7 @@ public class ExcelExportMsgClient { | |||
| list.add(client); | |||
| } | |||
| Date start = new Date(); | |||
| ExportParams params = new ExportParams("2412312", "测试"); | |||
| params.setType(PoiBaseConstants.XSSF); | |||
| ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF); | |||
| Workbook workbook = ExcelExportUtil.exportExcel(params, MsgClient.class, list); | |||
| System.out.println(new Date().getTime() - start.getTime()); | |||
| File savefile = new File("d:/"); | |||
| @@ -11,6 +11,7 @@ import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.jeecgframework.poi.entity.statistics.StatisticEntity; | |||
| import org.jeecgframework.poi.excel.ExcelExportUtil; | |||
| import org.jeecgframework.poi.excel.entity.ExportParams; | |||
| import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| import org.junit.Test; | |||
| @@ -19,7 +20,7 @@ public class ExcelExportStatisticTest { | |||
| public void test() throws Exception { | |||
| List<StatisticEntity> list = new ArrayList<StatisticEntity>(); | |||
| for (int i = 0; i < 250000; i++) { | |||
| for (int i = 0; i < 25000; i++) { | |||
| StatisticEntity client = new StatisticEntity(); | |||
| client.setName("index" + i); | |||
| client.setIntTest(1 + i); | |||
| @@ -30,8 +31,7 @@ public class ExcelExportStatisticTest { | |||
| list.add(client); | |||
| } | |||
| Date start = new Date(); | |||
| ExportParams params = new ExportParams("2412312", "测试"); | |||
| params.setType(ExportParams.XSSF); | |||
| ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF); | |||
| Workbook workbook = ExcelExportUtil.exportExcel(params, StatisticEntity.class, list); | |||
| System.out.println(new Date().getTime() - start.getTime()); | |||
| File savefile = new File("d:/"); | |||
| @@ -14,6 +14,7 @@ import org.jeecgframework.poi.entity.TeacherEntity; | |||
| import org.jeecgframework.poi.excel.ExcelExportUtil; | |||
| import org.jeecgframework.poi.excel.entity.ExportParams; | |||
| import org.jeecgframework.poi.excel.entity.TemplateExportParams; | |||
| import org.jeecgframework.poi.excel.entity.enmus.ExcelType; | |||
| import org.junit.Before; | |||
| import org.junit.Test; | |||
| @@ -33,15 +34,15 @@ public class ExcelExportUtilTest { | |||
| @Test | |||
| public void oneHundredThousandRowTest() throws Exception { | |||
| ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF); | |||
| Date start = new Date(); | |||
| Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试"), | |||
| CourseEntity.class, list); | |||
| Workbook workbook = ExcelExportUtil.exportExcel(params, CourseEntity.class, list); | |||
| System.out.println(new Date().getTime() - start.getTime()); | |||
| File savefile = new File("d:/"); | |||
| if (!savefile.exists()) { | |||
| savefile.mkdirs(); | |||
| } | |||
| FileOutputStream fos = new FileOutputStream("d:/tt.xls"); | |||
| FileOutputStream fos = new FileOutputStream("d:/tt.xlsx"); | |||
| workbook.write(fos); | |||
| fos.close(); | |||
| savefile = new File("d:/1"); | |||
| @@ -49,7 +50,7 @@ public class ExcelExportUtilTest { | |||
| savefile.setWritable(true, false); | |||
| savefile.mkdirs(); | |||
| } | |||
| fos = new FileOutputStream("d:/1/tt3.xls"); | |||
| fos = new FileOutputStream("d:/1/tt3.xlsx"); | |||
| workbook.write(fos); | |||
| fos.close(); | |||
| } | |||
| @@ -57,7 +58,7 @@ public class ExcelExportUtilTest { | |||
| @Before | |||
| public void testBefore() { | |||
| for (int i = 0; i < 2; i++) { | |||
| for (int i = 0; i < 2000000; i++) { | |||
| courseEntity = new CourseEntity(); | |||
| courseEntity.setId("1131"); | |||
| courseEntity.setName("海贼王必修(" + (i + 1) + ")"); | |||
| @@ -41,8 +41,7 @@ public class JeecgMapExcelView extends AbstractExcelView { | |||
| new ExcelExportServer().createSheetForMap(hssfWorkbook, | |||
| (ExportParams) model.get(MapExcelConstants.PARAMS), | |||
| (List<ExcelExportEntity>) model.get(MapExcelConstants.ENTITY_LIST), | |||
| (Collection<? extends Map<?, ?>>) model.get(MapExcelConstants.MAP_LIST), | |||
| ((ExportParams) model.get(MapExcelConstants.PARAMS)).getType()); | |||
| (Collection<? extends Map<?, ?>>) model.get(MapExcelConstants.MAP_LIST)); | |||
| } | |||
| public boolean isIE(HttpServletRequest request) { | |||
| @@ -9,7 +9,6 @@ import javax.servlet.http.HttpServletResponse; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.jeecgframework.poi.excel.entity.ExportParams; | |||
| import org.jeecgframework.poi.excel.entity.vo.MapExcelConstants; | |||
| import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants; | |||
| import org.jeecgframework.poi.excel.export.ExcelExportServer; | |||
| import org.springframework.web.servlet.view.document.AbstractExcelView; | |||
| @@ -42,15 +41,13 @@ public class JeecgSingleExcelView extends AbstractExcelView { | |||
| new ExcelExportServer().createSheet(hssfWorkbook, | |||
| (ExportParams) map.get(NormalExcelConstants.PARAMS), | |||
| (Class<?>) map.get(NormalExcelConstants.CLASS), | |||
| (Collection<?>) map.get(NormalExcelConstants.DATA_LIST), | |||
| ((ExportParams) model.get(MapExcelConstants.PARAMS)).getType()); | |||
| (Collection<?>) map.get(NormalExcelConstants.DATA_LIST)); | |||
| } | |||
| } else { | |||
| new ExcelExportServer().createSheet(hssfWorkbook, | |||
| (ExportParams) model.get(NormalExcelConstants.PARAMS), | |||
| (Class<?>) model.get(NormalExcelConstants.CLASS), | |||
| (Collection<?>) model.get(NormalExcelConstants.DATA_LIST), | |||
| ((ExportParams) model.get(MapExcelConstants.PARAMS)).getType()); | |||
| (Collection<?>) model.get(NormalExcelConstants.DATA_LIST)); | |||
| } | |||
| } | |||
| @@ -24,10 +24,7 @@ | |||
| <properties> | |||
| <sub.version>2.0.7-SNAPSHOT</sub.version> | |||
| <poi-ooxml-schemas.version>3.9</poi-ooxml-schemas.version> | |||
| <poi-ooxml.version>3.9</poi-ooxml.version> | |||
| <poi-scratchpad.version>3.9</poi-scratchpad.version> | |||
| <poi.version>3.9</poi.version> | |||
| <poi.version>3.11</poi.version> | |||
| <guava.version>16.0.1</guava.version> | |||
| <reflectasm.version>1.09</reflectasm.version> | |||
| <commons-lang.version>2.6</commons-lang.version> | |||
| @@ -46,17 +43,17 @@ | |||
| <dependency> | |||
| <groupId>org.apache.poi</groupId> | |||
| <artifactId>poi-ooxml</artifactId> | |||
| <version>${poi-ooxml.version}</version> | |||
| <version>${poi.version}</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>org.apache.poi</groupId> | |||
| <artifactId>poi-ooxml-schemas</artifactId> | |||
| <version>${poi-ooxml-schemas.version}</version> | |||
| <version>${poi.version}</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>org.apache.poi</groupId> | |||
| <artifactId>poi-scratchpad</artifactId> | |||
| <version>${poi-scratchpad.version}</version> | |||
| <version>${poi.version}</version> | |||
| </dependency> | |||
| <!-- google 工具类 --> | |||