|
|
@@ -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<String, PictureData> pictures = new HashMap<String, PictureData>();
|
|
|
|
|
|
|
|
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("<!DOCTYPE HTML>%n");
|
|
|
|
out.format("<html>%n");
|
|
|
|
out.format("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">%n");
|
|
|
|
out.format(
|
|
|
|
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">%n");
|
|
|
|
out.format("<head>%n");
|
|
|
|
}
|
|
|
|
if (StringUtils.isNotEmpty(imageCachePath)) {
|
|
|
|
getPictures();
|
|
|
|
}
|
|
|
|
new StylerHelper(wb, out, sheetNum, cssRandom);
|
|
|
|
if (completeHTML) {
|
|
|
|
out.format("</head>%n");
|
|
|
@@ -62,10 +95,26 @@ public class ExcelToHtmlServer { |
|
|
|
out.format("</html>%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 = "<img src='" + getImageSrc(pictures.get((rowIndex - 1) + "_" + i))
|
|
|
|
+ "' style='max-width: "
|
|
|
|
+ getImageMaxWidth(
|
|
|
|
mergedRegionHelper.getRowAndColSpan(rowIndex, i), i, sheet)
|
|
|
|
+ "px;' />";
|
|
|
|
}
|
|
|
|
if (mergedRegionHelper.isMergedRegion(rowIndex, i)) {
|
|
|
|
Integer[] rowAndColSpan = mergedRegionHelper.getRowAndColSpan(rowIndex, i);
|
|
|
|
out.format(" <td rowspan='%s' colspan='%s' class='%s' >%s</td>%n",
|
|
|
@@ -182,6 +238,60 @@ public class ExcelToHtmlServer { |
|
|
|
out.format("</tbody>%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 "";
|
|
|
|