import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Html2Doc {
/**
* 将html内容写入word文档
*
* @param htmlStr
* @param path
* @param filename
* @param examPaperName
* @return
* @throws IOException
*/
public static boolean writeWordFileByHtmlStr(String htmlStr, String path, String filename, String examPaperName)
throws IOException {
String head = "<html><div style=\"text-align: center\"><span style=\"font-size: 28px\"><span style=\"font-family: 黑体\">"
+ examPaperName + "<br /> <br /> </span></span></div>";
String tail = "</html>";
htmlStr = head + htmlStr + tail;
ByteArrayInputStream bais = null;
FileOutputStream ostream = null;
try {
if (!"".equals(path)) {
File fileDir = new File(path);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
if (fileDir.exists()) {
String fileName = filename;
byte b[] = htmlStr.getBytes("GBK");
bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
ostream = new FileOutputStream(path + fileName);
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
poifs.close();
documentEntry.delete();
}
}
} catch (IOException e) {
bais.close();
ostream.close();
e.printStackTrace();
throw e;
}
return true;
}
public static boolean writeWordFile(String content, String filename, String path, String examPaperName)
throws IOException {
String head = "<html><div style=\"text-align: center\"><span style=\"font-size: 28px\"><span style=\"font-family: 黑体\">"
+ examPaperName + "<br /> <br /> </span></span></div>";
String tail = "</html>";
content = head + content + tail;
content = exampaper_formater(content);
ByteArrayInputStream bais = null;
FileOutputStream ostream = null;
try {
if (!"".equals(path)) {
File fileDir = new File(path);
if (!fileDir.exists())
fileDir.mkdirs();
if (fileDir.exists()) {
String fileName = filename;
byte b[] = content.getBytes("GBK");
bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
// DocumentEntry documentEntry =
directory.createDocument("WordDocument", bais);
ostream = new FileOutputStream(path + fileName);
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
poifs.close();
}
}
} catch (IOException e) {
bais.close();
ostream.close();
e.printStackTrace();
throw e;
}
return true;
}
private static String exampaper_formater(String content) {
Document doc = Jsoup.parse(content, "UTF-8");
Elements lis = doc.select("li");
Elements textareas = doc.select("textarea");
Elements inputs = doc.select("input");
for (Element e : lis) {
e.after("<br/>");
}
for (Element e : inputs) {
String tmp_text = e.text();
e.after("<p>" + tmp_text + "</p>");
e.remove();
}
for (Element e : textareas) {
e.after("<br /><br /><br /><br /><br />");
e.remove();
}
return doc.toString();
}
public static void main(String[] args) {
String content = "1、两头渐变透明:"
+ "<HR style='FILTER: alpha(opacity=100,finishopacity=0,style=3)' width='80%' color=#987cb9 SIZE=3>"
+ "2、纺锤形:"
+ "<HR style='FILTER: alpha(opacity=100,finishopacity=0,style=2)' width='80%' color=#987cb9 SIZE=10>"
+ "3、右边渐变透明:"
+ "<HR style='FILTER: alpha(opacity=100,finishopacity=0,style=1)' width='80%' color=#987cb9 SIZE=3>"
+ "4、左边渐变透明:"
+ "<HR style='FILTER: alpha(opacity=0,finishopacity=100,style=1)' width='80%' color=#987cb9 SIZE=3>"
+ "5、虚线:<HR style='border:1 dashed #987cb9' width='80%' color=#987cb9 SIZE=1>"
+ "6、双线:<HR style=\"border:3 double #987cb9\" width=\"80%\" color=#987cb9 SIZE=3>"
+ "7、立体效果:<HR style=\"FILTER: progid:DXImageTransform.Microsoft.Shadow(color:#987cb9,direction:145,strength:15)\" width=\"80%\" color=#987cb9 SIZE=1>"
+ "8、钢针效果:<HR style=\"FILTER: progid:DXImageTransform.Microsoft.Glow(color=#987cb9,strength=10)\" width=\"80%\" color=#987cb9 SIZE=1>"
+ "9、长线效果:<HR align=center width=300 color=#987cb9 SIZE=1>";
String filename = "Paper.doc";
String path = "d:\\";
String examPaperName = "模拟试卷";
try {
writeWordFile(content, filename, path, examPaperName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
将html内容写入word文档
最新推荐文章于 2024-06-13 18:13:58 发布