From c21207d279bd81f174bb6f298c3d692d02297ced Mon Sep 17 00:00:00 2001 From: jueyue Date: Sun, 10 May 2015 12:21:12 +0800 Subject: [PATCH] Excel To Html --- easypoi-annotation/pom.xml | 2 +- easypoi-base/pom.xml | 2 +- .../poi/excel/ExcelToHtmlUtil.java | 55 ++++ .../poi/excel/html/ExcelToHtmlServer.java | 180 ++++++++++++ .../excel/html/helper/CellValueHelper.java | 140 +++++++++ .../excel/html/helper/MergedRegionHelper.java | 81 +++++ .../poi/excel/html/helper/StylerHelper.java | 278 ++++++++++++++++++ .../poi/excel/html/helper/excelStyle.css | 45 +++ .../poi/util/PoiPublicUtil.java | 9 + easypoi-test/pom.xml | 2 +- easypoi-web/pom.xml | 2 +- .../html/convert/ExcelConverHtmlUtil.java | 54 ++++ pom.xml | 4 +- 13 files changed, 848 insertions(+), 6 deletions(-) create mode 100644 easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelToHtmlUtil.java create mode 100644 easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/ExcelToHtmlServer.java create mode 100644 easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/CellValueHelper.java create mode 100644 easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/MergedRegionHelper.java create mode 100644 easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/StylerHelper.java create mode 100644 easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/excelStyle.css create mode 100644 easypoi-web/src/main/java/org/jeecgframework/poi/excel/html/convert/ExcelConverHtmlUtil.java diff --git a/easypoi-annotation/pom.xml b/easypoi-annotation/pom.xml index 9f9e747..584bc2f 100644 --- a/easypoi-annotation/pom.xml +++ b/easypoi-annotation/pom.xml @@ -4,7 +4,7 @@ org.jeecgframework easypoi - 2.1.1-SNAPSHOT + 2.1.2-SNAPSHOT easypoi-annotation 基础注解类,解耦合 diff --git a/easypoi-base/pom.xml b/easypoi-base/pom.xml index bc59aa7..61fcd58 100644 --- a/easypoi-base/pom.xml +++ b/easypoi-base/pom.xml @@ -4,7 +4,7 @@ org.jeecgframework easypoi - 2.1.1-SNAPSHOT + 2.1.2-SNAPSHOT easypoi-base 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 new file mode 100644 index 0000000..cf3e9eb --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/ExcelToHtmlUtil.java @@ -0,0 +1,55 @@ +package org.jeecgframework.poi.excel; + +import org.apache.poi.ss.usermodel.Workbook; +import org.jeecgframework.poi.excel.html.ExcelToHtmlServer; + +/** + * Excel 变成界面 + * @author JueYue + * @date 2015年5月10日 上午11:51:48 + */ +public final class ExcelToHtmlUtil { + + private ExcelToHtmlUtil() { + } + + /** + * 转换成为Table + * @param wb Excel + * @return + */ + public static String toTableHtml(Workbook wb) { + return new ExcelToHtmlServer(wb, false, 0).printPage(); + } + + /** + * 转换成为Table + * @param wb Excel + * @param sheetNum sheetNum + * @return + */ + public static String toTableHtml(Workbook wb, int sheetNum) { + return new ExcelToHtmlServer(wb, false, sheetNum).printPage(); + } + + /** + * 转换成为完整界面 + * @param wb Excel + * @param sheetNum sheetNum + * @return + */ + public static String toAllHtml(Workbook wb) { + return new ExcelToHtmlServer(wb, true, 0).printPage(); + } + + /** + * 转换成为完整界面 + * @param wb Excel + * @param sheetNum sheetNum + * @return + */ + public static String toAllHtml(Workbook wb, int sheetNum) { + return new ExcelToHtmlServer(wb, true, sheetNum).printPage(); + } + +} 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 new file mode 100644 index 0000000..76fb4fc --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/ExcelToHtmlServer.java @@ -0,0 +1,180 @@ +package org.jeecgframework.poi.excel.html; + +import java.util.Formatter; +import java.util.Iterator; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.jeecgframework.poi.excel.html.helper.CellValueHelper; +import org.jeecgframework.poi.excel.html.helper.MergedRegionHelper; +import org.jeecgframework.poi.excel.html.helper.StylerHelper; + +/** + * Excel转换成Html 服务 + * @author JueYue + * @date 2015年5月10日 上午11:41:15 + */ +public class ExcelToHtmlServer { + + private Workbook wb; + private int sheetNum; + private int cssRandom; + + /*是不是完成界面*/ + private boolean completeHTML; + private Formatter out; + /*已经完成范围处理*/ + private boolean gotBounds; + private int firstColumn; + private int endColumn; + 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; + cssRandom = (int) Math.ceil(Math.random() * 1000); + } + + public String printPage() { + try { + ensureOut(); + if (completeHTML) { + out.format("%n"); + out.format("%n"); + out.format("%n"); + out.format("%n"); + } + new StylerHelper(wb, out, sheetNum, cssRandom); + if (completeHTML) { + out.format("%n"); + out.format("%n"); + } + print(); + if (completeHTML) { + out.format("%n"); + out.format("%n"); + } + return out.toString(); + } finally { + if (out != null) + out.close(); + } + } + + private void print() { + printSheets(); + } + + private void ensureOut() { + if (out == null) + out = new Formatter(new StringBuilder()); + } + + private void printSheets() { + Sheet sheet = wb.getSheetAt(sheetNum); + printSheet(sheet); + } + + private void printSheet(Sheet sheet) { + out.format("%n", DEFAULTS_CLASS); + printCols(sheet); + printSheetContent(sheet); + out.format("
%n"); + } + + private void printCols(Sheet sheet) { + out.format("%n"); + ensureColumnBounds(sheet); + for (int i = firstColumn; i < endColumn; i++) { + out.format("%n", sheet.getColumnWidth(i) / 32); + } + } + + private void ensureColumnBounds(Sheet sheet) { + if (gotBounds) + return; + + Iterator iter = sheet.rowIterator(); + firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0); + endColumn = 0; + while (iter.hasNext()) { + Row row = iter.next(); + short firstCell = row.getFirstCellNum(); + if (firstCell >= 0) { + firstColumn = Math.min(firstColumn, firstCell); + endColumn = Math.max(endColumn, row.getLastCellNum()); + } + } + gotBounds = true; + } + + private void printColumnHeads(Sheet sheet) { + out.format("%n"); + out.format(" %n", COL_HEAD_CLASS); + out.format(" ◊%n", COL_HEAD_CLASS); + StringBuilder colName = new StringBuilder(); + for (int i = firstColumn; i < endColumn; i++) { + colName.setLength(0); + int cnum = i; + do { + colName.insert(0, (char) ('A' + cnum % 26)); + cnum /= 26; + } while (cnum > 0); + out.format(" %s%n", COL_HEAD_CLASS, colName); + } + out.format(" %n"); + out.format("%n"); + } + + private void printSheetContent(Sheet sheet) { + printColumnHeads(sheet); + MergedRegionHelper mergedRegionHelper = new MergedRegionHelper(sheet); + CellValueHelper cellValueHelper = new CellValueHelper(wb, cssRandom); + out.format("%n"); + Iterator rows = sheet.rowIterator(); + int rowIndex = 1; + while (rows.hasNext()) { + Row row = rows.next(); + out.format(" %n", row.getHeight() / 15); + out.format(" %d%n", ROW_HEAD_CLASS, row.getRowNum() + 1); + for (int i = firstColumn; i < endColumn; i++) { + if (mergedRegionHelper.isNeedCreate(rowIndex, i)) { + String content = " "; + CellStyle style = null; + if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) { + Cell cell = row.getCell(i); + if (cell != null) { + style = cell.getCellStyle(); + content = cellValueHelper.getHtmlValue(cell); + } + } + if (mergedRegionHelper.isMergedRegion(rowIndex, i)) { + Integer[] rowAndColSpan = mergedRegionHelper.getRowAndColSpan(rowIndex, i); + out.format(" %s%n", + rowAndColSpan[0], rowAndColSpan[1], styleName(style), content); + } else { + out.format(" %s%n", styleName(style), content); + } + } + + } + out.format(" %n"); + rowIndex++; + } + out.format("%n"); + } + + private String styleName(CellStyle style) { + if (style == null) + return ""; + return String.format("style_%02x_%s font_%s_%s", style.getIndex(), cssRandom, + style.getFontIndex(), cssRandom); + } +} diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/CellValueHelper.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/CellValueHelper.java new file mode 100644 index 0000000..0bf55bf --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/CellValueHelper.java @@ -0,0 +1,140 @@ +package org.jeecgframework.poi.excel.html.helper; + +import java.util.HashMap; +import java.util.Map; + +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.Font; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import com.google.common.xml.XmlEscapers; + +/** + * Cell值帮助类 + * @author JueYue + * @date 2015年5月9日 下午10:31:32 + */ +public class CellValueHelper { + /** + * Excel 格式 + */ + private boolean is07; + + private int cssRandom; + + private Map fontCache = new HashMap(); + + public CellValueHelper(Workbook wb, int cssRandom) { + this.cssRandom = cssRandom; + if (wb instanceof HSSFWorkbook) + is07 = false; + else if (wb instanceof XSSFWorkbook) { + is07 = true; + cacheFontInfo(wb); + } else + throw new IllegalArgumentException("unknown workbook type: " + + wb.getClass().getSimpleName()); + } + + /** + * O7 版本坑爹bug + * @param wb + */ + private void cacheFontInfo(Workbook wb) { + for (short i = 0, le = wb.getNumberOfFonts(); i < le; i++) { + Font font = wb.getFontAt(i); + fontCache.put(font.getBoldweight() + "_" + font.getItalic() + "_" + font.getFontName() + + "_" + font.getFontHeightInPoints() + "_" + font.getColor(), + font.getIndex() + ""); + } + + } + + public String getHtmlValue(Cell cell) { + if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType() + || Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { + cell.setCellType(Cell.CELL_TYPE_STRING); + return cell.getStringCellValue(); + } else if (Cell.CELL_TYPE_STRING == cell.getCellType()) { + if (cell.getRichStringCellValue().numFormattingRuns() == 0) { + return XmlEscapers.xmlContentEscaper().escape(cell.getStringCellValue()); + } else if (is07) { + return getXSSFRichString((XSSFRichTextString) cell.getRichStringCellValue()); + } else { + return getHSSFRichString((HSSFRichTextString) cell.getRichStringCellValue()); + } + } + return ""; + } + + /** + * 03版本复杂数据 + * @param rich + * @return + */ + private String getHSSFRichString(HSSFRichTextString rich) { + int nums = rich.numFormattingRuns(); + StringBuilder sb = new StringBuilder(); + String text = rich.toString(); + int currentIndex = 0; + sb.append(text.substring(0, rich.getIndexOfFormattingRun(0))); + for (int i = 0; i < nums; i++) { + sb.append(""); + currentIndex = rich.getIndexOfFormattingRun(i); + if (i < nums - 1) { + sb.append(XmlEscapers.xmlContentEscaper().escape( + text.substring(currentIndex, rich.getIndexOfFormattingRun(i + 1)))); + } else { + sb.append(XmlEscapers.xmlContentEscaper().escape( + text.substring(currentIndex, text.length()))); + } + sb.append(""); + } + return sb.toString(); + } + + /** + * 07版本复杂数据 + * @param rich + * @return + */ + private String getXSSFRichString(XSSFRichTextString rich) { + int nums = rich.numFormattingRuns(); + StringBuilder sb = new StringBuilder(); + String text = rich.toString(); + int currentIndex = 0, lastIndex = 0; + for (int i = 1; i <= nums; i++) { + sb.append(""); + currentIndex = rich.getIndexOfFormattingRun(i) == -1 ? text.length() : rich + .getIndexOfFormattingRun(i); + sb.append(XmlEscapers.xmlContentEscaper().escape( + text.substring(lastIndex, currentIndex))); + sb.append(""); + lastIndex = currentIndex; + } + return sb.toString(); + } + + private String getFontIndex(XSSFFont font) { + return fontCache.get(font.getBoldweight() + "_" + font.getItalic() + "_" + + font.getFontName() + "_" + font.getFontHeightInPoints() + "_" + + font.getColor()); + } +} diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/MergedRegionHelper.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/MergedRegionHelper.java new file mode 100644 index 0000000..f2095b3 --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/MergedRegionHelper.java @@ -0,0 +1,81 @@ +package org.jeecgframework.poi.excel.html.helper; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.apache.poi.ss.usermodel.Sheet; + +/** + * 合并单元格帮助类 + * @author JueYue + * @date 2015年5月9日 下午2:13:35 + */ +public class MergedRegionHelper { + + private Map mergedCache = new HashMap(); + + private Set notNeedCread = new HashSet(); + + public MergedRegionHelper(Sheet sheet) { + getAllMergedRegion(sheet); + } + + private void getAllMergedRegion(Sheet sheet) { + int nums = sheet.getNumMergedRegions(); + for (int i = 0; i < nums; i++) { + handerMergedString(sheet.getMergedRegion(i).formatAsString()); + } + } + + /** + * 根据合并输出内容,处理合并单元格事情 + * @param formatAsString + */ + private void handerMergedString(String formatAsString) { + String[] strArr = formatAsString.split(":"); + int startCol = strArr[0].charAt(0) - 65; + int startRol = Integer.valueOf(strArr[0].substring(1)); + int endCol = strArr[1].charAt(0) - 65; + int endRol = Integer.valueOf(strArr[1].substring(1)); + mergedCache.put(startRol + "_" + startCol, new Integer[] { endRol - startRol + 1, + endCol - startCol + 1 }); + for (int i = startRol; i <= endRol; i++) { + for (int j = startCol; j <= endCol; j++) { + notNeedCread.add(i + "_" + j); + } + } + notNeedCread.remove(startRol + "_" + startCol); + } + + /** + * 是不是需要创建这个TD + * @param row + * @param col + * @return + */ + public boolean isNeedCreate(int row, int col) { + return !notNeedCread.contains(row + "_" + col); + } + + /** + * 是不是合并区域 + * @param row + * @param col + * @return + */ + public boolean isMergedRegion(int row, int col) { + return mergedCache.containsKey(row + "_" + col); + } + + /** + * 获取合并区域 + * @param row + * @param col + * @return + */ + public Integer[] getRowAndColSpan(int row, int col) { + return mergedCache.get(row + "_" + col); + } +} diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/StylerHelper.java b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/StylerHelper.java new file mode 100644 index 0000000..41e3d8f --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/StylerHelper.java @@ -0,0 +1,278 @@ +package org.jeecgframework.poi.excel.html.helper; + +import static org.apache.poi.ss.usermodel.CellStyle.ALIGN_CENTER; +import static org.apache.poi.ss.usermodel.CellStyle.ALIGN_CENTER_SELECTION; +import static org.apache.poi.ss.usermodel.CellStyle.ALIGN_FILL; +import static org.apache.poi.ss.usermodel.CellStyle.ALIGN_JUSTIFY; +import static org.apache.poi.ss.usermodel.CellStyle.ALIGN_LEFT; +import static org.apache.poi.ss.usermodel.CellStyle.ALIGN_RIGHT; +import static org.apache.poi.ss.usermodel.CellStyle.VERTICAL_BOTTOM; +import static org.apache.poi.ss.usermodel.CellStyle.VERTICAL_CENTER; +import static org.apache.poi.ss.usermodel.CellStyle.VERTICAL_TOP; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Formatter; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFPalette; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Color; +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.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFColor; +import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.jeecgframework.poi.util.PoiPublicUtil; + +/** + * 样式帮助类 + * @author JueYue + * @date 2015年5月9日 下午4:04:24 + */ +public class StylerHelper { + + private static String DEFAULTS_CLASS_CSS; + + private static final String DEFAULTS_CLASS = "excelDefaults"; + + private static final Map ALIGN = PoiPublicUtil.mapFor(ALIGN_LEFT, + "left", ALIGN_CENTER, "center", + ALIGN_RIGHT, "right", ALIGN_FILL, + "left", ALIGN_JUSTIFY, "left", + ALIGN_CENTER_SELECTION, "center"); + + private static final Map VERTICAL_ALIGN = PoiPublicUtil.mapFor(VERTICAL_BOTTOM, + "bottom", VERTICAL_CENTER, "middle", + VERTICAL_TOP, "top"); + + private Formatter out; + + private Sheet sheet; + + private HtmlHelper helper; + + private int sheetNum; + + private int cssRandom; + + public StylerHelper(Workbook wb, Formatter out, int sheetNum, int cssRandom) { + this.out = out; + this.sheetNum = sheetNum; + this.cssRandom = cssRandom; + if (wb instanceof HSSFWorkbook) + helper = new HSSFHtmlHelper((HSSFWorkbook) wb); + else if (wb instanceof XSSFWorkbook) + helper = new XSSFHtmlHelper((XSSFWorkbook) wb); + else + throw new IllegalArgumentException("unknown workbook type: " + + wb.getClass().getSimpleName()); + printInlineStyle(wb); + } + + private void printInlineStyle(Workbook wb) { + out.format("%n"); + } + + private void prontFonts(Workbook wb) { + for (short i = 0, le = wb.getNumberOfFonts(); i < le; i++) { + Font font = wb.getFontAt(i); + out.format(".%s .%s {%n", DEFAULTS_CLASS, "font_" + i + "_" + cssRandom); + fontStyle(font); + out.format("}%n"); + } + + } + + public void printStyles(Workbook wb) { + if (DEFAULTS_CLASS_CSS == null) { + DEFAULTS_CLASS_CSS = getDefaultsClassCss(); + } + out.format(DEFAULTS_CLASS_CSS); + Set seen = new HashSet(); + sheet = wb.getSheetAt(sheetNum); + Iterator rows = sheet.rowIterator(); + while (rows.hasNext()) { + Row row = rows.next(); + for (Cell cell : row) { + CellStyle style = cell.getCellStyle(); + if (!seen.contains(style)) { + printStyle(style); + seen.add(style); + } + } + } + } + + @SuppressWarnings("resource") + private String getDefaultsClassCss() { + BufferedReader in = null; + StringBuilder sb = new StringBuilder(); + Formatter formatter = new Formatter(sb); + try { + in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream( + "excelStyle.css"))); + String line; + while ((line = in.readLine()) != null) { + formatter.format("%s%n", line); + } + } catch (IOException e) { + throw new IllegalStateException("Reading standard css", e); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + throw new IllegalStateException("Reading standard css", e); + } + } + } + return formatter.toString(); + } + + private void printStyle(CellStyle style) { + out.format(".%s .%s {%n", DEFAULTS_CLASS, styleName(style)); + styleContents(style); + out.format("}%n"); + } + + private void styleContents(CellStyle style) { + if (style.getAlignment() != 2) { + styleOut("text-align", style.getAlignment(), ALIGN); + styleOut("vertical-align", style.getAlignment(), VERTICAL_ALIGN); + } + helper.colorStyles(style, out); + } + + private void fontStyle(Font font) { + if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD) + out.format(" font-weight: bold;%n"); + if (font.getItalic()) + out.format(" font-style: italic;%n"); + out.format(" font-family: %s;%n", font.getFontName()); + + int fontheight = font.getFontHeightInPoints(); + if (fontheight == 9) { + fontheight = 10; + } + out.format(" font-size: %dpt;%n", fontheight); + helper.styleColor(out, "color", getColor(font)); + } + + private Color getColor(Font font) { + if (helper instanceof HSSFHtmlHelper) { + return ((HSSFWorkbook) sheet.getWorkbook()).getCustomPalette() + .getColor(font.getColor()); + } else { + return ((XSSFFont) font).getXSSFColor(); + } + } + + private String styleName(CellStyle style) { + if (style == null) + return ""; + return String.format("style_%02x_%s", style.getIndex(), cssRandom); + } + + private void styleOut(String attr, K key, Map mapping) { + String value = mapping.get(key); + if (value != null) { + out.format(" %s: %s;%n", attr, value); + } + } + + private interface HtmlHelper { + /** + * Outputs the appropriate CSS style for the given cell style. + * + * @param style The cell style. + * @param out The place to write the output. + */ + void colorStyles(CellStyle style, Formatter out); + + void styleColor(Formatter out, String attr, Color color); + } + + private class HSSFHtmlHelper implements HtmlHelper { + private final HSSFWorkbook wb; + private final HSSFPalette colors; + + private HSSFColor HSSF_AUTO = new HSSFColor.AUTOMATIC(); + + public HSSFHtmlHelper(HSSFWorkbook wb) { + this.wb = wb; + colors = wb.getCustomPalette(); + } + + public void colorStyles(CellStyle style, Formatter out) { + HSSFCellStyle cs = (HSSFCellStyle) style; + out.format(" /* fill pattern = %d */%n", cs.getFillPattern()); + styleColor(out, "background-color", cs.getFillForegroundColor()); + styleColor(out, "color", colors.getColor(cs.getFont(wb).getColor())); + } + + private void styleColor(Formatter out, String attr, short index) { + HSSFColor color = colors.getColor(index); + if (index == HSSF_AUTO.getIndex() || color == null) { + out.format(" /* %s: index = %d */%n", attr, index); + } else { + short[] rgb = color.getTriplet(); + out.format(" %s: #%02x%02x%02x; /* index = %d */%n", attr, rgb[0], rgb[1], rgb[2], + index); + } + } + + public void styleColor(Formatter out, String attr, Color color) { + if (color == null) { + return; + } + HSSFColor hSSFColor = (HSSFColor) color; + short[] rgb = hSSFColor.getTriplet(); + out.format(" %s: #%02x%02x%02x; %n", attr, rgb[0], rgb[1], rgb[2]); + } + } + + /** + * Implementation of {@link HtmlHelper} for XSSF files. + * + * @author Ken Arnold, Industrious Media LLC + */ + private class XSSFHtmlHelper implements HtmlHelper { + + public XSSFHtmlHelper(XSSFWorkbook wb) { + } + + public void colorStyles(CellStyle style, Formatter out) { + XSSFCellStyle cs = (XSSFCellStyle) style; + styleColor(out, "background-color", cs.getFillForegroundXSSFColor()); + styleColor(out, "color", cs.getFont().getXSSFColor()); + } + + public void styleColor(Formatter out, String attr, Color color) { + XSSFColor xSSFColor = (XSSFColor) color; + if (color == null || xSSFColor.isAuto()) + return; + + byte[] rgb = xSSFColor.getRgb(); + if (rgb == null) { + return; + } + out.format(" %s: #%02x%02x%02x;%n", attr, rgb[0], rgb[1], rgb[2]); + } + } + +} diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/excelStyle.css b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/excelStyle.css new file mode 100644 index 0000000..42d845f --- /dev/null +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/excel/html/helper/excelStyle.css @@ -0,0 +1,45 @@ +.excelDefaults { + background-color: white; + color: black; + text-decoration: none; + direction: ltr; + text-transform: none; + text-indent: 0; + letter-spacing: 0; + word-spacing: 0; + white-space: normal; + unicode-bidi: normal; + vertical-align: 0; + text-shadow: none; + padding: 0; + margin: 0; + border-collapse: collapse; + white-space: pre-wrap; + word-wrap: break-word; + word-break: break-all; +} + +.excelDefaults td { + padding: 1px 5px; + border: 1px solid silver; + border-color: #000000; + text-align: center; + vertical-align: middle; + font-size: 12pt; +} + +.excelDefaults .colHeader { + background-color: silver; + font-weight: bold; + border: 1px solid black; + text-align: center; + padding: 1px 5px; +} + +.excelDefaults .rowHeader { + background-color: silver; + font-weight: bold; + border: 1px solid black; + text-align: right; + padding: 1px 5px; +} \ No newline at end of file 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 779c4c0..e750d67 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 @@ -68,6 +68,15 @@ public final class PoiPublicUtil { } + @SuppressWarnings({ "unchecked" }) + public static Map mapFor(Object... mapping) { + Map map = new HashMap(); + for (int i = 0; i < mapping.length; i += 2) { + map.put((K) mapping[i], (V) mapping[i + 1]); + } + return map; + } + /** * 彻底创建一个对象 * diff --git a/easypoi-test/pom.xml b/easypoi-test/pom.xml index d8fd02b..b156a90 100644 --- a/easypoi-test/pom.xml +++ b/easypoi-test/pom.xml @@ -4,7 +4,7 @@ org.jeecgframework easypoi - 2.1.1-SNAPSHOT + 2.1.2-SNAPSHOT easypoi-test diff --git a/easypoi-web/pom.xml b/easypoi-web/pom.xml index 883a8d1..c7e5f4b 100644 --- a/easypoi-web/pom.xml +++ b/easypoi-web/pom.xml @@ -4,7 +4,7 @@ org.jeecgframework easypoi - 2.1.1-SNAPSHOT + 2.1.2-SNAPSHOT easypoi-web diff --git a/easypoi-web/src/main/java/org/jeecgframework/poi/excel/html/convert/ExcelConverHtmlUtil.java b/easypoi-web/src/main/java/org/jeecgframework/poi/excel/html/convert/ExcelConverHtmlUtil.java new file mode 100644 index 0000000..ec6822a --- /dev/null +++ b/easypoi-web/src/main/java/org/jeecgframework/poi/excel/html/convert/ExcelConverHtmlUtil.java @@ -0,0 +1,54 @@ +package org.jeecgframework.poi.excel.html.convert; + +import java.util.Iterator; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +/** + * Excel 转换成为Html + * @author JueYue + * @date 2015年5月7日 下午10:06:51 + */ +public final class ExcelConverHtmlUtil { + + private static final String STARTHTML = ""; + private static final String HTML_TR_S = ""; + private static final String HTML_TR_E = ""; + private static final String HTML_TD_S = ""; + private static final String HTML_TD_E = ""; + + private ExcelConverHtmlUtil() { + + } + + public String convert(Workbook workbook, int... sheets) { + StringBuilder html = new StringBuilder(STARTHTML); + StringBuilder css = new StringBuilder(); + StringBuilder body = new StringBuilder(); + sheetConverToHtml(workbook.getSheetAt(sheets[0]), css, body); + html.append(css); + html.append(body); + return html.toString(); + } + + private void sheetConverToHtml(Sheet sheet, StringBuilder css, StringBuilder body) { + Iterator rows = sheet.rowIterator(); + Iterator cells = null; + while (rows.hasNext()) { + Row row = rows.next(); + cells = row.cellIterator(); + body.append(HTML_TR_S); + while (cells.hasNext()) { + Cell cell = cells.next(); + body.append(HTML_TD_S); + body.append(cell.toString()); + body.append(HTML_TD_E); + } + body.append(HTML_TR_E); + } + } + +} diff --git a/pom.xml b/pom.xml index cfe838e..7685544 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.jeecgframework easypoi - 2.1.1-SNAPSHOT + 2.1.2-SNAPSHOT pom easypoi @@ -24,7 +24,7 @@ - 2.1.1-SNAPSHOT + 2.1.2-SNAPSHOT 3.9 2.9.1 16.0.1