package newDoc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class JacobDoc{
// 代表一个word 程序
private ActiveXComponent MsWordApp = null;
// 代表进行处理的word 文档
private Dispatch document = null;
public static final String [] KV = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
//初始化
public JacobDoc() {
if (MsWordApp == null) {
MsWordApp = new ActiveXComponent("Word.Application");
}
}
// 设置是否在前台打开 word 程序 ,
public void setVisible(boolean visible) {
MsWordApp.setProperty("Visible", new Variant(visible));
}
// 打开一个存在的word文档,并用document 引用 引用它
public void openFile(String wordFilePath) {
try{
Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
document = Dispatch.call(documents, "Open", wordFilePath,
new Variant(true)/* 是否进行转换ConfirmConversions */,
new Variant(false)/* 是否只读 */).toDispatch();
}catch (Exception e){
e.printStackTrace();
}
}
//创建一个新的word文档
public void createNewDocument() {
Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
document = Dispatch.call(documents, "Add").toDispatch();
}
// word 中在对表格进行遍历的时候 ,是先列后行 先column 后cell
// 另外下标从1开始
public void insertTable(List<List> list,String temp) {
try{
int row=list.get(0).size();//行数
int column=list.size();//列数
// 输入内容需要的对象
Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
// 建立表格
Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
// 移动到文件开头
Dispatch.call(selection, "HomeKey", new Variant(6));
//选中表格插入区域
find(selection,temp);
//定义表格插入位置
Dispatch range = Dispatch.get(selection, "Range").toDispatch();
// 设置row,column,表格外框宽度
Dispatch newTable = Dispatch.call(tables, "Add", range,new Variant(row), new Variant(column), new Variant(1)).toDispatch();
// 此表的所有列
Dispatch cols = Dispatch.get(newTable, "Columns").toDispatch();
// 一共有多少列 实际上这个数==column
int colCount = Dispatch.get(cols, "Count").changeType(Variant.VariantInt).getInt();
// 循环取出每一列
for (int i = 1; i <= colCount; i++) {
Dispatch col = Dispatch.call(cols, "Item", new Variant(i))
.toDispatch();
Dispatch cells = Dispatch.get(col, "Cells").toDispatch();// 当前列中单元格
int cellCount = Dispatch.get(cells, "Count").changeType(
Variant.VariantInt).getInt();// 当前列中单元格数 实际上这个数等于row
for (int j = 1; j <= cellCount; j++) {// 每一列中的单元格数
putTxtToCell(newTable, j, i, list.get(i-1).get(j-1)+"");// 向单元格添加内容
}
}
}catch (Exception e){
e.printStackTrace();
}
}
/** */
/**
* 在指定的单元格里填写数据
*
* @param tableIndex
* @param cellRowIdx
* @param cellColIdx
* @param txt
*/
public void putTxtToCell(Dispatch table, int cellRowIdx, int cellColIdx,
String txt) {
Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
new Variant(cellColIdx)).toDispatch();
Dispatch.call(cell, "Select");
Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 输入内容需要的对象
Dispatch font = Dispatch.get(selection, "Font").toDispatch();// 字形格式化需要的对象
if(cellRowIdx==1){
Dispatch shading = Dispatch.get(selection, "Shading" ).toDispatch();
Dispatch.put(shading, "BackgroundPatternColorIndex" ,new Variant(16));
Dispatch.put(font, "Bold", "4");
}
Dispatch.put(font, "Size", "10");
Dispatch.put(font, "Color", "0,0,0,0"); // 字型颜色
//Dispatch.put(font, "Italic", "1"); //字型斜体
Dispatch.put(selection, "Text", txt);
}
/** */
/**
* 从选定内容或插入点开始查找文本
*
* @param toFindText
* 要查找的文本
* @return boolean true-查找到并选中该文本,false-未查找到文本
*/
public boolean find(Dispatch selection,String toFindText) {
if (toFindText == null || toFindText.equals(""))
return false;
// 从selection所在位置开始查询
Dispatch find = Dispatch.call(selection, "Find").toDispatch();
// 设置要查找的内容
Dispatch.put(find, "Text", toFindText);
// 向前查找
Dispatch.put(find, "Forward", "True");
// 设置格式
Dispatch.put(find, "Format", "True");
// 大小写匹配
Dispatch.put(find, "MatchCase", "True");
// 全字匹配
Dispatch.put(find, "MatchWholeWord", "True");
// 查找并选中
return Dispatch.call(find, "Execute").getBoolean();
}
/** */
/**
* 把选定选定内容设定为替换文本
*
* @param toFindText
* 查找字符串
* @param newText
* 要替换的内容
* @return
*/
public void replaceText(String toFindText, String newText){
Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 输入内容需要的对象
Dispatch.call(selection, "HomeKey", new Variant(6));// 移动到文件开头
while (find(selection, toFindText)) {
Dispatch.put(selection, "Text", newText);
Dispatch.call(selection, "MoveRight");
}
}
/** *//**
* 在当前文档指定的位置拷贝来自另一个工作表Excel中的图表
*
* @param anotherDocPath 工作表Excel的路径
* @param tableIndex 被拷贝的图表在工作表Excel中的序号(从1开始)
* @param pos 当前文档指定的位置
* @param list 数据源
* @param title 图表标题
*/
public void copyChartFromAnotherExcel(String anotherExcelPath, int chartIndex,
String pos,List<Object[]> list,List<Object[]> titles,String title){
ActiveXComponent app = null;
Dispatch workbooks = null;
Dispatch workbook = null;
try{
app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", new Variant(false));
workbooks = app.getProperty("Workbooks").toDispatch();
workbook = Dispatch.call(workbooks, "Open", anotherExcelPath).toDispatch();
Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 输入内容需要的对象
Dispatch sheet = Dispatch.get(workbook,"ActiveSh
- 1
- 2
- 3
前往页