6:Jacob:需要借助
Office的com组件(jacob.jar)
所以这个只能放在装有office环境的windows环境下。
把jacob-1.16-M2-x86.dll放在C:\WINDOWS\
system32目录下
static int WORD_HTML = 8;
static int WORD_TXT = 7;
static int EXCEL_HTML = 44;
/**
*1. 在Word文档插入图片
* @param fileWordPath:word文档绝对路径
* @param fileImagePath:需要插入的图片的绝对路径
* @return 是否插入图片成功
*/
public static boolean insertImage2Word(String fileWordPath,String fileImagePath) {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
try {
app.setProperty("Visible", new Variant(false)); // 是否前台打开word程序,或者后台运行
Dispatch documents = Dispatch.get(app, "Documents") .toDispatch();
Dispatch document = Dispatch.call(documents, "Open", fileWordPath,
new Variant(true)/* 是否进行转换ConfirmConversions */,
new Variant(false)/* 是否只读 */).toDispatch();
// 插入图片(注意刚打开的word ,光标处于开头,故图片在最前方插入)
Dispatch selection = Dispatch.get(app, "Selection").toDispatch();
Dispatch image = Dispatch.get(selection, "InLineShapes").toDispatch();
Dispatch.call(image, "AddPicture", fileImagePath);
Dispatch.call(document, "Save");// 保存
Dispatch.call(document, "Close", new Variant(0)); // 关闭
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();//释放进程
}
return true;
}
/**
* 2. WORD转HTML
* @param docfile:WORD文件绝对路径
* @param htmlfile:转换后HTML存放的绝对路径
* @return 是否转换成功
*/
public static boolean wordToHtml(String docfile, String htmlfile) {
ActiveXComponent app = new ActiveXComponent("Word.Application");
try {
app.setProperty("Visible", new Variant(false));//设置打开方式为后台打开
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(docs, "Open",Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
return true;
}
/**
* 3. HTML转WORD
* @param html:HTML文件绝对路径
* @param docFile:生成的WORD文件存放的绝对路径
* @return 是否转换成功
*/
public static boolean htmlToWord(String html, String docFile) {
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application"); // 启动word
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(docs, "Open",Dispatch.Method,
new Object[] { html, new Variant(false),
new Variant(true) }, new int[1]) toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
docFile, new Variant(true) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
return false;
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
return true;
}
/**
*4. excel07转excel03
* @param excel07File:excel07文件的绝对路径
* @param excel03File:转换
后生成excel03文件存放的绝对路径
* @return 是否转换成功
*/
public static boolean excel07ToExcel03(String excel07File, String excel03File) {
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel
try {
app.setProperty("DisplayAlerts", new Variant(false));
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(excels, "Open",Dispatch.Method,
new Object[] { excel07File, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
excel03File, new Variant(true) }, new int[1]);
Dispatch.call(excel, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
return true;
}
/**
*5. EXCEL转HTML
* @param xlsfile :Excel文件绝对路径
* @param htmlfile:生成的HTML文件存放的绝对路径
* @return 是否转换成功
*/
public static boolean excelToHtml(String xlsfile, String htmlfile)
{
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel
try {
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(excels, "Open",Dispatch.Method,
new Object[] { xlsfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
}
catch (Exception e) {
e.printStackTrace();
}
finally{
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
return true;
}
/**
* 6. word文档中插入表格
* @param filePath --word文件绝对路径
* @param rowsNum --插入位置在第几行
* @param colsNum --插入位置在第几列
* @param vals --表格中填充的数据
* @return 是否转换成功
*/
public static boolean wordAddTable(String filePath, int rowsNum,int colsNum,String[][] vals){
ActiveXComponent msWordApp = null;
int rowNum = vals.length; //表格行数
int colNum = vals[0].length; //表格列数
try {
msWordApp = new ActiveXComponent("Word.Application");//启动word程序
msWordApp.setProperty("Visible", new Variant(false));// 是否前台打开word程序,或者后台运行
Dispatch documents = Dispatch.get(msWordApp, "Documents").toDispatch();
Dispatch document = Dispatch.call(documents, "Open", filePath,
new Variant(true),
new Variant(false)).toDispatch();
// 插入表格(注意刚打开的word ,光标处于开头)
Dispatch selection = Dispatch.get(msWordApp, "Selection").toDispatch();
// Dispatch.call(selection, "TypeParagraph"); // 空一行段落
for(int r= 0;r<rowsNum;r++) {
Dispatch.call(selection, "MoveDown"); // 游标往下一行
}
for(int c=0;c<colsNum;c++) {
// Dispatch.call(selection, "MoveRight"); // 游标往右一格
}
Dispatch range = Dispatch.get(selection, "Range").toDispatch(); //当前光标位置或者选中的区域
Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); // 建立表格
//设置表格行数,列数,外格宽度
Dispatch newTable = Dispatch.call(tables, "Add", range,new Variant(rowNum), new Variant(colNum), new Variant(1)).toDispatch();
Dispatch cols = Dispatch.get(newTable, "Columns").toDispatch(); // 此表的所有列
int colCount = Dispatch.get(cols, "Count").changeType(Variant.VariantInt).getInt();//获取表格列数
//填充表格
for (int i = 0; i < colCount; i++) { //
循环取出每一列
Dispatch col = Dispatch.call(cols, "Item", new Variant(i+1)).toDispatch();
// 当前列中单元格
Dispatch cells = Dispatch.get(col, "Cells").toDispatch();
// 当前列中单元格数
Int cellCount = Dispatch.get(cells, "Count").changeType(Variant.VariantInt).getInt();
for (int j = 0; j < cellCount; j++) {
//当前单元格
Dispatch cell = Dispatch.call(cells, "Item", new Variant(j+1)). toDispatch();
//选中当前单元格
Dispatch.call(cell, "Select");
//往选中的区域中填值,也就是往当前单元格填值
Dispatch.put(selection, "Text",vals[j][i]);
}
}
Dispatch.call(document, "Save");// 保存
// 关闭
Dispatch.call(document, "Close", new Variant(0));
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
msWordApp.invoke("Quit", new Variant[] {});
ComThread.Release();
}
return true;
}
- jacob-1.16-M2-x86.rar (60.4 KB)
- 下载次数: 0
- jacob.jar (46.7 KB)
- 下载次数: 0