private final static String tempPath = "C:\\Users\\xxx\\Desktop\\Word2Html\\src\\test\\";//图片及相关文件保存的路径
public static void main(String argv[]) {
try {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Select a Word Document");
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.addChoosableFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("Word Documents", "doc", "docx"));
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File inputFile = fileChooser.getSelectedFile();
String fileName = inputFile.getAbsolutePath();
String defaultOutputDir = System.getProperty("user.home") + "\\Desktop\\";
String outputFileName = defaultOutputDir + inputFile.getName().replaceFirst("[.][^.]+$", "") + ".html";
if (fileName.endsWith(".doc")) {
doc2Html(fileName, outputFileName);
} else if (fileName.endsWith(".docx")) {
docx2Html(fileName, outputFileName);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* doc转换为html
*
* @param fileName
* @param outPutFile
* @throws TransformerException
* @throws IOException
* @throws ParserConfigurationException
*/
public static void doc2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException {
long startTime = System.currentTimeMillis();
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument())