diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/cache/HtmlCache.java b/easypoi-base/src/main/java/org/jeecgframework/poi/cache/HtmlCache.java new file mode 100644 index 0000000..5c6b785 --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/cache/HtmlCache.java @@ -0,0 +1,40 @@ +package org.jeecgframework.poi.cache; + +import java.util.concurrent.TimeUnit; + +import org.jeecgframework.poi.excel.entity.ExcelToHtmlParams; +import org.jeecgframework.poi.excel.html.ExcelToHtmlServer; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + +/** + * Excel 转变成为Html 的缓存 + * @author JueYue + * @date 2015年8月7日 下午1:29:47 + */ +public class HtmlCache { + + private static LoadingCache loadingCache; + + static { + loadingCache = CacheBuilder.newBuilder().expireAfterWrite(7, TimeUnit.DAYS).maximumSize(200) + .build(new CacheLoader() { + @Override + public String load(ExcelToHtmlParams params) throws Exception { + return new ExcelToHtmlServer(params).printPage(); + } + }); + } + + public static String getHtml(ExcelToHtmlParams params) { + try { + return loadingCache.get(params); + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + +} diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelToHtmlUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelToHtmlUtil.java index cf3e9eb..0b3582a 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelToHtmlUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelToHtmlUtil.java @@ -1,7 +1,8 @@ package org.jeecgframework.poi.excel; import org.apache.poi.ss.usermodel.Workbook; -import org.jeecgframework.poi.excel.html.ExcelToHtmlServer; +import org.jeecgframework.poi.cache.HtmlCache; +import org.jeecgframework.poi.excel.entity.ExcelToHtmlParams; /** * Excel 变成界面 @@ -19,7 +20,16 @@ public final class ExcelToHtmlUtil { * @return */ public static String toTableHtml(Workbook wb) { - return new ExcelToHtmlServer(wb, false, 0).printPage(); + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, false, 0, null)); + } + + /** + * 转换成为Table,显示图片 + * @param wb Excel + * @return + */ + public static String toTableHtml(Workbook wb, String path) { + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, false, 0, path)); } /** @@ -29,17 +39,36 @@ public final class ExcelToHtmlUtil { * @return */ public static String toTableHtml(Workbook wb, int sheetNum) { - return new ExcelToHtmlServer(wb, false, sheetNum).printPage(); + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, false, sheetNum, null)); } /** - * 转换成为完整界面 + * 转换成为Table,显示图片 * @param wb Excel * @param sheetNum sheetNum * @return */ + public static String toTableHtml(Workbook wb, int sheetNum, String path) { + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, false, sheetNum, path)); + } + + /** + * 转换成为完整界面 + * @param wb Excel + * @return + */ public static String toAllHtml(Workbook wb) { - return new ExcelToHtmlServer(wb, true, 0).printPage(); + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, true, 0, null)); + } + + /** + * 转换成为完整界面,显示图片 + * @param wb Excel + * @param path 图片保存路径 + * @return + */ + public static String toAllHtml(Workbook wb, String path) { + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, true, 0, path)); } /** @@ -49,7 +78,17 @@ public final class ExcelToHtmlUtil { * @return */ public static String toAllHtml(Workbook wb, int sheetNum) { - return new ExcelToHtmlServer(wb, true, sheetNum).printPage(); + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, true, sheetNum, null)); + } + + /** + * 转换成为完整界面,显示图片 + * @param wb Excel + * @param sheetNum sheetNum + * @return + */ + public static String toAllHtml(Workbook wb, int sheetNum, String path) { + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, true, sheetNum, path)); } } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ExcelToHtmlParams.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ExcelToHtmlParams.java new file mode 100644 index 0000000..2717645 --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/entity/ExcelToHtmlParams.java @@ -0,0 +1,97 @@ +package org.jeecgframework.poi.excel.entity; + +import org.apache.poi.ss.usermodel.Workbook; + +/** + * Excel到HTML的参数 + * @author JueYue + * @date 2015年8月7日 下午1:45:46 + */ +public class ExcelToHtmlParams { + /** + * Excel + */ + private Workbook wb; + /** + * 是不是全界面 + */ + private boolean completeHTML; + /** + * 位置 + */ + private int sheetNum; + /** + * 图片保存路径 + */ + private String path; + + public ExcelToHtmlParams(Workbook wb, boolean completeHTML, int sheetNum, String path) { + this.wb = wb; + this.completeHTML = completeHTML; + this.sheetNum = sheetNum; + this.path = path; + } + + public Workbook getWb() { + return wb; + } + + public void setWb(Workbook wb) { + this.wb = wb; + } + + public boolean isCompleteHTML() { + return completeHTML; + } + + public void setCompleteHTML(boolean completeHTML) { + this.completeHTML = completeHTML; + } + + public int getSheetNum() { + return sheetNum; + } + + public void setSheetNum(int sheetNum) { + this.sheetNum = sheetNum; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof ExcelToHtmlParams) { + ExcelToHtmlParams other = (ExcelToHtmlParams) obj; + if (!this.wb.equals(other.getWb()) || this.completeHTML != other.completeHTML + || this.sheetNum != other.getSheetNum()) { + return false; + } + if ((this.path == null && other.getPath() != null) + || !this.path.equals(other.getPath())) { + return false; + } + return true; + } + return false; + } + + /** + * 保持一个参数一个对象的hashCode + */ + @Override + public int hashCode() { + StringBuilder sb = new StringBuilder(); + sb.append(wb.hashCode()); + sb.append(path); + sb.append(completeHTML); + sb.append(sheetNum); + return sb.toString().hashCode(); + } + +} diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/ExcelToHtmlServer.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/ExcelToHtmlServer.java index 657221c..6ac4c27 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/ExcelToHtmlServer.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/ExcelToHtmlServer.java @@ -1,16 +1,32 @@ package org.jeecgframework.poi.excel.html; +import java.io.File; +import java.io.FileOutputStream; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.Formatter; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.hssf.usermodel.HSSFSheet; +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.PictureData; 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.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.jeecgframework.poi.excel.entity.ExcelToHtmlParams; import org.jeecgframework.poi.excel.html.helper.CellValueHelper; import org.jeecgframework.poi.excel.html.helper.MergedRegionHelper; import org.jeecgframework.poi.excel.html.helper.StylerHelper; +import org.jeecgframework.poi.util.PoiPublicUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Excel转换成Html 服务 @@ -19,9 +35,15 @@ import org.jeecgframework.poi.excel.html.helper.StylerHelper; */ public class ExcelToHtmlServer { - private Workbook wb; - private int sheetNum; - private int cssRandom; + private static final Logger LOGGER = LoggerFactory.getLogger(ExcelToHtmlServer.class); + + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy_MM_dd"); + + private String today; + + private Workbook wb; + private int sheetNum; + private int cssRandom; /*是不是完成界面*/ private boolean completeHTML; @@ -30,27 +52,38 @@ public class ExcelToHtmlServer { private boolean gotBounds; private int firstColumn; private int endColumn; + private String imageCachePath; private static final String COL_HEAD_CLASS = "colHeader"; //private static final String ROW_HEAD_CLASS = "rowHeader"; private static final String DEFAULTS_CLASS = "excelDefaults"; - public ExcelToHtmlServer(Workbook wb, boolean completeHTML, int sheetNum) { - this.wb = wb; - this.completeHTML = completeHTML; - this.sheetNum = sheetNum; + //图片缓存 + private Map pictures = new HashMap(); + + public ExcelToHtmlServer(ExcelToHtmlParams params) { + this.wb = params.getWb(); + this.completeHTML = params.isCompleteHTML(); + this.sheetNum = params.getSheetNum(); cssRandom = (int) Math.ceil(Math.random() * 1000); + this.imageCachePath = params.getPath(); + today = DATE_FORMAT.format(new Date()); } public String printPage() { try { + System.out.println("--来了一次请求--"); ensureOut(); if (completeHTML) { out.format("%n"); out.format("%n"); - out.format("%n"); + out.format( + "%n"); out.format("%n"); } + if (StringUtils.isNotEmpty(imageCachePath)) { + getPictures(); + } new StylerHelper(wb, out, sheetNum, cssRandom); if (completeHTML) { out.format("%n"); @@ -62,10 +95,26 @@ public class ExcelToHtmlServer { out.format("%n"); } return out.toString(); + } catch (Exception e) { + LOGGER.error(e.getMessage(), e); } finally { if (out != null) out.close(); } + return null; + } + + /** + * 获取Sheet缓存的图片 + */ + private void getPictures() { + if (wb instanceof XSSFWorkbook) { + pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) wb.getSheetAt(sheetNum), + (XSSFWorkbook) wb); + } else { + pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) wb.getSheetAt(sheetNum), + (HSSFWorkbook) wb); + } } private void print() { @@ -166,6 +215,13 @@ public class ExcelToHtmlServer { content = cellValueHelper.getHtmlValue(cell); } } + if (pictures.containsKey((rowIndex - 1) + "_" + i)) { + content = ""; + } if (mergedRegionHelper.isMergedRegion(rowIndex, i)) { Integer[] rowAndColSpan = mergedRegionHelper.getRowAndColSpan(rowIndex, i); out.format(" %s%n", @@ -182,6 +238,60 @@ public class ExcelToHtmlServer { out.format("%n"); } + /** + * 获取图片最大宽度 + * @param colIndex + * @param sheet + * @param span + * @return + */ + private int getImageMaxWidth(Integer[] rowAndColSpan, int colIndex, Sheet sheet) { + if (rowAndColSpan == null) { + return sheet.getColumnWidth(colIndex) / 32; + } + int maxWidth = 0; + for (int i = 0; i < rowAndColSpan[1]; i++) { + maxWidth += sheet.getColumnWidth(colIndex + i) / 32; + } + return maxWidth; + } + + /** + * 获取图片路径 + * @param pictureData + * @return + */ + private String getImageSrc(PictureData pictureData) { + if (pictureData == null) { + return ""; + } + byte[] data = pictureData.getData(); + String fileName = "pic" + Math.round(Math.random() * 100000000000L); + fileName += "." + PoiPublicUtil.getFileExtendName(data); + if (!imageCachePath.startsWith("/") && !imageCachePath.contains(":")) { + imageCachePath = PoiPublicUtil.getWebRootPath(imageCachePath); + } + File savefile = new File(imageCachePath + "/" + today); + if (!savefile.exists()) { + savefile.mkdirs(); + } + savefile = new File(imageCachePath + "/" + today + "/" + fileName); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(savefile); + fos.write(data); + } catch (Exception e) { + LOGGER.error(e.getMessage(), e); + } finally { + try { + fos.close(); + } catch (Exception e) { + LOGGER.error(e.getMessage(), e); + } + } + return imageCachePath + "/" + today + "/" + fileName; + } + private String styleName(CellStyle style) { if (style == null) return ""; diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java index f8ecb95..598c006 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java @@ -21,7 +21,6 @@ import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.math.BigDecimal; import java.net.URISyntaxException; import java.util.ArrayList;