/**
* 类描述:工具类,用来生成word以及word中的表格,表格中插入图片
* author: 杨辉
* 创建时间:2015-07-23
*/
package test1;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class MakeWord {
/*
* 定义常量
*/
public static final String RECORD = "--巡查报告";
public static final String CHECKTRIVAL = "巡查单位";
public static final String CHECKEDTRIVAL = "被巡查单位";
public static final String ARCHIVENAME = "案号";
public static final String ARCHIVETYPE = "案件类型";
public static final String JUDGE = "审判长";
public static final String DEPARTMENT = "承办部门";
public static final String CHECKTIME = "巡查时间";
public static final String TRIVALTIME = "开庭时间";
public static final String COUNT = "问题数量";
public static final String QUESTIONSLIST = "问题列表:";
public static final String LISTNUM = "序号";
public static final String QUESTION = "问题";
public static final String TIME = "时间";
public static final String PICTURE = "截图";
public void createDocContext(String file,CLS_VO_CheckQuestion checkq,List<CLS_VO_Questions> list)
throws DocumentException, IOException {
// 设置纸张大小
Document document = new Document(PageSize.A4);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document, new FileOutputStream(file));
document.open();
// 设置中文字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//两种字体格式
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.BLACK);
Font fontChinese2 = new Font(bfChinese, 12, Font.BOLD, Color.BLACK);
// 标题字体风格
Font titleFont = new Font(bfChinese, 14, Font.BOLD);
Paragraph title = new Paragraph(checkq.getArchiveName()+RECORD);
// 设置标题格式对齐方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
/*
* 违规次数,开庭时间,巡查时间,三个特殊类型的参数转型
*/
Integer it = checkq.getIllegalCount();//将违规次数进行转换 然后存放和使用
String strCount = it.toString();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String checkTime = df.format(checkq.getCheckTime()); //巡查时间类型转换 为字符串
String trivalTime = df.format(checkq.getTrivalTime()); //开庭时间类型转换 为字符串
// 创建一个四列的Table 表格
Table aTable = new Table(4);
int width[] = {15,35,17,33};
aTable.setWidths(width);//设置每列所占比例
aTable.setWidth(90); // 占页面宽度 90%
aTable.setAlignment(Element.ALIGN_CENTER);//居中显示
aTable.setAlignment(Element.ALIGN_MIDDLE);//纵向居中显示
aTable.setAutoFillEmptyCells(true); //自动填满
aTable.setBorderWidth(1); //边框宽度
aTable.setBorderColor(new Color(0, 0, 0)); //边框颜色
aTable.setPadding(0);//衬距,看效果就知道什么意思了
aTable.setSpacing(0);//即单元格之间的间距
aTable.setBorder(2);//边框
/*
* 第一张表格赋值
*/
String s1 = new String();
String s2 = new String();
String s3 = new String();
String s4 = new String();
for(int i=0;i<5;i++){
if ( i==0 ){
s1 = CHECKTRIVAL;
s2 = checkq.getCheckTrival();
s3 = CHECKEDTRIVAL;
s4 = checkq.getCheckedTrival();
}else if ( i==1 ){
s1 = ARCHIVENAME ;
s2 = checkq.getArchiveName();
s3 = ARCHIVETYPE;
s4 = checkq.getArchiveType();
}else if ( i==2 ){
s1 = JUDGE;
s2 = checkq.getJudge();
s3 = DEPARTMENT;
s4 = checkq.getDepartment();
}else if ( i==3 ){
s1 = CHECKTIME;
s2 = checkTime;
s3 = TRIVALTIME;
s4 = trivalTime;
}else if ( i==4 ){
s1 = COUNT;
s2 = strCount;
s3 = "";
s4 = "";
}
Cell cell = new Cell(new Phrase(s1, fontChinese2 ));
cell.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell);
Cell cell2 = new Cell (new Phrase(s2));
cell2.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell2);
Cell cell3 = new Cell (new Phrase(s3, fontChinese2 ));
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell3);
Cell cell4 = new Cell (new Phrase(s4));
cell4.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell4);
}
document.add(aTable);
document.add(new Paragraph("\n"));
//第二个标题 以及第二张表格
Paragraph title2 = new Paragraph(QUESTIONSLIST);
// 设置标题格式对齐方式
title2.setAlignment(Element.ALIGN_LEFT);
title2.setFont(titleFont);
document.add(title2);
Table aTable2 = new Table(4);
int width2[] = {10,20,20,50};
aTable2.setWidths(width2);
aTable2.setWidth(90);
aTable2.setAlignment(Element.ALIGN_CENTER);
aTable2.setAlignment(Element.ALIGN_MIDDLE);
aTable2.setAutoFillEmptyCells(true);
aTable2.setBorderWidth(1);
aTable2.setBorderColor(new Color(0, 0, 0));
aTable2.setPadding(0);
aTable2.setSpacing(0);
aTable2.setBorder(2);
Cell cel = new Cell(new Phrase(LISTNUM, fontChinese2 ));
cel.setVerticalAlignment(Element.ALIGN_TOP);
cel.setBorderColor(new Color(0, 0, 0));
aTable2.addCell(cel);
aTable2.addCell(new Cell(new Phrase(QUESTION, fontChinese2 )));
aTable2.addCell(new Cell(new Phrase(TIME, fontChinese2 )));
aTable2.addCell(new Cell(new Phrase(PICTURE, fontChinese2 )));
CLS_VO_Questions question = new CLS_VO_Questions();
String strType = new String();
String strPath = new String();
String strTime = new String();
for(int i=0;i<it;i++){
question = list.get(i);
strType = question.getQuestionType();
strPath = question.getPicturePath();
strTime = df.format(question.getQuestionTime());
Cell cel2 = new Cell(new Phrase(String.valueOf(i+1), fontChinese )); //序号
cel2.setVerticalAlignment(Element.ALIGN_CENTER);
aTable2.addCell(cel2);
Cell cel3 = new Cell(new Phrase(strType, fontChinese )); //违规类型
cel3.setVerticalAlignment(Element.ALIGN_CENTER);
aTable2.addCell(cel3);
Cell cel4 = new Cell(new Phrase(strTime, fontChinese )); //违规时间
cel4.setVerticalAlignment(Element.ALIGN_CENTER); //违规截图
aTable2.addCell(cel4);
Image img2=Image.getInstance(strPath);
img2.scalePercent(50);
img2.scalePercent(23, 25);
aTable2.addCell(new Cell(img2));
};
document.add(aTable2);
document.close();
}
/*
* 测试该工具类的方法
*/
public static void main(String[] args){
MakeWord b=new MakeWord();
CLS_VO_CheckQuestion checkq = new CLS_VO_CheckQuestion();
List<CLS_VO_Questions> list = new ArrayList<CLS_VO_Questions>();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
/*
* 先给第一张表格实体类传参数
*/
checkq.setArchiveName("(2015)