| @@ -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)); | |||
| } | |||
| } | |||
| @@ -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); | |||
| } | |||
| } | |||
| } | |||
| @@ -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; | |||
| @@ -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)); | |||
| @@ -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++; | |||
| } | |||