導航:首頁 > 編程語言 > java生成07版的docx

java生成07版的docx

發布時間:2024-09-28 19:32:50

A. java代碼讀寫word2007文件 .docx後綴的問題,你解決了嗎是怎樣處理的呢

docx 其實是用zip打包了的一堆xml,所以仍然是 binary 格式的
你先嘗試下,把你 attachFile.getFileContent 的內容拿出來,直接寫本地看看
contenttype 這里也看不到你寫的是啥,所以不能判斷

B. java讀取帶格式word內容

用jacob吧。。

/**
*@author eyuan
*/
package per.eyuan.word2txt.core;

import com.jacob.*;
import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;
import java.util.Scanner;

public class Core {
/**
* 實現轉換的函數
* @param sourceFilesPath
* @param destinationFilesPath
* @param destinationFilesType
* @return void
* @see import com.jacob.activeX.*;
*/
public static void change(String sourceFilesPath,String destinationFilesPath,int destinationFilesType){
//使用word文件所在的目錄(源路徑)建立目錄文件
File sourcePathFile=new File(sourceFilesPath);
//取得word文件(源文件列表)
File sourceFilesList[]=sourcePathFile.listFiles();
System.out.println("共有"+sourceFilesList.length+"個文件(文件夾)");
//指定要轉換的文件所在的目錄下,如果有子目錄,
//則進入子目錄,繼續查找word文檔並將其轉換,
//直到將指定目錄下的所有word文檔轉換完。
//子目錄名
String sourceChildPath=new String("");
//保持原來的層次關系,將子目錄下的文件存放在新建的子目錄中
String destiNationChildPath=new String("");
//檢索文件,過濾掉非word文件,通過擴展名過濾
for(int i=0;i<sourceFilesList.length;i++){
//排除掉子文件夾
if(sourceFilesList[i].isFile()){
System.out.println("第"+(i+1)+"個文件:");
//取得文件全名(包含擴展名)
String fileName=sourceFilesList[i].getName();
String fileType=new String("");
//取得文件擴展名
fileType=fileName.substring((fileName.length()-4), fileName.length());
//word2007-2010擴展名為docx
//判斷是否為word2007-2010文檔,及是否以docx為後綴名
if(fileType.equals("docx")){
System.out.println("正在轉換。。。");
//輸出word文檔所在路勁
System.out.println("目錄:"+sourceFilesPath);
//輸出word文檔名
System.out.println("文件名:"+fileName);
//System.out.println(fileName.substring(0, (fileName.length()-5)));
//核心函數
//啟動word
ActiveXComponent app=new ActiveXComponent("Word.Application");
//要轉換的文檔的全路徑(所在文件夾+文件全名)
String docPath=sourceFilesPath+"\\"+fileName;
//轉換後的文檔的全路徑(所在文件夾+文件名)
String othersPath=destinationFilesPath+"\\"+fileName.substring(0,(fileName.length()-5));
//
String inFile=docPath;
String outFile=othersPath;
//
boolean flag=false;
//核心代碼
try{
//設置word可見性
app.setProperty("Visible", new Variant(false));
//
Dispatch docs=app.getProperty("Documents").toDispatch();
//打開word文檔
Dispatch doc=Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{inFile,new Variant(false),new Variant(true)}, new int[1]).toDispatch();
//0:Microsoft Word 97 - 2003 文檔 (.doc)
//1:Microsoft Word 97 - 2003 模板 (.dot)
//2:文本文檔 (.txt)
//3:文本文檔 (.txt)
//4:文本文檔 (.txt)
//5:文本文檔 (.txt)
//6:RTF 格式 (.rtf)
//7:文本文檔 (.txt)
//8:HTML 文檔 (.htm)(帶文件夾)
//9:MHTML 文檔 (.mht)(單文件)
//10:MHTML 文檔 (.mht)(單文件)
//11:XML 文檔 (.xml)
//12:Microsoft Word 文檔 (.docx)
//13:Microsoft Word 啟用宏的文檔 (.docm)
//14:Microsoft Word 模板 (.dotx)
//15:Microsoft Word 啟用宏的模板 (.dotm)
//16:Microsoft Word 文檔 (.docx)
//17:PDF 文件 (.pdf)
//18:XPS 文檔 (.xps)
//19:XML 文檔 (.xml)
//20:XML 文檔 (.xml)
//21:XML 文檔 (.xml)
//22:XML 文檔 (.xml)
//23:OpenDocument 文本 (.odt)
//24:WTF 文件 (.wtf)
//另存為指定格式的文檔
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{outFile,new Variant(destinationFilesType)}, new int[1]);
//
Variant file=new Variant(false);
//關閉文檔
Dispatch.call(doc, "Close",file);
//
flag=true;
}catch(Exception e){
e.printStackTrace();
System.out.println("文檔轉換失敗");
}finally{
app.invoke("Quit",new Variant[]{});
}
System.out.println("轉換完畢");
}
//word97-2003擴展名為doc
//判斷是否為word2003-2007文檔,及是否以doc為後綴名
else if(fileType.equals(".doc")){
System.out.println("正在轉換。。。");
//輸出word文檔所在路勁
System.out.println("目錄:"+sourceFilesPath);
//輸出word文檔名
System.out.println("文件名:"+fileName);
//System.out.println(fileName.substring(0, (fileName.length()-4)));
//核心函數
//啟動word
ActiveXComponent app=new ActiveXComponent("Word.Application");
//要轉換的文檔的全路徑(所在文件夾+文件全名)
String docPath=sourceFilesPath+"\\"+fileName;
//轉換後的文檔的全路徑(所在文件夾+文件名)
String othersPath=destinationFilesPath+"\\"+fileName.substring(0,(fileName.length()-4));
//
String inFile=docPath;
String outFile=othersPath;
//
boolean flag=false;
//核心代碼
try{
//設置word可見性
app.setProperty("Visible", new Variant(false));
//
Dispatch docs=app.getProperty("Documents").toDispatch();
//打開word文檔
Dispatch doc=Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{inFile,new Variant(false),new Variant(true)}, new int[1]).toDispatch();
//另存為指定格式的文檔
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{outFile,new Variant(destinationFilesType)}, new int[1]);
//
Variant file=new Variant(false);
//關閉文檔
Dispatch.call(doc, "Close",file);
//
flag=true;
}catch(Exception e){
e.printStackTrace();
System.out.println("文檔轉換失敗");
}finally{
app.invoke("Quit",new Variant[]{});
}
System.out.println("轉換完畢");
}
//文檔的擴展名不是doc或docx
else{
System.out.println("非word文檔");
}
}
//如果是子文件夾,則遞歸遍歷,將所有的word文檔轉換
else{
//
sourceChildPath=sourceFilesPath;
//該文件是目錄
sourceChildPath=sourceChildPath+"\\"+sourceFilesList[i].getName()+"\\";
System.out.println("源文件所在路徑:"+sourceChildPath);
//修改目標文件夾,保持原來的層級關系
destiNationChildPath=destinationFilesPath;
destiNationChildPath=destinationFilesPath+"\\"+sourceFilesList[i].getName()+"\\";
System.out.println("轉換後文件所在路徑"+destiNationChildPath);
//
mkdir(destiNationChildPath);
//遞歸遍歷所有目錄,查找word文檔,並將其轉換
change(sourceChildPath, destiNationChildPath,destinationFilesType);
}
}
System.out.println("所有文檔轉換完畢");
}
/**
* 用於創建文件夾的方法
* @param mkdirName
*/
public static void mkdir(String mkdirName){
try{
//使用指定的路徑創建文件對象
File dirFile = new File(mkdirName);
//
boolean bFile = dirFile.exists();
//已經存在文件夾,操作???提醒是否要替換
if( bFile == true ) {
System.out.println("已經存在文件夾"+mkdirName);
}
//不存在該文件夾,則新建該目錄
else{
System.out.println("新建文件夾"+mkdirName);
bFile = dirFile.mkdir();
if( bFile == true ){
System.out.println("文件夾創建成功");
}else{
System.out.println(" 文件夾創建失敗,清確認磁碟沒有防寫並且空件足夠");
System.exit(1);
}
}
}catch(Exception err){
System.err.println("ELS - Chart : 文件夾創建發生異常");
err.printStackTrace();
}finally{

}
}
/**
* 判斷某個文件夾是否存在
* @param path
*/
public static boolean isPathExist(String path){
boolean isPathExist=false;
try{
File pathFile = new File(path);
if(pathFile.exists())
isPathExist= true;
else
isPathExist= false;
}catch(Exception err){
err.printStackTrace();
}
return isPathExist;
}
/**
* 主函數
*/
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
//源文檔所在路徑
String sourceFilesPath="";
// String inputSourcePath="";
// boolean sourcePathFlag=true;
// System.out.println("請輸入要轉換文檔所在的文件夾");
// while(sourcePathFlag){
// inputSourcePath=sc.next();
// if(!isPathExist(inputSourcePath))
// System.out.println("源路徑不存在,請輸入正確的路徑");
// else
// sourcePathFlag=false;
// }
// sourceFilesPath=inputSourcePath;
sourceFilesPath="D:\\word";
//目標文檔要存放的目錄
String destinationFilesPath="";
// String inputdestinationPath="";
// boolean destinationPathFlag=true;
// System.out.println("請輸入轉換後文檔要存放的文件夾");
// while(destinationPathFlag){
// inputdestinationPath=sc.next();
// //目標文件不存在時,是否要提示用戶創建文件
// if(!isPathExist(inputdestinationPath))
// System.out.println("目標路徑不存在,請輸入正確的路徑");
// else
// destinationPathFlag=false;
// }
// destinationFilesPath=inputdestinationPath;
destinationFilesPath="D:\\txt";
//選擇要轉換的類型
int destinationFilesType=0;
int inputNumber=0;
boolean numFlag=true;
System.out.println("您要將word文檔轉換為哪種文檔格式?");
System.out.println("0:doc \t 2:txt \t 8:html \t 9:htm \t 11:xml \t 12:docx \t 17:pdf \t 18:xps");
while(numFlag){
inputNumber=sc.nextInt();
if(inputNumber!=2&&inputNumber!=8&&inputNumber!=9&&inputNumber!=11&&inputNumber!=12&&inputNumber!=17){
System.out.println("您的輸入有誤,請輸入要轉換的文檔類型前的數字");
}else
numFlag=false;
}
destinationFilesType=inputNumber;
//實行轉換
change(sourceFilesPath, destinationFilesPath,destinationFilesType);
//測試各種類型轉換
// for(int i=0;i<25;i++){
// destinationFilesType=i;
// System.out.println("文件類型"+destinationFilesType);
// System.out.println("存放目錄:"+destinationFilesPath+"\\"+i);
// mkdir(destinationFilesPath+"\\"+i);
// change(sourceFilesPath, destinationFilesPath+"\\"+i,destinationFilesType);
// }
}
}

這個我剛用的。。格式都能帶過來的。 你自己再下載個 jacob的包和dll文件

C. 用java打開一個word文檔(a.docx) 怎麼搞

package cn.rain.main;

import java.io.File;
import java.io.IOException;

public class TT {

/**
* @param args
*/
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE aa.doc");
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}
你的WORD安裝路徑C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE空格後所加的aa.doc為你的文檔文件名(有文件名重復的,專可以加路徑,寫屬法和安裝路徑的寫法一樣)
或者用Runtime.getRuntime().exec("cmd /c \"C:\\temp\\the list.doc\"");
也可以運行

閱讀全文

與java生成07版的docx相關的資料

熱點內容
蘋果電腦桌面文件夾太多了怎麼辦 瀏覽:180
搜狐視頻文件多大 瀏覽:816
qq文件存在手機哪裡 瀏覽:675
電信網路上的微信號怎麼查 瀏覽:490
涿州少兒編程哪裡有 瀏覽:736
壓縮文件之後文件夾大小不變 瀏覽:689
可編程滑鼠怎麼使用識別圖片 瀏覽:275
物理實驗數據處理有哪些 瀏覽:724
怎麼去推廣app平台 瀏覽:466
捕魚1000炮網路版 瀏覽:679
編程用的鍵盤是什麼 瀏覽:316
perl判斷文件為空 瀏覽:865
java生成07版的docx 瀏覽:276
華碩a555l升級了win10 瀏覽:820
文件夾怎麼加密win7 瀏覽:341
文件及文件夾操作 瀏覽:329
大白菜win10密鑰 瀏覽:718
ghost程序是什麼 瀏覽:233
電信營業廳手機app如何測網速 瀏覽:910
邊城浪子幾個版本 瀏覽:488

友情鏈接