pom依赖
<!-- poi word、excel转html-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>2.0.2</version>
</dependency>
<!-- pdfbox pdf转html-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.19</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.19</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.19</version>
</dependency>
<dependency>
<groupId>net.sf.cssbox</groupId>
<artifactId>pdf2dom</artifactId>
<version>1.9</version>
</dependency>
1.word转html
import java.io.StringWriter;
import java.util.Base64;
import java.util.Base64.Encoder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import fr.opensagres.poi.xwpf.converter.xhtml.Base64EmbedImgManager;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
/**
* @author taoli
* @date 2020年3月8日 下午4:54:45
* word转html工具类
*/
public class WordToHtml {
/**
* word转html字符串
* @param file word文件(支持的后缀:doc、docx)
* @return html字符串
* @throws Exception
*/
public static String transform(MultipartFile file) throws Exception {
StringBuilder wordHtml = new StringBuilder();
String fileName = file.getOriginalFilename();
if (fileName.indexOf(".") != -1) {
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (suffix.equalsIgnoreCase(".doc")) {
wordHtml.append(docToHtml(file));
}else if (suffix.equalsIgnoreCase(".docx")) {
wordHtml.append(docxToHtml(file));
}
}
return wordHtml.toString();
}
/**
* word97-03转html
* @param file
* @return
* @throws Exception
*/
private static String docToHtml(MultipartFile file) throws Exception{
HWPFDocument wordDocument = new HWPFDocument(file.getInputStream());
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document);
//图片使用base64编码
wordToHtmlConverter.setPicturesManager((content, pictureType, name, width, height) -> {
Encoder encoder = Base64.getEncoder();
return "data:image/"+pictureType.getExtension()+";base64,"+encoder.encodeToString(content);
});
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(new StringWriter());
//StreamResult streamResult = new StreamResult(new File(target));
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
}finally {
streamResult.getWriter().flush();
try {
streamResult.getWriter().close();
} catch (IOException ioe) {
// ignore
}
}
return streamResult.getWriter().toString();
}
/**
* word07转html
* @param file
* @return
* @throws Exception
*/
private static String docxToHtml(MultipartFile file) throws Exception{
StringWriter writer = new StringWriter();
try {
XWPFDocument document = new XWPFDocument(file.getInputStream());
XHTMLOptions options = XHTMLOptions.create();
//图片使用base64编码
options.setImageManager(new Base64EmbedImgManager());
XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance();
xhtmlConverter.convert(document, writer, options);
} finally {
writer.flush();
try {
writer.close();
} catch (IOException ioe) {
// ignore
}
}
return writer.toString();
}
}
2.excel转html
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
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.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import org.springframework.web.multipart.MultipartFile;
/**
* @author taoli
* @date 2020年3月8日 下午4:34:30
* excel转html工具类
*/
public class ExcelToHtml {
/**
* excel文件转html
* @param file excel文件(支持的后缀:xls、xlsx)
* @return html 字符串
* @throws IOException
*/
public static String transform(MultipartFile file) throws IOException {
InputStream is = null;
StringBuilder htmlExcel = new StringBuilder();
try {
//添加开头
htmlExcel.append("<html><head><meta http-equiv=\"Content-Type\" charset=\"utf-8\"><title>"+file.getOriginalFilename()+"</title></head><body>");
htmlExcel.append("<div>");
//获取excel内容
is = file.getInputStream();
Workbook wb = WorkbookFactory.create(is);// 此WorkbookFactory在POI-3.10版本中使用需要添加dom4j
if (wb instanceof XSSFWorkbook) {
XSSFWorkbook xWb = (XSSFWorkbook) wb;
htmlExcel.append(getExcelInfo(xWb, true));
} else if (wb instanceof HSSFWorkbook) {
HSSFWorkbook hWb = (HSSFWorkbook) wb;
htmlExcel.append(getExcelInfo(hWb, true));
}
//添加结尾
htmlExcel.append("</div>");
htmlExcel.append("</body></html>");
}finally {
try {
if(is != null){
is.close();
}
} catch (IOException ioe) {
// ignore
}
}
return htmlExcel.toString();
}
private static String getExcelInfo(Workbook wb, boolean isWithStyle) {
StringBuffer sb = new StringBuffer();
for(int i=0;i<wb.getNumberOfSheets();i++) {
Sheet sheet = wb.getSheetAt(i);// 获取第一个Sheet的内容
String sheetName = sheet.getSheetName();
int lastRowNum = sheet.getLastRowNum();
Map<String, String> map[] = getRowSpanColSpanMap(sheet);
sb.append("<h3>"+sheetName+"</h3>");
sb.append("<table style='border-collapse:collapse;' width='100%'>");
// map等待存储excel图片
Map<String, PictureData> sheetIndexPicMap = getSheetPictrues(i, sheet, wb);
Map<String, String> imgMap = new HashMap<String, String>();
if (sheetIndexPicMap != null) {
imgMap = printImg(sheetIndexPicMap);
printImpToWb(imgMap, wb);
}
Row row = null; // 兼容
Cell cell = null; // 兼容
for (int rowNum = sheet.getFirstRowNum(); rowNum <= lastRowNum; rowNum++) {
row = sheet.getRow(rowNum);
if (row == null) {
sb.append("<tr><td > </td></tr>");
continue;
}
sb.append("<tr>");
int lastColNum = row.getLastCellNum();
for (int colNum = 0; colNum < lastColNum; colNum++) {
cell = row.getCell(colNum);
if (cell == null) { // 特殊情况 空白的单元格会返回null
sb.append("<td> </td>");
continue;
}
String imageHtml = "";
String imageRowNum = i + "_" + rowNum + "_" + colNum;
if (sheetIndexPicMap != null && sheetIndexPicMap.containsKey(imageRowNum)) {
String imagePath = imgMap.get(imageRowNum);
imageHtml = "<img src='" + imagePath + "' style='height:auto;'>";
}
String stringValue = getCellValue(cell);
if (map[0].containsKey(rowNum + "," + colNum)) {
String pointString = map[0].get(rowNum + "," + colNum);
map[0].remove(rowNum + "," + colNum);
int bottomeRow = Integer.valueOf(pointString.split(",")[0]);
int bottomeCol = Integer.valueOf(pointString.split(",")[1]);
int rowSpan = bottomeRow - rowNum + 1;
int colSpan = bottomeCol - colNum + 1;
sb.append("<td rowspan= '" + rowSpan + "' colspan= '" + colSpan + "' ");
} else if (map[1].containsKey(rowNum + "," + colNum)) {
map[1].remove(rowNum + "," + colNum);
continue;
} else {
sb.append("<td ");
}
// 判断是否需要样式
if (isWithStyle) {
dealExcelStyle(wb, sheet, cell, sb);// 处理单元格样式
}
sb.append(">");
if (sheetIndexPicMap != null && sheetIndexPicMap.containsKey(imageRowNum)) {
sb.append(imageHtml);
}
if (stringValue == null || "".equals(stringValue.trim())) {
sb.append(" ");
} else {
// 将ascii码为160的空格转换为html下的空格( )
sb.append(stringValue.replace(String.valueOf((char) 160), " "));
}
sb.append("</td>");
}
sb.append("</tr>");
}
sb.append("</table>");
}
return sb.toString();
}
/**
* 获取Excel图片公共方法
*
* @param sheetNum 当前sheet编号
* @param sheet 当前sheet对象
* @param workbook 工作簿对象
* @return Map key:图片单元格索引(0_1_1)String,value:图片流PictureData
*/
private static Map<String, PictureData> getSheetPictrues(int sheetNum, Sheet sheet, Workbook workbook) {
if (workbook instanceof HSSFWorkbook) {
return getSheetPictrues03(sheetNum, (HSSFSheet) sheet, (HSSFWorkbook) workbook);
} else if (workbook instanceof XSSFWorkbook) {
return getSheetPictrues07(sheetNum, (XSSFSheet) sheet, (XSSFWorkbook) workbook);
} else {
return null;
}
}
/**
* 获取Excel2003图片
*
* @param sheetNum 当前sheet编号
* @param sheet 当前sheet对象
* @param workbook 工作簿对象
* @return Map key:图片单元格索引(0_1_1)String,value:图片流PictureData
* @throws IOException
*/
private static Map<String, PictureData> getSheetPictrues03(int sheetNum,
HSSFSheet sheet, HSSFWorkbook workbook) {
Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
List<HSSFPictureData> pictures = workbook.getAllPictures();
if (pictures.size() != 0 && sheet.getDrawingPatriarch() != null) {
for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
shape.getLineWidth();
if (shape instanceof HSSFPicture) {
HSSFPicture pic = (HSSFPicture) shape;
int pictureIndex = pic.getPictureIndex() - 1;
HSSFPictureData picData = pictures.get(pictureIndex);
String picIndex = String.valueOf(sheetNum) + "_"
+ String.valueOf(anchor.getRow1()) + "_"
+ String.valueOf(anchor.getCol1());
sheetIndexPicMap.put(picIndex, picData);
}
}
return sheetIndexPicMap;
} else {
return null;
}
}
/**
* 获取Excel2007图片
*
* @param sheetNum 当前sheet编号
* @param sheet 当前sheet对象
* @param workbook 工作簿对象
* @return Map key:图片单元格索引(0_1_1)String,value:图片流PictureData
*/
private static Map<String, PictureData> getSheetPictrues07(int sheetNum,
XSSFSheet sheet, XSSFWorkbook workbook) {
Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
for (POIXMLDocumentPart dr : sheet.getRelations()) {
if (dr instanceof XSSFDrawing) {
XSSFDrawing drawing = (XSSFDrawing) dr;
List<XSSFShape> shapes = drawing.getShapes();
for (XSSFShape shape : shapes) {
XSSFPicture pic = (XSSFPicture) shape;
XSSFClientAnchor anchor = pic.getPreferredSize();
CTMarker ctMarker = anchor.getFrom();
String picIndex = String.valueOf(sheetNum) + "_"
+ ctMarker.getRow() + "_"
+ ctMarker.getCol();
sheetIndexPicMap.put(picIndex, pic.getPictureData());
}
}
}
return sheetIndexPicMap;
}
/**
* 对图片单元格赋值使其可读取到
* <p>add by CJ 2018年5月21日</p>
*
* @param imgMap
* @param wb
*/
@SuppressWarnings("unused")
private static void printImpToWb(Map<String, String> imgMap, Workbook wb) {
Sheet sheet = null;
Row row = null;
String[] sheetRowCol = new String[3];
for (String key : imgMap.keySet()) {
sheetRowCol = key.split("_");
sheet = wb.getSheetAt(Integer.parseInt(sheetRowCol[0]));
row = sheet.getRow(Integer.parseInt(sheetRowCol[1])) == null ? sheet.createRow(Integer.parseInt(sheetRowCol[1])) :
sheet.getRow(Integer.parseInt(sheetRowCol[1]));
Cell cell = row.getCell(Integer.parseInt(sheetRowCol[2])) == null ? row.createCell(Integer.parseInt(sheetRowCol[2])) :
row.getCell(Integer.parseInt(sheetRowCol[2]));
}
}
private static Map<String, String> printImg(Map<String, PictureData> map) {
Map<String, String> imgMap = new HashMap<String, String>();
try {
Object key[] = map.keySet().toArray();
for (int i = 0; i < map.size(); i++) {
// 获取图片流
PictureData pic = map.get(key[i]);
// 获取图片索引
String picName = key[i].toString();
Encoder encoder = Base64.getEncoder();
imgMap.put(picName, "data:image/"+pic.suggestFileExtension()+";base64,"+encoder.encodeToString(pic.getData()));
}
} catch (Exception e) {
}
return imgMap;
}
@SuppressWarnings("unchecked")
private static Map<String, String>[] getRowSpanColSpanMap(Sheet sheet) {
Map<String, String> map0 = new HashMap<String, String>();
Map<String, String> map1 = new HashMap<String, String>();
int mergedNum = sheet.getNumMergedRegions();
CellRangeAddress range = null;
for (int i = 0; i < mergedNum; i++) {
range = sheet.getMergedRegion(i);
int topRow = range.getFirstRow();
int topCol = range.getFirstColumn();
int bottomRow = range.getLastRow();
int bottomCol = range.getLastColumn();
map0.put(topRow + "," + topCol, bottomRow + "," + bottomCol);
// System.out.println(topRow + "," + topCol + "," + bottomRow + ","
// + bottomCol);
int tempRow = topRow;
while (tempRow <= bottomRow) {
int tempCol = topCol;
while (tempCol <= bottomCol) {
map1.put(tempRow + "," + tempCol, "");
tempCol++;
}
tempRow++;
}
map1.remove(topRow + "," + topCol);
}
@SuppressWarnings("rawtypes")
Map[] map = { map0, map1 };
return map;
}
/**
* 获取表格单元格Cell内容
*
* @param cell
* @return
*/
private static String getCellValue(Cell cell) {
String result = new String();
switch (cell.getCellType()) {
case NUMERIC:// 数字类型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
CellStyle style = cell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
// 单元格设置成常规
if (temp.equals("General")) {
format.applyPattern("#");
}
result = format.format(value);
}
break;
case STRING:// String类型
result = cell.getRichStringCellValue().toString();
break;
case BLANK:
result = "";
break;
default:
result = "";
break;
}
return result;
}
/**
* 处理表格样式
*
* @param wb
* @param sheet
* @param cell
* @param sb
*/
private static void dealExcelStyle(Workbook wb, Sheet sheet, Cell cell, StringBuffer sb) {
CellStyle cellStyle = cell.getCellStyle();
if (cellStyle != null) {
HorizontalAlignment alignment = cellStyle.getAlignment();
sb.append("align='" + convertAlignToHtml(alignment) + "' ");// 单元格内容的水平对齐方式
VerticalAlignment verticalAlignment = cellStyle.getVerticalAlignment();
sb.append("valign='" + convertVerticalAlignToHtml(verticalAlignment) + "' ");// 单元格中内容的垂直排列方式
if (wb instanceof XSSFWorkbook) {
XSSFFont xf = ((XSSFCellStyle) cellStyle).getFont();
boolean bold = xf.getBold();
sb.append("style='");
if (bold) {//字体加粗
sb.append("font-weight:bold;");
}
sb.append("font-size: " + xf.getFontHeight() / 2 + "%;"); // 字体大小
int columnWidth = sheet.getColumnWidth(cell.getColumnIndex());
sb.append("width:" + columnWidth + "px;");
XSSFColor xc = xf.getXSSFColor();
if (xc != null && !"".equals(xc)) {
String string = xc.getARGBHex();
if(string!=null&& !"".equals(string)) {
sb.append("color:#" + string.substring(2) + ";"); // 字体颜色
}
}
XSSFColor bgColor = (XSSFColor) cellStyle.getFillForegroundColorColor();
if (bgColor != null && !"".equals(bgColor) && bgColor!=null) {
String argbHex = bgColor.getARGBHex();
if(argbHex!=null && !"".equals(argbHex)) {
sb.append("background-color:#" + argbHex.substring(2) + ";"); // 背景颜色
}
}
sb.append(getBorderStyle(0, cellStyle.getBorderTop().getCode(),
((XSSFCellStyle) cellStyle).getTopBorderXSSFColor()));
sb.append(getBorderStyle(1, cellStyle.getBorderRight().getCode(),
((XSSFCellStyle) cellStyle).getRightBorderXSSFColor()));
sb.append(getBorderStyle(2, cellStyle.getBorderBottom().getCode(),
((XSSFCellStyle) cellStyle).getBottomBorderXSSFColor()));
sb.append(getBorderStyle(3, cellStyle.getBorderLeft().getCode(),
((XSSFCellStyle) cellStyle).getLeftBorderXSSFColor()));
} else if (wb instanceof HSSFWorkbook) {
HSSFFont hf = ((HSSFCellStyle) cellStyle).getFont(wb);
boolean bold = hf.getBold();
short fontColor = hf.getColor();
sb.append("style='");
HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette(); // 类HSSFPalette用于求的颜色的国际标准形式
HSSFColor hc = palette.getColor(fontColor);
if (bold) {//字体加粗
sb.append("font-weight:bold;");
}
sb.append("font-size: " + hf.getFontHeight() / 2 + "%;"); // 字体大小
String fontColorStr = convertToStardColor(hc);
if (fontColorStr != null && !"".equals(fontColorStr.trim())) {
sb.append("color:" + fontColorStr + ";"); // 字体颜色
}
int columnWidth = sheet.getColumnWidth(cell.getColumnIndex());
sb.append("width:" + columnWidth + "px;");
short bgColor = cellStyle.getFillForegroundColor();
hc = palette.getColor(bgColor);
String bgColorStr = convertToStardColor(hc);
if (bgColorStr != null && !"".equals(bgColorStr.trim())) {
sb.append("background-color:" + bgColorStr + ";"); // 背景颜色
}
sb.append(getBorderStyle(palette, 0, cellStyle.getBorderTop().getCode(), cellStyle.getTopBorderColor()));
sb.append(getBorderStyle(palette, 1, cellStyle.getBorderRight().getCode(), cellStyle.getRightBorderColor()));
sb.append(getBorderStyle(palette, 3, cellStyle.getBorderLeft().getCode(), cellStyle.getLeftBorderColor()));
sb.append(getBorderStyle(palette, 2, cellStyle.getBorderBottom().getCode(), cellStyle.getBottomBorderColor()));
}
sb.append("' ");
}
}
/**
* 单元格内容的水平对齐方式
*
* @param alignment
* @return
*/
private static String convertAlignToHtml(HorizontalAlignment alignment) {
String align = "left";
switch (alignment) {
case LEFT:
align = "left";
break;
case CENTER:
align = "center";
break;
case RIGHT:
align = "right";
break;
default:
break;
}
return align;
}
/**
* 单元格中内容的垂直排列方式
*
* @param verticalAlignment
* @return
*/
private static String convertVerticalAlignToHtml(VerticalAlignment verticalAlignment) {
String valign = "middle";
switch (verticalAlignment) {
case BOTTOM:
valign = "bottom";
break;
case CENTER:
valign = "center";
break;
case TOP:
valign = "top";
break;
default:
break;
}
return valign;
}
private static String convertToStardColor(HSSFColor hc) {
StringBuffer sb = new StringBuffer("");
if (hc != null) {
if (HSSFColor.HSSFColorPredefined.AUTOMATIC.getIndex() == hc.getIndex()) {
return null;
}
sb.append("#");
for (int i = 0; i < hc.getTriplet().length; i++) {
sb.append(fillWithZero(Integer.toHexString(hc.getTriplet()[i])));
}
}
return sb.toString();
}
private static String fillWithZero(String str) {
if (str != null && str.length() < 2) {
return "0" + str;
}
return str;
}
static String[] bordesr = { "border-top:", "border-right:", "border-bottom:", "border-left:" };
static String[] borderStyles = { "solid ", "solid ", "solid ", "solid ", "solid ", "solid ", "solid ", "solid ",
"solid ", "solid", "solid", "solid", "solid", "solid" };
private static String getBorderStyle(HSSFPalette palette, int b, short s, short t) {
if (s == 0)
return bordesr[b] + borderStyles[s] + "#d0d7e5 1px;";
String borderColorStr = convertToStardColor(palette.getColor(t));
borderColorStr = borderColorStr == null || borderColorStr.length() < 1 ? "#000000" : borderColorStr;
return bordesr[b] + borderStyles[s] + borderColorStr + " 1px;";
}
private static String getBorderStyle(int b, short s, XSSFColor xc) {
if (s == 0)
return bordesr[b] + borderStyles[s] + "#d0d7e5 1px;";
if (xc != null && !"".equals(xc)) {
String borderColorStr = xc.getARGBHex();// t.getARGBHex();
borderColorStr = borderColorStr == null || borderColorStr.length() < 1 ? "#000000"
: borderColorStr.substring(2);
return bordesr[b] + borderStyles[s] + borderColorStr + " 1px;";
}
return "";
}
}
3.pdf转html
import java.io.StringWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.fit.pdfdom.PDFDomTree;
import org.springframework.web.multipart.MultipartFile;
/**
* @author taoli
* @date 2020年3月8日 下午5:21:42
* pdf转html工具类
*/
public class PdfToHtml {
public static String transform(MultipartFile file) throws Exception {
PDDocument pdf = PDDocument.load(file.getInputStream());
StringWriter writer = new StringWriter();
try {
new PDFDomTree().writeText(pdf, writer);
} finally {
writer.flush();
try {
writer.close();
} catch (IOException ioe) {
// ignore
}
}
return writer.toString();
}
}
参考
https://blog.csdn.net/shiyu18045181748/article/details/99841319
https://www.jianshu.com/p/def263d48508
https://www.cnblogs.com/x54256/p/8820471.html