您的位置:首页 > 其它

Jacob生成word文档 由打卡记录生成加班和调休申请单

2013-03-25 22:52 232 查看
需求: 公司每个月初,人事部会把我们上个月的打卡记录(一个excel文档)发给我们。由我们根据每个人的打卡记录自己填写加班声请 单和调休声请单。 每个月都要填写,而且要一个个的对照填写。实在受不了。于是工作之余搞了这个节约下写这个破单子 的时间。 起码每个月能节约20分钟。10个同事一起每个月团队就节约了200分钟。让他们其他部门的孩子慢慢去对比去吧。

准备:

1: jacob包 http://sourceforge.net/projects/jacob-project/ 要把dll文件拷到system32下。所有的这种JNI的包发现都是拷到这个目录下。

2: word对象模型参考 打开word帮助。 在搜索按钮中有个脱机开发人员帮助,选择这个,并选择Word 对象模型参考 (这个就是你用jacob生成word文件时需要查看的API)

前面记得有个快捷键可以直接在打开这个对象模型参考的。忘记啦。

效果:

考勤记录文件格式:



生成的2个word文件格式:





代码: 贴出部分代码。完整测试工程就不改好了。直接放到资源下啦,。 没有怎么封装。由于目的很简单就没有考虑那么多啦。谁叫自己懒啦。

部分生成word的代码:当然刚开始写这个开始search了下其本的使用jacob的代码。最重要的就是怎么给对象赋属性 取对象属性,调用对象属性。晓得这个了再看word对象模型你想干啥就可以干啥啦。工程就不上传啦。因为工程依赖的excel和word有其他同事的信息,上传上去不太好。提供编写代码的思路和方法自己摸索还有意思些,其实工程也没什么只是读excel分装数据对象再根据上班下班时间计算生成数据对象给下面方法调用。 本来还想着封装下jacob让他像freemarker模板一样,以后只要自己把word格式搞出来,程序只要往里面加数据就行啦。无赖看了对象模型

,我觉得还是陪女朋友去秀十字绣比较靠谱些。

public static void generateExtraWorkWord(ExtraWorkParam extraWorkParam,String wordType,boolean isExtraWork)
{
// 1:初始化com的线程
ComThread.InitSTA();

// 2:初始化word应用程序,新建一个空白文档,取得文档内容对象
ActiveXComponent objWord = new ActiveXComponent("Word.Application");

Dispatch wordObject = (Dispatch) objWord.getObject();

// new Variant(true)表示word应用程序可见
// 设置一个对象的属性的时候,利用Dispatch的put方法,给属性赋值。上面这行语句相当于vb的 wordObject.Visible =
// true 语句
Dispatch.put(wordObject, "Visible", new Variant(true));// new
// Variant(true)表示word应用程序可见

// //documents表示word的所有文档窗口,(word是多文档应用程序)
Dispatch documents = objWord.getProperty("Documents").toDispatch();

// 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档
// Tip:调用一个对象的方法的时候,利用Dispatch的call方法,上面的语句相当于vb的document =
// documents.Add() 语句。
Dispatch document = Dispatch.call(documents, "Add").toDispatch();

// 取得word文件的内容
// Tip:取得一个对象的成员变量(属性)时利用Dispatch的get方法,上面的语句相当于vb的wordContent =
// document.Content语句
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch();

// 取得word文档的内容后,可以对其内容进行操作
Dispatch.call(wordContent, "InsertAfter", "××××有限公司");// 插入一个段落
Dispatch.call(wordContent, "InsertAfter", "\r\n" + wordType +  "\r\n");
Dispatch.call(wordContent, "InsertAfter", " " + extraWorkParam.getExtraWorkYear()+"年"+extraWorkParam.getExtraWorkMonth()+"月" + "\r\n");
// 4. 设置刚插入的段落的文字格式
Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs")
.toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs, "Count").getInt(); // 一共的段落数
// 找到刚输入的段落,设置格式
Dispatch titleParagraph = Dispatch.call(paragraphs, "Item",
new Variant(1)).toDispatch(); // 第1段
setParagraphAlignment(titleParagraph, TestJacob.align_center);
Dispatch titleParagraphRange = Dispatch.get(titleParagraph, "Range")
.toDispatch();
Dispatch titlefont = Dispatch.get(titleParagraphRange, "Font")
.toDispatch();
setCommonSeeFontAttribute(titlefont, true, false,
TestJacob.FONT_NAME_SONG_TI, 18);
// Dispatch.put(font, "", val)
// 注意:如果想插入一个新的空白行,也需要设置段落的文字格式,否则新插入行的文字格式会于刚插入的段落的格式相同。

// 找到刚输入的段落,设置格式居中对齐
Dispatch docTypeParagraph = Dispatch.call(paragraphs, "Item",
new Variant(2)).toDispatch(); // 第2段
setParagraphAlignment(docTypeParagraph, align_center);

Dispatch docTypeParagraphRange = Dispatch
.get(docTypeParagraph, "Range").toDispatch();

Dispatch docTypeFont = Dispatch.get(docTypeParagraphRange, "Font")
.toDispatch();
setCommonSeeFontAttribute(docTypeFont, false, false, FONT_NAME_SONG_TI,
16);

Dispatch timeParagraph = Dispatch.call(paragraphs, "Item",new Variant(3)).toDispatch();
setParagraphAlignment(timeParagraph, align_right);
Dispatch timeParagraphRange = Dispatch.get(timeParagraph, "Range").toDispatch();

Dispatch insertTableRange = Dispatch.call(timeParagraphRange, "Next").toDispatch();

Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
Dispatch table = Dispatch.call(tables, "Add",
new Object[]{ insertTableRange,new Variant(2), new Variant(1) }).toDispatch();
// Dispatch tableRange = Dispatch.get(table, "Range").toDispatch();
//		Dispatch.put(table, "Enable", new Variant(true));
Dispatch borders = Dispatch.get(table,"Borders").toDispatch();
setTableBordersStyle(borders, 1, 2);// 单实线 0.25 磅。

Dispatch.put(table, "Borders", borders);
Dispatch.put(table, "PreferredWidthType", new Variant(2));// 百分比
Dispatch.put(table, "PreferredWidth", new Variant(110));
Dispatch.put(table, "AllowAutoFit", new Variant(true));

Dispatch cellOneRow = Dispatch.call(table, "cell",new Object[]{new Variant(1),new Variant(1)}).toDispatch();
Dispatch.call(cellOneRow, "Split", new Object[]{new Variant(1),new Variant(6)});

getCellAndSetContent(table, 1, 1, "姓名");
getCellAndSetContent(table, 1, 2, extraWorkParam.getName());
getCellAndSetContent(table, 1, 3, "部门");
getCellAndSetContent(table, 1, 4, extraWorkParam.getDepart());
getCellAndSetContent(table, 1, 5, "职务");
getCellAndSetContent(table, 1, 6, extraWorkParam.getPost());

Dispatch firstRow = Dispatch.get(Dispatch.get(table,"Rows").toDispatch(), "First").toDispatch();
Dispatch firstRowRange = Dispatch.get(firstRow, "Range").toDispatch();
Dispatch firstRowFont = Dispatch.get(firstRowRange, "Font").toDispatch();
setCommonSeeFontAttribute(firstRowFont, true, false, FONT_NAME_SONG_TI, 14);

Dispatch secondRowCell = Dispatch.call(table,"cell",new Object[]{new Variant(2),new Variant(1)}).toDispatch();
Dispatch secondRowCellRange = Dispatch.get(secondRowCell, "Range").toDispatch();
Dispatch.call(secondRowCellRange, "InsertAfter","\r\n      \r\n");
Dispatch.call(secondRowCellRange, "InsertAfter","\r\n      \r\n");

Dispatch insideTableRange = Dispatch.call(secondRowCellRange, "GoToNext", new Object[]{3}).toDispatch();

//		Dispatch insideTableRange = Dispatch.call(secondRowCellRange, "Next").toDispatch();
Dispatch insideTables = Dispatch.get(insideTableRange, "Tables").toDispatch();
Dispatch insideTable = Dispatch.call(insideTables, "Add", new Object[]{secondRowCellRange,new Variant(extraWorkParam.getExtraWorkDayCount()+2+1),new Variant(3)}).toDispatch();
Dispatch insideTableBorders = Dispatch.get(insideTable,"Borders").toDispatch();
setTableBordersStyle(insideTableBorders, 1, 2);// 单实线 0.25 磅。
Dispatch insideTableRows = Dispatch.get(insideTable, "Rows").toDispatch();

int rowsNumber = Dispatch.get(insideTableRows, "Count").getInt();

Dispatch insideTableFirstRow = Dispatch.get(insideTableRows, "First").toDispatch();
Dispatch insideTableLastRow = Dispatch.get(insideTableRows, "Last").toDispatch();

Dispatch insideTableFirstRowCells = Dispatch.get(insideTableFirstRow, "Cells").toDispatch();
Dispatch.call(insideTableFirstRowCells, "Merge");

Dispatch insideTableLastRowCells = Dispatch.get(insideTableLastRow, "Cells").toDispatch();
Dispatch.call(insideTableLastRowCells, "Merge");

Dispatch firstRowCells = Dispatch.call(insideTable, "cell", new Object[]{1,1}).toDispatch();
Dispatch insideTableFirstCellRange = Dispatch.get(firstRowCells, "Range").toDispatch();
if(isExtraWork)
{
Dispatch.call(insideTableFirstCellRange, "InsertAfter", "加班时间:");
}else
{
Dispatch.call(insideTableFirstCellRange, "InsertAfter", "调休时间:");
}
Dispatch firstRowBorders = Dispatch.get(firstRowCells, "Borders").toDispatch();
Dispatch firstRowBottomBorder = Dispatch.call(firstRowBorders, "Item",3).toDispatch();//边框顺序为从底部开始逆时针转
Dispatch.put(firstRowBorders, "Enable", new Variant(false));
Dispatch.put(firstRowBottomBorder, "Visible", new Variant(true));

Dispatch insideTableTitleColOne = Dispatch.call(insideTable, "cell", new Object[]{2,1}).toDispatch();
Dispatch.call(Dispatch.get(insideTableTitleColOne, "Range").toDispatch(), "InsertAfter", "日期");
Dispatch insideTableTitleColTwo = Dispatch.call(insideTable, "cell", new Object[]{2,2}).toDispatch();
Dispatch.call(Dispatch.get(insideTableTitleColTwo, "Range").toDispatch(), "InsertAfter", "时间段");
Dispatch insideTableTitleColThree = Dispatch.call(insideTable, "cell", new Object[]{2,3}).toDispatch();
Dispatch.call(Dispatch.get(insideTableTitleColThree, "Range").toDispatch(), "InsertAfter", "小计(单位:小时)");

List<ExtraWorkTime> listExtraWorkTime = extraWorkParam.getListExtraWorkTime();
int size = listExtraWorkTime.size();
ExtraWorkTime tempExtraWorkTime = null;
for (int i = 0; i < size; i++)
{
tempExtraWorkTime = listExtraWorkTime.get(i);
Dispatch tempOne = Dispatch.call(insideTable, "cell", new Object[]{3+i,1}).toDispatch();
Dispatch.call(Dispatch.get(tempOne, "Range").toDispatch(), "InsertAfter", tempExtraWorkTime.getDate());
Dispatch tempTwo = Dispatch.call(insideTable, "cell", new Object[]{3+i,2}).toDispatch();
Dispatch.call(Dispatch.get(tempTwo, "Range").toDispatch(), "InsertAfter", tempExtraWorkTime.getStartTime() + ExtraWorkTime.SEPARATOR_FOR_TIME + tempExtraWorkTime.getEndTime());
Dispatch tempThree = Dispatch.call(insideTable, "cell", new Object[]{3+i,3}).toDispatch();
Dispatch.call(Dispatch.get(tempThree, "Range").toDispatch(), "InsertAfter", tempExtraWorkTime.getSumHour());
}

Dispatch lastRowCells = Dispatch.call(insideTable, "cell", new Object[]{rowsNumber,1}).toDispatch();
Dispatch insideTableLastCellRange = Dispatch.get(lastRowCells, "Range").toDispatch();

Dispatch.call(insideTableLastCellRange, "InsertAfter", "合计:" + extraWorkParam.getSumTotalExtraWorkTime() + "小时\r\n");
if (isExtraWork)
{
Dispatch.call(insideTableLastCellRange, "InsertAfter", "加班事由:" + extraWorkParam.getExtraWorkReason() + "\r\n");
}
Dispatch.call(insideTableLastCellRange, "InsertAfter", "部门负责人审批:" + "     " + "\r\n");
Dispatch.call(insideTableLastCellRange, "InsertAfter", "分管领导审批:" + "      " + " \r\n");
Dispatch.call(insideTableLastCellRange, "InsertAfter", "人力资源部审批:" + "      " + "\r\n");

Dispatch lastRowFont = Dispatch.get(insideTableLastCellRange, "Font").toDispatch();
setCommonSeeFontAttribute(lastRowFont, false, false, FONT_NAME_SONG_TI, 14);
Dispatch lastRowBorders = Dispatch.get(lastRowCells, "Borders").toDispatch();
Dispatch lastRowTopBorder = Dispatch.call(lastRowBorders, "Item",1).toDispatch();
//		int bordersCount = Dispatch.get(lastRowBorders, "Count").getInt();
Dispatch.put(lastRowBorders, "Enable", new Variant(false));
Dispatch.put(lastRowTopBorder, "Visible", new Variant(true));

String generateDocName = extraWorkParam.getDepart()+ DOC_NAME_SEPARATOR +  extraWorkParam.getName()
+ DOC_NAME_SEPARATOR +  extraWorkParam.getExtraWorkYear()+"年"+extraWorkParam.getExtraWorkMonth()
+ "月" + wordType;

// 5. 将当前文档保存
Dispatch.call(document, "SaveAs", new Variant(
StringUtils.DIR_FOR_WORD_GENERATE + "/" + generateDocName + ".doc")); // 保存一个新文档

// 6. 释放COM线程
ComThread.Release();// 释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}

public static void getCellAndSetContent(Dispatch tableDispatch,int row , int col, String content)
{
Dispatch firstCell = Dispatch.call(tableDispatch, "cell", new Object[]{new Variant(row),new Variant(col)}).toDispatch();
Dispatch.call(Dispatch.get(firstCell, "Range").toDispatch(), "InsertAfter", content);
}

public static void setTableBordersStyle(Dispatch tableBordersDispatch,int lineStyle,int lineWidth)
{
Dispatch.put(tableBordersDispatch, "Enable", new Variant(true));

}

public static void setBorderLineStyleAndWidth(Dispatch borderDispatch,int lineStyle,int lineWidth)
{
Dispatch.put(borderDispatch, "LineStyle", new Variant(lineStyle));//wdLineStyleSingle 1 单实线
Dispatch.put(borderDispatch, "LineWidth", new Variant(lineWidth));//wdLineWidth025pt 2 0.25 磅。
}

public static void setCommonSeeFontAttribute(Dispatch titlefont,
boolean isBold, boolean isItalic, String name, int size)
{
Dispatch.put(titlefont, "Bold", new Variant(isBold)); // 设置为黑体
Dispatch.put(titlefont, "Italic", new Variant(isItalic)); // 设置为斜体
Dispatch.put(titlefont, "Name", new Variant(name)); //
Dispatch.put(titlefont, "Size", new Variant(size)); // 小四 sss
}

public static void setParagraphAlignment(Dispatch paragraph,
int alignmentType)
{
Dispatch.put(paragraph, "Alignment", new Variant(align_center));
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: