| @@ -6,9 +6,11 @@ import java.util.Map; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| 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.params.ExcelExportEntity; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| import org.jeecgframework.poi.excel.export.ExcelExportServer; | |||
| import org.jeecgframework.poi.excel.export.template.ExcelExportOfTemplateUtil; | |||
| @@ -29,14 +31,20 @@ public final class ExcelExportUtil { | |||
| * @param dataSet | |||
| * Excel对象数据List | |||
| */ | |||
| public static HSSFWorkbook exportExcel(ExportParams entity, Class<?> pojoClass, | |||
| Collection<?> dataSet) { | |||
| HSSFWorkbook workbook = new HSSFWorkbook(); | |||
| new ExcelExportServer().createSheet(workbook, entity, pojoClass, dataSet); | |||
| public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass, | |||
| Collection<?> dataSet) { | |||
| Workbook workbook; | |||
| if (entity.getType().equals(PoiBaseConstants.HSSF)) { | |||
| workbook = new HSSFWorkbook(); | |||
| } else { | |||
| workbook = new XSSFWorkbook(); | |||
| } | |||
| new ExcelExportServer().createSheet(workbook, entity, pojoClass, dataSet, entity.getType()); | |||
| return workbook; | |||
| } | |||
| /** | |||
| * 根据Map创建对应的Excel | |||
| * @param entity | |||
| * 表格标题属性 | |||
| * @param pojoClass | |||
| @@ -44,10 +52,16 @@ public final class ExcelExportUtil { | |||
| * @param dataSet | |||
| * Excel对象数据List | |||
| */ | |||
| public static HSSFWorkbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, | |||
| Collection<? extends Map<?, ?>> dataSet) { | |||
| HSSFWorkbook workbook = new HSSFWorkbook(); | |||
| new ExcelExportServer().createSheetForMap(workbook, entity, entityList, dataSet); | |||
| public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, | |||
| Collection<? extends Map<?, ?>> dataSet) { | |||
| Workbook workbook; | |||
| if (entity.getType().equals(PoiBaseConstants.HSSF)) { | |||
| workbook = new HSSFWorkbook(); | |||
| } else { | |||
| workbook = new XSSFWorkbook(); | |||
| } | |||
| new ExcelExportServer().createSheetForMap(workbook, entity, entityList, dataSet, | |||
| entity.getType()); | |||
| return workbook; | |||
| } | |||
| @@ -59,12 +73,17 @@ public final class ExcelExportUtil { | |||
| * Collection 数据 | |||
| * @return | |||
| */ | |||
| public static HSSFWorkbook exportExcel(List<Map<String, Object>> list) { | |||
| HSSFWorkbook workbook = new HSSFWorkbook(); | |||
| public static Workbook exportExcel(List<Map<String, Object>> list, String type) { | |||
| Workbook workbook; | |||
| if (type.equals(PoiBaseConstants.HSSF)) { | |||
| workbook = new HSSFWorkbook(); | |||
| } else { | |||
| workbook = new XSSFWorkbook(); | |||
| } | |||
| 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")); | |||
| (Class<?>) map.get("entity"), (Collection<?>) map.get("data"), type); | |||
| } | |||
| return workbook; | |||
| } | |||
| @@ -1,6 +1,7 @@ | |||
| package org.jeecgframework.poi.excel.entity; | |||
| import org.apache.poi.hssf.util.HSSFColor; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| /** | |||
| * Excel 导出参数 | |||
| @@ -49,9 +50,15 @@ public class ExportParams extends ExcelBaseParams { | |||
| * 属性说明行的颜色 例如:HSSFColor.SKY_BLUE.index 默认 | |||
| */ | |||
| private short headerColor = HSSFColor.SKY_BLUE.index; | |||
| /** | |||
| * Excel 导出版本 | |||
| */ | |||
| private String type = PoiBaseConstants.HSSF; | |||
| public ExportParams() { | |||
| } | |||
| public ExportParams(String title, String sheetName) { | |||
| this.title = title; | |||
| this.sheetName = sheetName; | |||
| @@ -134,4 +141,12 @@ public class ExportParams extends ExcelBaseParams { | |||
| public void setTitleHeight(short titleHeight) { | |||
| this.titleHeight = titleHeight; | |||
| } | |||
| public String getType() { | |||
| return type; | |||
| } | |||
| public void setType(String type) { | |||
| this.type = type; | |||
| } | |||
| } | |||
| @@ -5,13 +5,29 @@ package org.jeecgframework.poi.excel.entity.vo; | |||
| * Created by jue on 14-4-21. | |||
| */ | |||
| public interface PoiBaseConstants { | |||
| /** | |||
| * 字段属性对应方法 | |||
| */ | |||
| public static String GET = "get"; | |||
| /** | |||
| * 字段属性对应方法 | |||
| */ | |||
| public static String SET = "set"; | |||
| /** | |||
| * 字段属性对应方法 | |||
| */ | |||
| public static String IS = "is"; | |||
| /** | |||
| * 是否增加属性列 | |||
| */ | |||
| public static String IS_ADD_INDEX = "isAddIndex"; | |||
| /** | |||
| * 03版本Excel | |||
| */ | |||
| public static String HSSF = "HSSF"; | |||
| /** | |||
| * 07版本Excel | |||
| */ | |||
| public static String XSSF = "XSSF"; | |||
| } | |||
| @@ -9,15 +9,13 @@ import java.util.Iterator; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |||
| import org.apache.poi.hssf.usermodel.HSSFDataFormat; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.hssf.util.HSSFColor; | |||
| import org.apache.poi.ss.usermodel.BuiltinFormats; | |||
| import org.apache.poi.ss.usermodel.CellStyle; | |||
| import org.apache.poi.ss.usermodel.Drawing; | |||
| import org.apache.poi.ss.usermodel.Font; | |||
| import org.apache.poi.ss.usermodel.Row; | |||
| import org.apache.poi.ss.usermodel.Sheet; | |||
| 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; | |||
| @@ -40,12 +38,12 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| private final static Logger logger = LoggerFactory.getLogger(ExcelExportServer.class); | |||
| private static final short cellFormat = HSSFDataFormat.getBuiltinFormat("TEXT"); | |||
| private static final short cellFormat = (short) BuiltinFormats.getBuiltinFormat("TEXT"); | |||
| // 最大行数,超过自动多Sheet | |||
| private final int MAX_NUM = 60000; | |||
| private int MAX_NUM = 60000; | |||
| private int createHeaderAndTitle(ExportParams entity, Sheet sheet, HSSFWorkbook workbook, | |||
| private int createHeaderAndTitle(ExportParams entity, Sheet sheet, Workbook workbook, | |||
| List<ExcelExportEntity> excelParams) { | |||
| int rows = 0, feildWidth = getFieldWidth(excelParams); | |||
| if (entity.getTitle() != null) { | |||
| @@ -64,8 +62,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| * @param workbook | |||
| * @param feildWidth | |||
| */ | |||
| public int createHeaderRow(ExportParams entity, Sheet sheet, HSSFWorkbook workbook, | |||
| int feildWidth) { | |||
| public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook, int feildWidth) { | |||
| Row row = sheet.createRow(0); | |||
| row.setHeight(entity.getTitleHeight()); | |||
| createStringCell(row, 0, entity.getTitle(), getHeaderStyle(workbook, entity), null); | |||
| @@ -73,8 +70,8 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| if (entity.getSecondTitle() != null) { | |||
| row = sheet.createRow(1); | |||
| row.setHeight(entity.getSecondTitleHeight()); | |||
| HSSFCellStyle style = workbook.createCellStyle(); | |||
| style.setAlignment(HSSFCellStyle.ALIGN_RIGHT); | |||
| CellStyle style = workbook.createCellStyle(); | |||
| style.setAlignment(CellStyle.ALIGN_RIGHT); | |||
| createStringCell(row, 0, entity.getSecondTitle(), style, null); | |||
| sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, feildWidth)); | |||
| return 2; | |||
| @@ -82,14 +79,19 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| return 1; | |||
| } | |||
| public void createSheet(HSSFWorkbook workbook, ExportParams entity, Class<?> pojoClass, | |||
| Collection<?> dataSet) { | |||
| public void createSheet(Workbook workbook, ExportParams entity, Class<?> pojoClass, | |||
| Collection<?> dataSet, String type) { | |||
| if (logger.isDebugEnabled()) { | |||
| logger.debug("Excel export start ,class is {}", pojoClass); | |||
| logger.debug("Excel version is {}", type.equals(PoiBaseConstants.HSSF) ? "03" : "07"); | |||
| } | |||
| if (workbook == null || entity == null || pojoClass == null || dataSet == null) { | |||
| throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | |||
| } | |||
| if (type.equals(PoiBaseConstants.XSSF)) { | |||
| MAX_NUM = 1000000; | |||
| } | |||
| super.type = type; | |||
| Sheet sheet = null; | |||
| try { | |||
| sheet = workbook.createSheet(entity.getSheetName()); | |||
| @@ -103,7 +105,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields()); | |||
| } | |||
| // 创建表格属性 | |||
| Map<String, HSSFCellStyle> styles = createStyles(workbook); | |||
| Map<String, CellStyle> styles = createStyles(workbook); | |||
| Drawing patriarch = sheet.createDrawingPatriarch(); | |||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | |||
| if (entity.isAddIndex()) { | |||
| @@ -139,7 +141,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| } | |||
| // 发现还有剩余list 继续循环创建Sheet | |||
| if (dataSet.size() > 0) { | |||
| createSheet(workbook, entity, pojoClass, dataSet); | |||
| createSheet(workbook, entity, pojoClass, dataSet, type); | |||
| } | |||
| } catch (Exception e) { | |||
| @@ -148,12 +150,19 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| } | |||
| } | |||
| public void createSheetForMap(HSSFWorkbook workbook, ExportParams entity, | |||
| public void createSheetForMap(Workbook workbook, ExportParams entity, | |||
| List<ExcelExportEntity> entityList, | |||
| Collection<? extends Map<?, ?>> dataSet) { | |||
| Collection<? extends Map<?, ?>> dataSet, String type) { | |||
| if (logger.isDebugEnabled()) { | |||
| logger.debug("Excel version is {}", type.equals(PoiBaseConstants.HSSF) ? "03" : "07"); | |||
| } | |||
| if (workbook == null || entity == null || entityList == null || dataSet == null) { | |||
| throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR); | |||
| } | |||
| if (type.equals(PoiBaseConstants.XSSF)) { | |||
| MAX_NUM = 1000000; | |||
| } | |||
| super.type = type; | |||
| Sheet sheet = null; | |||
| try { | |||
| sheet = workbook.createSheet(entity.getSheetName()); | |||
| @@ -167,7 +176,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| needHanlderList = Arrays.asList(dataHanlder.getNeedHandlerFields()); | |||
| } | |||
| // 创建表格属性 | |||
| Map<String, HSSFCellStyle> styles = createStyles(workbook); | |||
| Map<String, CellStyle> styles = createStyles(workbook); | |||
| Drawing patriarch = sheet.createDrawingPatriarch(); | |||
| List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>(); | |||
| if (entity.isAddIndex()) { | |||
| @@ -199,7 +208,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| } | |||
| // 发现还有剩余list 继续循环创建Sheet | |||
| if (dataSet.size() > 0) { | |||
| createSheetForMap(workbook, entity, entityList, dataSet); | |||
| createSheetForMap(workbook, entity, entityList, dataSet, type); | |||
| } | |||
| } catch (Exception e) { | |||
| @@ -209,8 +218,8 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| } | |||
| } | |||
| private Map<String, HSSFCellStyle> createStyles(HSSFWorkbook workbook) { | |||
| Map<String, HSSFCellStyle> map = new HashMap<String, HSSFCellStyle>(); | |||
| private Map<String, CellStyle> createStyles(Workbook workbook) { | |||
| Map<String, CellStyle> map = new HashMap<String, CellStyle>(); | |||
| map.put("one", getOneStyle(workbook, false)); | |||
| map.put("oneWrap", getOneStyle(workbook, true)); | |||
| map.put("two", getTwoStyle(workbook, false)); | |||
| @@ -224,7 +233,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| * @param title | |||
| * @param index | |||
| */ | |||
| private int createTitleRow(ExportParams title, Sheet sheet, HSSFWorkbook workbook, int index, | |||
| private int createTitleRow(ExportParams title, Sheet sheet, Workbook workbook, int index, | |||
| List<ExcelExportEntity> excelParams) { | |||
| Row row = sheet.createRow(index); | |||
| int rows = getRowNums(excelParams); | |||
| @@ -264,25 +273,25 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| * @param workbook | |||
| * @return | |||
| */ | |||
| public HSSFCellStyle getHeaderStyle(HSSFWorkbook workbook, ExportParams entity) { | |||
| HSSFCellStyle titleStyle = workbook.createCellStyle(); | |||
| public CellStyle getHeaderStyle(Workbook workbook, ExportParams entity) { | |||
| CellStyle titleStyle = workbook.createCellStyle(); | |||
| Font font = workbook.createFont(); | |||
| font.setFontHeightInPoints((short) 24); | |||
| titleStyle.setFont(font); | |||
| titleStyle.setFillForegroundColor(entity.getColor()); | |||
| titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |||
| titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); | |||
| titleStyle.setAlignment(CellStyle.ALIGN_CENTER); | |||
| titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); | |||
| return titleStyle; | |||
| } | |||
| public HSSFCellStyle getOneStyle(HSSFWorkbook workbook, boolean isWarp) { | |||
| HSSFCellStyle style = workbook.createCellStyle(); | |||
| public CellStyle getOneStyle(Workbook workbook, boolean isWarp) { | |||
| CellStyle style = workbook.createCellStyle(); | |||
| style.setBorderLeft((short) 1); // 左边框 | |||
| style.setBorderRight((short) 1); // 右边框 | |||
| style.setBorderBottom((short) 1); | |||
| style.setBorderTop((short) 1); | |||
| style.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |||
| style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); | |||
| style.setAlignment(CellStyle.ALIGN_CENTER); | |||
| style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); | |||
| style.setDataFormat(cellFormat); | |||
| if (isWarp) { | |||
| style.setWrapText(true); | |||
| @@ -305,7 +314,7 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| return 1; | |||
| } | |||
| public CellStyle getStyles(Map<String, HSSFCellStyle> map, boolean needOne, boolean isWrap) { | |||
| public CellStyle getStyles(Map<String, CellStyle> map, boolean needOne, boolean isWrap) { | |||
| if (needOne && isWrap) { | |||
| return map.get("oneWrap"); | |||
| } | |||
| @@ -324,26 +333,26 @@ public class ExcelExportServer extends ExcelExportBase { | |||
| * @param workbook | |||
| * @return | |||
| */ | |||
| public HSSFCellStyle getTitleStyle(HSSFWorkbook workbook, ExportParams entity) { | |||
| HSSFCellStyle titleStyle = workbook.createCellStyle(); | |||
| public CellStyle getTitleStyle(Workbook workbook, ExportParams entity) { | |||
| CellStyle titleStyle = workbook.createCellStyle(); | |||
| titleStyle.setFillForegroundColor(entity.getHeaderColor()); // 填充的背景颜色 | |||
| titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |||
| titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); | |||
| titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 填充图案 | |||
| titleStyle.setAlignment(CellStyle.ALIGN_CENTER); | |||
| titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); | |||
| titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案 | |||
| titleStyle.setWrapText(true); | |||
| return titleStyle; | |||
| } | |||
| public HSSFCellStyle getTwoStyle(HSSFWorkbook workbook, boolean isWarp) { | |||
| HSSFCellStyle style = workbook.createCellStyle(); | |||
| public CellStyle getTwoStyle(Workbook workbook, boolean isWarp) { | |||
| CellStyle style = workbook.createCellStyle(); | |||
| style.setBorderLeft((short) 1); // 左边框 | |||
| style.setBorderRight((short) 1); // 右边框 | |||
| style.setBorderBottom((short) 1); | |||
| style.setBorderTop((short) 1); | |||
| style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index); // 填充的背景颜色 | |||
| style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 填充图案 | |||
| style.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |||
| style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); | |||
| style.setFillForegroundColor((short) 41); // 填充的背景颜色 | |||
| style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案 | |||
| style.setAlignment(CellStyle.ALIGN_CENTER); | |||
| style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); | |||
| style.setDataFormat(cellFormat); | |||
| if (isWarp) { | |||
| style.setWrapText(true); | |||
| @@ -14,18 +14,19 @@ import java.util.Set; | |||
| import javax.imageio.ImageIO; | |||
| import org.apache.commons.lang.StringUtils; | |||
| import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |||
| import org.apache.poi.hssf.usermodel.HSSFClientAnchor; | |||
| import org.apache.poi.hssf.usermodel.HSSFRichTextString; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.Cell; | |||
| import org.apache.poi.ss.usermodel.CellStyle; | |||
| import org.apache.poi.ss.usermodel.ClientAnchor; | |||
| import org.apache.poi.ss.usermodel.Drawing; | |||
| import org.apache.poi.ss.usermodel.RichTextString; | |||
| import org.apache.poi.ss.usermodel.Row; | |||
| import org.apache.poi.ss.usermodel.Sheet; | |||
| 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.params.ExcelExportEntity; | |||
| import org.jeecgframework.poi.excel.entity.params.MergeEntity; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| @@ -39,7 +40,9 @@ import org.jeecgframework.poi.util.POIPublicUtil; | |||
| */ | |||
| public abstract class ExcelExportBase extends ExportBase { | |||
| private int currentIndex = 0; | |||
| private int currentIndex = 0; | |||
| protected String type = PoiBaseConstants.HSSF; | |||
| private boolean checkIsEqualByCellContents(MergeEntity mergeEntity, String text, Cell cell, | |||
| int[] delys, int rowNum) { | |||
| @@ -69,7 +72,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| */ | |||
| public int createCells(Drawing patriarch, int index, Object t, | |||
| List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, | |||
| Map<String, HSSFCellStyle> styles, short rowHeight) throws Exception { | |||
| Map<String, CellStyle> styles, short rowHeight) throws Exception { | |||
| ExcelExportEntity entity; | |||
| Row row = sheet.createRow(index); | |||
| row.setHeight(rowHeight); | |||
| @@ -128,29 +131,37 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| * @param entity | |||
| * @param row | |||
| * @param i | |||
| * @param string | |||
| * @param imagePath | |||
| * @param obj | |||
| * @throws Exception | |||
| */ | |||
| public void createImageCell(Drawing patriarch, ExcelExportEntity entity, Row row, int i, | |||
| String string, Object obj) throws Exception { | |||
| String imagePath, Object obj) throws Exception { | |||
| row.setHeight((short) (50 * entity.getHeight())); | |||
| row.createCell(i); | |||
| HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) i, row.getRowNum(), | |||
| (short) (i + 1), row.getRowNum() + 1); | |||
| if (StringUtils.isEmpty(string)) { | |||
| ClientAnchor anchor; | |||
| if (type.equals(PoiBaseConstants.HSSF)) { | |||
| anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) i, row.getRowNum(), (short) (i + 1), | |||
| row.getRowNum() + 1); | |||
| } else { | |||
| anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) i, row.getRowNum(), (short) (i + 1), | |||
| row.getRowNum() + 1); | |||
| } | |||
| if (StringUtils.isEmpty(imagePath)) { | |||
| return; | |||
| } | |||
| if (entity.getExportImageType() == 1) { | |||
| ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); | |||
| BufferedImage bufferImg; | |||
| try { | |||
| String path = POIPublicUtil.getWebRootPath(string); | |||
| String path = POIPublicUtil.getWebRootPath(imagePath); | |||
| path = path.replace("WEB-INF/classes/", ""); | |||
| path = path.replace("file:/", ""); | |||
| bufferImg = ImageIO.read(new File(path)); | |||
| ImageIO.write(bufferImg, | |||
| string.substring(string.indexOf(".") + 1, string.length()), byteArrayOut); | |||
| imagePath.substring(imagePath.indexOf(".") + 1, imagePath.length()), | |||
| byteArrayOut); | |||
| byte[] value = byteArrayOut.toByteArray(); | |||
| patriarch.createPicture(anchor, | |||
| row.getSheet().getWorkbook().addPicture(value, getImageType(value))); | |||
| @@ -168,7 +179,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| } | |||
| private int createIndexCell(Row row, Map<String, HSSFCellStyle> styles, int index, | |||
| private int createIndexCell(Row row, Map<String, CellStyle> styles, int index, | |||
| ExcelExportEntity excelExportEntity) { | |||
| if (excelExportEntity.getName().equals("序号") | |||
| && excelExportEntity.getFormat().equals(PoiBaseConstants.IS_ADD_INDEX)) { | |||
| @@ -188,8 +199,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| */ | |||
| public void createListCells(Drawing patriarch, int index, int cellNum, Object obj, | |||
| List<ExcelExportEntity> excelParams, Sheet sheet, | |||
| Workbook workbook, Map<String, HSSFCellStyle> styles) | |||
| throws Exception { | |||
| Workbook workbook, Map<String, CellStyle> styles) throws Exception { | |||
| ExcelExportEntity entity; | |||
| Row row; | |||
| if (sheet.getRow(index) == null) { | |||
| @@ -234,7 +244,12 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| public void createStringCell(Row row, int index, String text, CellStyle style, | |||
| ExcelExportEntity entity) { | |||
| Cell cell = row.createCell(index); | |||
| RichTextString Rtext = new HSSFRichTextString(text); | |||
| RichTextString Rtext; | |||
| if (type.equals(PoiBaseConstants.HSSF)) { | |||
| Rtext = new HSSFRichTextString(text); | |||
| } else { | |||
| Rtext = new XSSFRichTextString(text); | |||
| } | |||
| cell.setCellValue(Rtext); | |||
| if (style != null) { | |||
| cell.setCellStyle(style); | |||
| @@ -282,11 +297,11 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| public int getImageType(byte[] value) { | |||
| String type = POIPublicUtil.getFileExtendName(value); | |||
| if (type.equalsIgnoreCase("JPG")) { | |||
| return HSSFWorkbook.PICTURE_TYPE_JPEG; | |||
| return Workbook.PICTURE_TYPE_JPEG; | |||
| } else if (type.equalsIgnoreCase("PNG")) { | |||
| return HSSFWorkbook.PICTURE_TYPE_PNG; | |||
| return Workbook.PICTURE_TYPE_PNG; | |||
| } | |||
| return HSSFWorkbook.PICTURE_TYPE_JPEG; | |||
| return Workbook.PICTURE_TYPE_JPEG; | |||
| } | |||
| private Map<Integer, int[]> getMergeDataMap(List<ExcelExportEntity> excelParams) { | |||
| @@ -311,7 +326,7 @@ public abstract class ExcelExportBase extends ExportBase { | |||
| return mergeMap; | |||
| } | |||
| public CellStyle getStyles(Map<String, HSSFCellStyle> styles, boolean b, boolean wrap) { | |||
| public CellStyle getStyles(Map<String, CellStyle> styles, boolean b, boolean wrap) { | |||
| return null; | |||
| } | |||
| @@ -2,36 +2,39 @@ package org.jeecgframework.poi.excel; | |||
| import java.util.Map; | |||
| import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.CellStyle; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.jeecgframework.poi.excel.entity.ExportParams; | |||
| import org.jeecgframework.poi.excel.export.ExcelExportServer; | |||
| /** | |||
| * 自己定义导出的样式 | |||
| * @author JueYue | |||
| * @date 2014年12月2日 上午10:32:44 | |||
| */ | |||
| public class MyExcelExport extends ExcelExportServer { | |||
| @Override | |||
| public HSSFCellStyle getHeaderStyle(HSSFWorkbook workbook, ExportParams entity) { | |||
| public CellStyle getHeaderStyle(Workbook workbook, ExportParams entity) { | |||
| return super.getHeaderStyle(workbook, entity); | |||
| } | |||
| @Override | |||
| public HSSFCellStyle getOneStyle(HSSFWorkbook workbook, boolean isWarp) { | |||
| public CellStyle getOneStyle(Workbook workbook, boolean isWarp) { | |||
| return super.getOneStyle(workbook, isWarp); | |||
| } | |||
| @Override | |||
| public CellStyle getStyles(Map<String, HSSFCellStyle> map, boolean needOne, boolean isWrap) { | |||
| public CellStyle getStyles(Map<String, CellStyle> map, boolean needOne, boolean isWrap) { | |||
| return super.getStyles(map, needOne, isWrap); | |||
| } | |||
| @Override | |||
| public HSSFCellStyle getTitleStyle(HSSFWorkbook workbook, ExportParams entity) { | |||
| public CellStyle getTitleStyle(Workbook workbook, ExportParams entity) { | |||
| return super.getTitleStyle(workbook, entity); | |||
| } | |||
| @Override | |||
| public HSSFCellStyle getTwoStyle(HSSFWorkbook workbook, boolean isWarp) { | |||
| public CellStyle getTwoStyle(Workbook workbook, boolean isWarp) { | |||
| return super.getTwoStyle(workbook, isWarp); | |||
| } | |||
| @@ -9,14 +9,18 @@ import java.util.List; | |||
| import java.util.Map; | |||
| 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.params.ExcelExportEntity; | |||
| import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants; | |||
| import org.junit.Test; | |||
| public class ExcelExportForMap { | |||
| @Test | |||
| /** | |||
| * Map 测试 | |||
| */ | |||
| // @Test | |||
| public void test() { | |||
| try { | |||
| List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>(); | |||
| @@ -32,8 +36,8 @@ public class ExcelExportForMap { | |||
| list.add(map); | |||
| } | |||
| HSSFWorkbook workbook = ExcelExportUtil.exportExcel(new ExportParams("测试", "测试"), | |||
| entity, list); | |||
| Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("测试", "测试"), entity, | |||
| list); | |||
| FileOutputStream fos = new FileOutputStream("d:/tt.xls"); | |||
| workbook.write(fos); | |||
| fos.close(); | |||
| @@ -44,4 +48,34 @@ public class ExcelExportForMap { | |||
| } | |||
| } | |||
| @Test | |||
| public void testMany() { | |||
| try { | |||
| List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>(); | |||
| for (int i = 0; i < 500; i++) { | |||
| entity.add(new ExcelExportEntity("姓名" + i, "name" + i)); | |||
| } | |||
| List<Map<String, String>> list = new ArrayList<Map<String, String>>(); | |||
| Map<String, String> map; | |||
| for (int i = 0; i < 10; i++) { | |||
| map = new HashMap<String, String>(); | |||
| for (int j = 0; j < 500; j++) { | |||
| map.put("name" + j, j + "_" + i); | |||
| } | |||
| list.add(map); | |||
| } | |||
| ExportParams params = new ExportParams("测试", "测试"); | |||
| params.setType(PoiBaseConstants.XSSF); | |||
| Workbook workbook = ExcelExportUtil.exportExcel(params, entity, list); | |||
| FileOutputStream fos = new FileOutputStream("d:/tt.xlsx"); | |||
| workbook.write(fos); | |||
| fos.close(); | |||
| } catch (FileNotFoundException e) { | |||
| e.printStackTrace(); | |||
| } catch (IOException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| } | |||
| @@ -6,6 +6,7 @@ import java.util.Date; | |||
| import java.util.List; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.jeecgframework.poi.entity.CourseEntity; | |||
| import org.jeecgframework.poi.entity.StudentEntity; | |||
| import org.jeecgframework.poi.entity.TeacherEntity; | |||
| @@ -64,7 +65,7 @@ public class ExcelExportUtilDataHandlerTest { | |||
| CourseHanlder hanlder = new CourseHanlder(); | |||
| hanlder.setNeedHandlerFields(new String[] { "课程名称" }); | |||
| exportParams.setDataHanlder(hanlder); | |||
| HSSFWorkbook workbook = ExcelExportUtil.exportExcel(exportParams, CourseEntity.class, list); | |||
| Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CourseEntity.class, list); | |||
| FileOutputStream fos = new FileOutputStream("d:/tt.xls"); | |||
| workbook.write(fos); | |||
| fos.close(); | |||
| @@ -6,6 +6,7 @@ import java.util.Date; | |||
| import java.util.List; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.jeecgframework.poi.entity.CourseEntity; | |||
| import org.jeecgframework.poi.entity.StudentEntity; | |||
| import org.jeecgframework.poi.entity.TeacherEntity; | |||
| @@ -69,7 +70,7 @@ public class ExcelExportUtilIdTest { | |||
| public void testExportExcel() throws Exception { | |||
| ExportParams params = new ExportParams("2412312", "测试", "测试"); | |||
| params.setAddIndex(true); | |||
| HSSFWorkbook workbook = ExcelExportUtil.exportExcel(params, TeacherEntity.class, telist); | |||
| Workbook workbook = ExcelExportUtil.exportExcel(params, TeacherEntity.class, telist); | |||
| FileOutputStream fos = new FileOutputStream("d:/tt.xls"); | |||
| workbook.write(fos); | |||
| fos.close(); | |||
| @@ -7,7 +7,6 @@ import java.util.Date; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |||
| import org.apache.poi.ss.usermodel.Workbook; | |||
| import org.jeecgframework.poi.entity.CourseEntity; | |||
| import org.jeecgframework.poi.entity.StudentEntity; | |||
| @@ -38,7 +37,7 @@ public class ExcelExportUtilTest { | |||
| list.add(courseEntity); | |||
| } | |||
| Date start = new Date(); | |||
| HSSFWorkbook workbook = ExcelExportUtil.exportExcel( | |||
| Workbook workbook = ExcelExportUtil.exportExcel( | |||
| new ExportParams("2412312", "测试", "测试"), CourseEntity.class, list); | |||
| System.out.println(new Date().getTime() - start.getTime()); | |||
| File savefile = new File("d:/"); | |||
| @@ -97,7 +96,7 @@ public class ExcelExportUtilTest { | |||
| //@Test | |||
| public void testExportExcel() throws Exception { | |||
| Date start = new Date(); | |||
| HSSFWorkbook workbook = ExcelExportUtil.exportExcel( | |||
| Workbook workbook = ExcelExportUtil.exportExcel( | |||
| new ExportParams("2412312", "测试", "测试"), CourseEntity.class, list); | |||
| System.out.println(new Date().getTime() - start.getTime()); | |||
| File savefile = new File("d:/"); | |||
| @@ -117,7 +116,7 @@ public class ExcelExportUtilTest { | |||
| //@Test | |||
| public void testExportTitleExcel() throws Exception { | |||
| Date start = new Date(); | |||
| HSSFWorkbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试"), | |||
| Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试"), | |||
| CourseEntity.class, list); | |||
| System.out.println(new Date().getTime() - start.getTime()); | |||
| File savefile = new File("d:/"); | |||
| @@ -41,7 +41,8 @@ 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)); | |||
| (Collection<? extends Map<?, ?>>) model.get(MapExcelConstants.MAP_LIST), | |||
| ((ExportParams) model.get(MapExcelConstants.PARAMS)).getType()); | |||
| } | |||
| public boolean isIE(HttpServletRequest request) { | |||
| @@ -9,6 +9,7 @@ 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; | |||
| @@ -41,13 +42,15 @@ 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)); | |||
| (Collection<?>) map.get(NormalExcelConstants.DATA_LIST), | |||
| ((ExportParams) model.get(MapExcelConstants.PARAMS)).getType()); | |||
| } | |||
| } else { | |||
| new ExcelExportServer().createSheet(hssfWorkbook, | |||
| (ExportParams) model.get(NormalExcelConstants.PARAMS), | |||
| (Class<?>) model.get(NormalExcelConstants.CLASS), | |||
| (Collection<?>) model.get(NormalExcelConstants.DATA_LIST)); | |||
| (Collection<?>) model.get(NormalExcelConstants.DATA_LIST), | |||
| ((ExportParams) model.get(MapExcelConstants.PARAMS)).getType()); | |||
| } | |||
| } | |||