From e04b4a3f9682de1c5d818cd5c7987002fa250ce9 Mon Sep 17 00:00:00 2001 From: jueyue Date: Wed, 12 Jul 2017 20:44:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=AA=E5=B0=8Fbu?= =?UTF-8?q?g,=E8=B0=83=E6=95=B4=E4=BA=86Excel=E5=92=8CHtml=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E7=9A=84=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easypoi/excel/ExcelToHtmlUtil.java | 95 ----------- ...ToExcelUtil.java => ExcelXorHtmlUtil.java} | 147 ++++++++++-------- .../excel/entity/ExcelToHtmlParams.java | 39 ++++- .../excel/export/base/ExcelExportBase.java | 2 +- .../word/parse/excel/ExcelEntityParse.java | 2 +- 5 files changed, 124 insertions(+), 161 deletions(-) delete mode 100644 easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelToHtmlUtil.java rename easypoi-base/src/main/java/cn/afterturn/easypoi/excel/{HtmlToExcelUtil.java => ExcelXorHtmlUtil.java} (53%) diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelToHtmlUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelToHtmlUtil.java deleted file mode 100644 index d97ea1e..0000000 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelToHtmlUtil.java +++ /dev/null @@ -1,95 +0,0 @@ -package cn.afterturn.easypoi.excel; - -import org.apache.poi.ss.usermodel.Workbook; - -import cn.afterturn.easypoi.cache.HtmlCache; -import cn.afterturn.easypoi.excel.entity.ExcelToHtmlParams; - -/** - * Excel 变成界面 - * @author JueYue - * 2015年5月10日 上午11:51:48 - */ -public class ExcelToHtmlUtil { - - private ExcelToHtmlUtil() { - } - - /** - * 转换成为Table - * @param wb Excel - * @return - */ - public static String toTableHtml(Workbook wb) { - 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)); - } - - /** - * 转换成为Table - * @param wb Excel - * @param sheetNum sheetNum - * @return - */ - public static String toTableHtml(Workbook wb, int sheetNum) { - 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 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)); - } - - /** - * 转换成为完整界面 - * @param wb Excel - * @param sheetNum sheetNum - * @return - */ - public static String toAllHtml(Workbook wb, int sheetNum) { - 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/cn/afterturn/easypoi/excel/HtmlToExcelUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelXorHtmlUtil.java similarity index 53% rename from easypoi-base/src/main/java/cn/afterturn/easypoi/excel/HtmlToExcelUtil.java rename to easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelXorHtmlUtil.java index 7f2d9db..7a2e837 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/HtmlToExcelUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/ExcelXorHtmlUtil.java @@ -1,61 +1,86 @@ -/** - * Copyright 2013-2017 JueYue (qrb.jueyue@gmail.com) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.afterturn.easypoi.excel; - -import java.io.IOException; -import java.io.InputStream; - -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.util.IOUtils; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; - -import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; -import cn.afterturn.easypoi.excel.html.HtmlToExcelServer; -import cn.afterturn.easypoi.exception.excel.ExcelExportException; -import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; - -/** - * 基于Excel和Html的互换 - * @author JueYue - * 2017年3月27日 - */ -public final class HtmlToExcelUtil { - - private HtmlToExcelUtil() { - - } - - public static Workbook htmlToExcel(String html, ExcelType type) { - Workbook workbook = null; - if (ExcelType.HSSF.equals(type)) { - workbook = new HSSFWorkbook(); - } else { - workbook = new XSSFWorkbook(); - } - new HtmlToExcelServer().createSheet(html, workbook); - return workbook; - } - - public static Workbook htmlToExcel(InputStream is, ExcelType type) { - try { - return htmlToExcel(new String(IOUtils.toByteArray(is)), type); - } catch (IOException e) { - throw new ExcelExportException(ExcelExportEnum.HTML_ERROR, e); - } - } - -} +package cn.afterturn.easypoi.excel; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.util.IOUtils; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.IOException; +import java.io.InputStream; + +import cn.afterturn.easypoi.cache.HtmlCache; +import cn.afterturn.easypoi.excel.entity.ExcelToHtmlParams; +import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; +import cn.afterturn.easypoi.excel.html.HtmlToExcelServer; +import cn.afterturn.easypoi.exception.excel.ExcelExportException; +import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; + +/** + * Excel 变成界面 + * @author JueYue + * 2015年5月10日 上午11:51:48 + */ +public class ExcelXorHtmlUtil { + + private ExcelXorHtmlUtil() { + } + + /** + * 转换成为Table + * @param wb Excel + * @return + */ + public static String toTableHtml(Workbook wb) { + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, false, 0, null)); + } + + /** + * 转换成为完整界面,显示图片 + * @param wb Excel + * @return + */ + public static String toAllHtml(Workbook wb) { + return HtmlCache.getHtml(new ExcelToHtmlParams(wb, true, 0, null)); + } + + /** + * 转换成为Table + * @param params + * @return + */ + public static String excelToHtml(ExcelToHtmlParams params) { + return HtmlCache.getHtml(params); + } + + /** + * Html 读取Excel + * @param html + * @param type + * @return + */ + public static Workbook htmlToExcel(String html, ExcelType type) { + Workbook workbook = null; + if (ExcelType.HSSF.equals(type)) { + workbook = new HSSFWorkbook(); + } else { + workbook = new XSSFWorkbook(); + } + new HtmlToExcelServer().createSheet(html, workbook); + return workbook; + } + + /** + * Html 读取Excel + * @param is + * @param type + * @return + */ + public static Workbook htmlToExcel(InputStream is, ExcelType type) { + try { + return htmlToExcel(new String(IOUtils.toByteArray(is)), type); + } catch (IOException e) { + throw new ExcelExportException(ExcelExportEnum.HTML_ERROR, e); + } + } + +} diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ExcelToHtmlParams.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ExcelToHtmlParams.java index fb2282c..2ceff49 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ExcelToHtmlParams.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ExcelToHtmlParams.java @@ -15,15 +15,48 @@ public class ExcelToHtmlParams { /** * 是不是全界面 */ - private boolean completeHTML; + private boolean completeHTML = false; /** * 位置 */ - private int sheetNum; + private int sheetNum = 0; + /** * 图片保存路径,/开始或者含有: 认为是绝对路径,其他是相对路径,每次img名称随机生成,按照天生成文件夹 + * 不为空就认为现实图片 */ - private String path; + private String path = null; + + public ExcelToHtmlParams(Workbook wb) { + this.wb = wb; + } + + public ExcelToHtmlParams(Workbook wb, boolean completeHTML) { + this.wb = wb; + this.completeHTML = completeHTML; + } + + public ExcelToHtmlParams(Workbook wb, int sheetNum) { + this.wb = wb; + this.sheetNum = sheetNum; + } + + public ExcelToHtmlParams(Workbook wb, String path) { + this.wb = wb; + this.path = path; + } + + public ExcelToHtmlParams(Workbook wb, int sheetNum, String path) { + this.wb = wb; + this.sheetNum = sheetNum; + this.path = path; + } + + public ExcelToHtmlParams(Workbook wb, boolean completeHTML, String path) { + this.wb = wb; + this.completeHTML = completeHTML; + this.path = path; + } public ExcelToHtmlParams(Workbook wb, boolean completeHTML, int sheetNum, String path) { this.wb = wb; diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExcelExportBase.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExcelExportBase.java index 5e00315..eccb19a 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExcelExportBase.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExcelExportBase.java @@ -137,7 +137,7 @@ public abstract class ExcelExportBase extends ExportBase { entity = excelParams.get(k); if (entity.getList() != null) { cellNum += entity.getList().size(); - } else if (entity.isNeedMerge()) { + } else if (entity.isNeedMerge() && maxHeight > 1) { for (int i = index + 1; i < index + maxHeight; i++) { sheet.getRow(i).createCell(cellNum); sheet.getRow(i).getCell(cellNum).setCellStyle(getStyles(false, entity)); diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/word/parse/excel/ExcelEntityParse.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/word/parse/excel/ExcelEntityParse.java index d1ef3bc..4c92f51 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/word/parse/excel/ExcelEntityParse.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/word/parse/excel/ExcelEntityParse.java @@ -87,7 +87,7 @@ public class ExcelEntityParse extends ExportBase { entity = excelParams.get(k); if (entity.getList() != null) { cellNum += entity.getList().size(); - } else if (entity.isNeedMerge()) { + } else if (entity.isNeedMerge() && maxHeight > 1) { table.setCellMargins(index, index + maxHeight - 1, cellNum, cellNum); cellNum++; }