您的位置首页百科问答

怎样才能把XML格式转换成Word文档?

怎样才能把XML格式转换成Word文档?

通过jodconveter来实现转化(http://www.artofsolving.com/opensource/jodconverter)。这种方式实现起来比较麻烦,操作有点繁琐,但是能解决上述问题。通过茄举启动OpenOffice.org的服务端口,实现程序操作opeanoffice实现文件的转换。 贴上颤御碧代码:mport java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.DocumentFormatRegistry; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; public class test { /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub test t = new test(); //File in = new File("d:\\mytest\\test1.pdf"); //File out = new File("d:\\mytest\\test11.html"); FileInputStream input = new FileInputStream("拆键d:\\mytest\\test11.pdf"); FileOutputStream output = new FileOutputStream("d:\\mytest\\test11.doc"); t.convert(input, output); } public void convert(File input, File output) throws Exception { OpenOfficeConnection conn = new SocketOpenOfficeConnection("localhost", 8100); conn.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(conn); converter.convert(input, output); conn.disconnect(); } public void convert(InputStream input, OutputStream output) throws Exception { OpenOfficeConnection conn = new SocketOpenOfficeConnection("localhost", 8100); conn.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(conn); DocumentFormatRegistry registry = new DefaultDocumentFormatRegistry(); converter.convert(input, registry.getFormatByFileExtension("pdf"), output, registry.getFormatByFileExtension("doc")); conn.disconnect(); } }