根据excel word文件,生成网页录入,自动求和。
使用工具:jsoup、poi、summernote(前台富文本编辑器)
先写一个关闭输入输出流的方法
/**
* 关闭输入流
* @param is
*/
private void closeStream(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭输出流
* @param os
*/
private void closeStream(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
根据word生成html文件及字符串
/**
*
* @param path 所在的文件夹
* @param inFileName 输入Word文档
* @return String
* @throws Throwable
*/
public String wordToHtml(String path) throws Throwable {
return hWPFToHtml(wordToHWPF(path));
}
/**
*
* @param path 所在的文件夹
* @param inFileName 输入Word文档
* @return HWPFDocument
* @throws Throwable
*/
public HWPFDocument wordToHWPF(String path) throws Throwable {
InputStream input = new FileInputStream(path);
HWPFDocument wordDocument = new HWPFDocument(input);
this.closeStream(input);
return wordDocument;
}
/**
*
* @param wordDocument 输入Word文档
* @return String
* @throws Throwable
*/
public String hWPFToHtml(HWPFDocument wordDocument) throws Throwable{
String path = ""; //想要生成html文件的物理地址
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
public String savePicture(byte[] content, PictureType pictureType,
String suggestedName, float widthInches, float heightInches) {
return suggestedName;
}
});
wordToHtmlConverter.processDocument(wordDocument);
List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
try {
pic.writeImageContent(new FileOutputStream(path
+ pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
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);
String content = new String(outStream.toByteArray());
/*FileUtils.write(new File(path, "test.html"), content, "utf-8");*/ //需要生成html文件解除注释
this.closeStream(outStream);
return content;
}