excel表格数据转成HTML格式得数据

excel.xls 表格数据转换成html格式得数据(不包含图片)

首先先导入相应得pom包
<!-- excel 转 html -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.8</version>
</dependency>
// path = "/Applications/cyq_file/e6b3ed259841490c94f531624af1da0e.xls";
// path 要转换得数据文件
public static String convertExceltoHtml(String path) throws IOException, ParserConfigurationException, TransformerException, InvalidFormatException {
HSSFWorkbook workBook = null;
String content = null;
StringWriter writer = null;
File excelFile = new File(path);
InputStream is = new FileInputStream(excelFile);;
//判断Excel文件是2003版还是2007版
//String suffix = path.substring(path.lastIndexOf("."));
workBook = new HSSFWorkbook(is);
try {
ExcelToHtmlConverter converter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
converter.setOutputColumnHeaders(false);// 不显示列的表头
converter.setOutputRowNumbers(false);// 不显示行的表头
converter.processWorkbook(workBook);

writer = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(new DOMSource(converter.getDocument()), new StreamResult(writer));
content = writer.toString();
writer.close();
} finally {
try {
if (is != null) {
is.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return content;
}

// 返回一个字符串就是我们转换后得HTML数据,我们只需要返回到前端即可,其余的根据自己得业务来书写即可

posted on 2021-05-20 16:37  UnmatchedSelf  阅读(585)  评论(0编辑  收藏  举报

导航