一:需求如下
生成一批word文档(分不同类型),每个word插入一些文字和一个唯一的二维码;比如100个人会生成100个单独的word,
目前客户需求修改为同一类(ABCDE五类)只生成一个word文档
导出word功能原代码如下(重构前):
/**
* 创建word文档,将二维码图片插入
* @param contextString
*/
public static String exportDoc(String realPath, Integer index, String typeLable, String contextString)
throws DocumentException, IOException {
// 设置纸张大小
Document document = new Document(PageSize.A4);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 创建文件路径分二维码类型ABDCE类
File file = new File(realPath + File.separator + typeLable + "类" + File.separator);
if (!file.exists()) {
file.mkdir();
}
// 创建word文件
File fileWrod = new File(realPath + File.separator + typeLable + "类" + File.separator + "中交考评" + typeLable + "类"
+ (index + 1) + ".doc");
RtfWriter2.getInstance(document, new FileOutputStream(fileWrod));
document.open();
// 设置中文字体
BaseFont bfChinese = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
// 标题字体风格
Font titleFont = new Font(bfChinese, 36, Font.BOLD);
// // 正文字体风格
Font contextFont = new Font(bfChinese, 17, Font.NORMAL);
Paragraph title = new Paragraph("绩效考评" + typeLable + "类");
// 设置标题格式对齐方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
//设置内容
Paragraph context = new Paragraph(contextString);
String contextString1 = "开始你的测评";
Paragraph context1 = new Paragraph(contextString1);
context1.setAlignment(Element.ALIGN_CENTER);
context1.setFont(contextFont);
// 正文格式左对齐
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
// 离上一段落(标题)空的行数
context.setSpacingBefore(10);
// 设置第一行空的列数
context.setFirstLineIndent(50);
document.add(context);
document.add(context1);
// // 利用类FontFactory结合Font和Color可以设置各种各样字体样式
// Paragraph underline = new Paragraph("下划线的实现", FontFactory.getFont(
// FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE,
// new Color(0, 0, 255)));
// document.add(underline);
// // 添加图片 Image.getInstance即可以放路径又可以放二进制字节流
Image img = Image.getInstance(realPath + "考评二维码_" + typeLable + index + ".png");
img.setAbsolutePosition(0, 0);
img.setAlignment(Image.ALIGN_CENTER);// 设置图片显示位置
img.scaleAbsolute(140, 140);// 直接设定显示尺寸
// // img.scalePercent(50);//表示显示的大小为原尺寸的50%
// // img.scalePercent(25, 12);//图像高宽的显示比例
// // img.setRotation(30);//图像旋转一定角度
document.add(img);
document.close();
// 得到输入流
// wordFile = new ByteArrayInputStream(baos.toByteArray());
// baos.close();
return "";
}
二:函数问题
1.传入参数命名不明确
2.注释的代码太多
3.生成word过程不明显
三:重构
将word中各种不同类型的内容进行封装,标题createTitle,主体内容createMainContext,测试内容createTestContext,图片createImage,控制分页createNextPageContext
/**
* 创建word文档,将二维码图片插入
*
* @param contextString
* @param typeValue
*/
public static String exportDoc(String realPath, String wordType, String contextStr, Integer wordTypeCnt)
throws DocumentException, IOException {
// 创建文件路径分二维码类型ABDCE类
File file = new File(realPath + File.separator + wordType + "类" + File.separator);
if (!file.exists()) {
file.mkdir();
}
// 创建word文件
File fileWrod = new File(realPath + File.separator + wordType + "类" + File.separator + "XX考评" + wordType + "类"
+ ".doc");
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, new FileOutputStream(fileWrod));
document.open();
BaseFont bfChinese = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bfChinese, 36, Font.BOLD);
Font contextFont = new Font(bfChinese, 17, Font.NORMAL);
Paragraph mainContext = createMainContext(contextStr, contextFont);
Paragraph startTestContext = createTestContext(contextFont);
Paragraph nextPageContext = createNextPageContext();
for (int j = 0; j < wordTypeCnt; j++) {
Paragraph title = createTitle(wordType, titleFont,j);
document.add(title);
document.add(mainContext);
document.add(startTestContext);
Image img = createImage(realPath, wordType, j);
document.add(img);
document.add(nextPageContext);
}
document.close();
return "";
}
private static Paragraph createNextPageContext() {
Paragraph title = new Paragraph("\n\n\n");
return title;
}
private static Paragraph createTitle(String typeLable, Font titleFont,int index) {
Paragraph title = new Paragraph("xx考评" + typeLable + "类"+(index+1));
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
return title;
}
private static Paragraph createTestContext(Font contextFont) {
Paragraph startTestContext = new Paragraph("开始你的测评");
startTestContext.setAlignment(Element.ALIGN_CENTER);
startTestContext.setFont(contextFont);
return startTestContext;
}
private static Paragraph createMainContext(String contextString, Font contextFont) {
Paragraph context = new Paragraph(contextString);
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
context.setSpacingBefore(10);
context.setFirstLineIndent(50);
return context;
}
private static Image createImage(String realPath, String typeLable, Integer index)
throws BadElementException, MalformedURLException, IOException {
Image img = Image.getInstance(realPath + "考评二维码_" + typeLable + index + ".png");
img.setAbsolutePosition(0, 0);
img.setAlignment(Image.ALIGN_CENTER);// 设置图片显示位置
img.scaleAbsolute(140, 140);// 直接设定显示尺寸
// // img.scalePercent(50);//表示显示的大小为原尺寸的50%
// // img.scalePercent(25, 12);//图像高宽的显示比例
// // img.setRotation(30);//图像旋转一定角度
return img;
}