package com...util;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
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;
/**
* 根据itext提供的java类库,构建word模板,并添加相应的内容,从而导出word报告;平台不相关
*/
public class WordDocCreater {
private Document document;
private BaseFont bfChinese;
public BaseFont getBfChinese() {
return bfChinese;
}
public void setBfChinese(BaseFont bfChinese) {
this.bfChinese = bfChinese;
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public WordDocCreater() {
this.document = new Document(PageSize.A4);
}
/**
* @param filePath
* 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
* @throws DocumentException
* @throws IOException
*/
public void openDocument(String filePath) throws DocumentException,
IOException {
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(this.document, new FileOutputStream(filePath));
this.document.open();
// 设置中文字体
this.bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
}
/**
* @param titleStr
* 标题
* @param fontsize
* 字体大小
* @param fontStyle
* 字体样式
* @param elementAlign
* 对齐方式
* @throws DocumentException
*/
public void insertTitle(String titleStr, int fontsize, int fontStyle,
int elementAlign) throws DocumentException {
Font titleFont = new Font(this.bfChinese, fontsize, fontStyle);
Paragraph title = new Paragraph(titleStr);
// 设置标题格式对齐方式
title.setAlignment(elementAlign);
title.setFont(titleFont);
this.document.add(title);
}
/**
* @param contextStr
* 内容
* @param fontsize
* 字体大小
* @param fontStyle
* 字体样式
* @param elementAlign
* 对齐方式
* @throws DocumentException
*/
public void insertContext(String contextStr, int fontsize, int fontStyle,
int elementAlign) throws DocumentException {
// 正文字体风格
Font contextFont = new Font(bfChinese, fontsize, fontStyle);
Paragraph context = new Paragraph(contextStr);
// 设置行距
context.setLeading(30f);
// 正文格式左对齐
context.setAlignment(elementAlign);
context.setFont(contextFont);
// 离上一段落(标题)空的行数
context.setSpacingBefore(5);
// 设置第一行空的列数
context.setFirstLineIndent(20);
document.add(context);
}
/*
* 测试清单
*/
public void insertRiskTable() throws DocumentException {
Table aTable = new Table(6, 3);
int width[] = { 10, 40, 17, 13, 10, 10 };
aTable.setWidths(width);// 设置每列所占比例
aTable.setWidth(100); // 占页面宽度 90%
aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
aTable.setAutoFillEmptyCells(true); // 自动填满
aTable.setBorderWidth(0); // 边框宽度
aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色
aTable.setPadding(2);// 衬距,看效果就知道什么意思了
aTable.setSpacing(3);// 即单元格之间的间距
aTable.setBorder(2);// 边框
Font fontChinese = new Font(bfChinese, 10, Font.BOLD);
Cell cell = new Cell(new Phrase("\n测试代码\n", fontChinese));
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorderColor(new Color(0, 0, 0));
cell.setBackgroundColor(new Color(153, 204, 255));
aTable.addCell(cell);
Cell cell1 = new Cell(new Phrase("测试名称", fontChinese));
cell1.setVerticalAlignment(Element.ALIGN_CENTER);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setBorderColor(new Color(0, 0, 0));
cell1.setBackgroundColor(new Color(153, 204, 255));
aTable.addCell(cell1);
Cell cell2 = new Cell(new Phrase("测试发生可能性", fontChinese));
cell2.setVerticalAlignment(Element.ALIGN_CENTER);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setBorderColor(new Color(0, 0, 0));
cell2.setBackgroundColor(new Color(255, 255, 0));
aTable.addCell(cell2);
Cell cell3 = new Cell(new Phrase("测试损失度", fontChinese));
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setBorderColor(new Color(0, 0, 0));
cell3.setBackgroundColor(new Color(255, 255, 0));
aTable.addCell(cell3);
Cell cell4 = new Cell(new Phrase("测试水平", fontChinese));
cell4.setVerticalAlignment(Element.ALIGN_CENTER);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setBorderColor(new Color(0, 0, 0));
cell4.setBackgroundColor(new Color(255, 255, 0));
aTable.addCell(cell4);
Cell cell5 = new Cell(new Phrase("测试等级", fontChinese));
cell5.setVerticalAlignment(Element.ALIGN_CENTER);
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setBorderColor(new Color(0, 0, 0));
cell5.setBackgroundColor(new Color(255, 255, 0));
aTable.addCell(cell5);
for (int i = 0; i < 12; i++) {
aTable.addCell(new Cell(i + ""));
}
document.add(aTable);
document.add(new Paragraph("\n"));
}
/*
* 现状评估
*/
public void insertRiskEvaluationTable() throws DocumentException {
Table aTable = new Table(12, 4);
int width1[] = { 5, 20, 5, 20, 5, 5, 5, 5, 5, 5, 5, 5 };
aTable.setWidths(width1);// 设置每列所占比例
aTable.setWidth(100); // 占页面宽度 90%
aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
aTable.setAutoFillEmptyCells(true); // 自动填满
aTable.setBorderWidth(0); // 边框宽度
aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色
Font fontChinese = new Font(bfChinese, 10, Font.BOLD);
Cell cell = new Cell(new Phrase("\n测试代码\n", fontChinese));
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(2);
cell.setBorderColor(new Color(0, 0, 0));
cell.setBackgroundColor(new Color(153, 204, 255));
aTable.addCell(cell);
Cell cell2 = new Cell(new Phrase("测试名称", fontChinese));
cell2.setVerticalAlignment(Element.ALIGN_CENTER);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setRowspan(2);
cell2.setBorderColor(new Color(0, 0, 0));
cell2.setBackgroundColor(new Color(153, 204, 255));
aTable.addCell(cell2);
Cell cell3 = new Cell(new Phrase("行为代码", fontChinese));
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setRowspan(2);
cell3.setBorderColor(new Color(0, 0, 0));
cell3.setBackgroundColor(new Color(153, 204, 255));
aTable.addCell(cell3);
Cell cell4 = new Cell(new Phrase("引发测试的行为", fontChinese));
cell4.setVerticalAlignment(Element.ALIGN_CENTER);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setRowspan(2);
cell4.setBorderColor(new Color(0, 0, 0));
cell4.setBackgroundColor(new Color(153, 204, 255));
aTable.addCell(cell4);
Ce