发布网友 发布时间:2022-04-21 05:23
共4个回答
懂视网 时间:2022-04-21 09:44
1、介绍3、转换word为pdf、html、txt 的示例
package com.shanhy.demo.windowsoffice;import java.io.File;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;/** * * 将jacob.dll放入JDK的bin目录下 * 把jacob.jar放入项目的buildPath中(web项目放到WEB-INFlib目录下) * * @author 单红宇 * */public class ConvertToPdf { // WORD 转换文档格式参数值:17为pdf,8为html,2为txt(支持的格式不限与此,其他格式暂为列出) static final int wdFormatPDF = 17;// PDF 格式 static final int wdFormatHTML = 8;// HTML 格式 static final int wdFormatTXT = 2;// TXT 格式 /** * Word文档转换 * * @param fromfileName * @param toFileName * @author SHANHY */ public void wordConvert(String fromfileName, String toFileName) { System.out.println("启动Word..."); ComThread.InitSTA(); long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent("Word.Application");//创建一个word对象 app.setProperty("Visible", new Variant(false)); //不可见打开word app.setProperty("AutomationSecurity", new Variant(3)); //禁用宏 Dispatch docs = app.getProperty("Documents").toDispatch();//获取文挡属性 System.out.println("打开文档 >>> " + fromfileName); //Object[]第三个参数是表示“是否只读方式打开” doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { fromfileName, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); File tofile = new File(toFileName); if (tofile.exists()) { tofile.delete(); } int formatValue = -1; if(toFileName.toLowerCase().endsWith(".pdf")){ formatValue = wdFormatPDF; }else if(toFileName.toLowerCase().endsWith(".html")){ formatValue = wdFormatHTML; }else if(toFileName.toLowerCase().endsWith(".txt")){ formatValue = wdFormatTXT; }else{ formatValue = -1; } if(formatValue != -1){ System.out.println("转换文档 ["+fromfileName+"] >>> ["+toFileName+"]"); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toFileName, new Variant(formatValue) }, new int[1]); }else{ System.out.println("转换文件到目标文档不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); } long end = System.currentTimeMillis(); System.out.println("用时:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { Dispatch.call(doc, "Close", false); System.out.println("关闭文档"); if (app != null) app.invoke("Quit", new Variant[] {}); } // 如果没有这句话,winword.exe进程将不会关闭 ComThread.Release(); ComThread.quitMainSTA(); } /** * PPT(PowerPoint)文档转换 * * @param fromfileName * @param toFileName * @author SHANHY */ public void pptConvert(String fromfileName, String toFileName) { System.out.println("启动PPT..."); ComThread.InitSTA(); long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent("Word.Application");//创建一个word对象 app.setProperty("Visible", new Variant(false)); //不可见打开word app.setProperty("AutomationSecurity", new Variant(3)); //禁用宏 Dispatch docs = app.getProperty("Documents").toDispatch();//获取文挡属性 System.out.println("打开文档 >>> " + fromfileName); //Object[]第三个参数是表示“是否只读方式打开” doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { fromfileName, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); File tofile = new File(toFileName); if (tofile.exists()) { tofile.delete(); } int formatValue = -1; if(toFileName.toLowerCase().endsWith(".pdf")){ formatValue = wdFormatPDF; }else if(toFileName.toLowerCase().endsWith(".html")){ formatValue = wdFormatHTML; }else if(toFileName.toLowerCase().endsWith(".txt")){ formatValue = wdFormatTXT; }else{ formatValue = -1; } if(formatValue != -1){ System.out.println("转换文档 ["+fromfileName+"] >>> ["+toFileName+"]"); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toFileName, new Variant(formatValue) }, new int[1]); }else{ System.out.println("转换文件到目标文档不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); } long end = System.currentTimeMillis(); System.out.println("用时:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { Dispatch.call(doc, "Close", false); System.out.println("关闭文档"); if (app != null) app.invoke("Quit", new Variant[] {}); } // 如果没有这句话,winword.exe进程将不会关闭 ComThread.Release(); ComThread.quitMainSTA(); } public static void main(String[] args) { ConvertToPdf d = new ConvertToPdf(); d.wordConvert("g:\test.docx", "g:\test.pdf"); }}
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Variant; import com.jacob.com.Dispatch; public class Word { String strDir = "c:jacob_1.9"; String strInputDoc = strDir + "file_in.doc"; String strOutputDoc = strDir + "file_out.doc"; String strOldText = "[label:import:1]"; String strNewText = "I am some horribly long sentence, so long that [insert anything]"; boolean isVisible = true; boolean isSaveOnExit = true; public Word() { ActiveXComponent oWord = new ActiveXComponent("Word.Application"); oWord.setProperty("Visible", new Variant(isVisible)); Dispatch oDocuments = oWord.getProperty("Documents").toDispatch(); Dispatch oDocument = Dispatch.call(oDocuments, "Open", strInputDoc). toDispatch(); Dispatch oSelection = oWord.getProperty("Selection").toDispatch(); Dispatch oFind = oWord.call(oSelection, "Find").toDispatch(); Dispatch.put(oFind, "Text", strOldText); Dispatch.call(oFind, "Execute"); Dispatch.put(oSelection, "Text", strNewText); Dispatch.call(oSelection, "MoveDown"); Dispatch.put(oSelection, "Text", "nSo we got the next line including BR.n"); Dispatch oFont = Dispatch.get(oSelection, "Font").toDispatch(); Dispatch.put(oFont, "Bold", "1"); Dispatch.put(oFont, "Italic", "1"); Dispatch.put(oFont, "Underline", "0"); Dispatch oAlign = Dispatch.get(oSelection, "ParagraphFormat"). toDispatch(); Dispatch.put(oAlign, "Alignment", "3"); Dispatch oWordBasic = (Dispatch) Dispatch.call(oWord, "WordBasic"). getDispatch(); Dispatch.call(oWordBasic, "FileSaveAs", strOutputDoc); Dispatch.call(oDocument, "Close", new Variant(isSaveOnExit)); oWord.invoke("Quit", new Variant[0]); } public static void main(String[] args) { Word word = new Word(); } }
部分内容参考: http://www.cnblogs.com/vulcans/archive/2009/09/08/1562588.html
热心网友 时间:2022-04-21 06:52
居中一般都是使用css来实现。如果你现在文件中各个内容的width已经用数字标明了,就再加上margin-left:auto;margin-right:auto;就可以了(或者简写为margin:0 auto;)如果width还没有指明,你还需要提前把width的值写上热心网友 时间:2022-04-21 08:10
你变成html后是表格还是段落热心网友 时间:2022-04-21 09:45
margin:0 auto