这几天在项目组只做了很少的事情,主要还是自己不认真地说.我的部分是要负责用itext导出word文档这一块,之前看到大佬们做出了EXCEL部分觉得很是惊奇,就像刚刚接触HTML一样的感觉。但是毕竟自己的任务就要好好的去完成,所以经过努力还是取得了一点的成果.的。
先上一个效果图
接下来就是代码了,码字不易,还有事情要做~~
从前台利用Ajax传入到后台部分后,利用itext方法导出word,需要注意的是document.close和out.close这两个部分,没有close很容易出错。
public void exportJdyWord(HttpServletResponse response,HttpServletRequest request,String xsId) throws UnsupportedEncodingException {
for(int c=1;c<=2;c++){
String s = " select c_username from c_kdxsyb where c_xs_id = ? and c_xs_rq = ‘"+c+"‘";
List<Map<String,Object>> username = jt.queryForList(s,xsId);
Set<String> set = new HashSet<String>();
for(int i=0;i<username.size();i++){
Map <String, Object> map = username.get(i);
Object vala = map.get(username);
if(vala!=null){String v = vala.toString();
set.add(v);}
}
response.setContentType("application/msword");//下载
String agent = request.getHeader("USER-AGENT").toLowerCase();
String codedFileName = java.net.URLEncoder.encode("监督员表", "UTF-8");
if (agent.contains("firefox")) {
response.setCharacterEncoding("utf-8");
response.setHeader("content-disposition",
"attachment;filename=" + new String("监督员".getBytes(), "ISO8859-1") + ".doc");
} else {
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".doc");
}
try{
Document document = new Document(PageSize.A4);
OutputStream out = response.getOutputStream();
RtfWriter2.getInstance(document, out);
document.open();//打开文件
BaseFont bfChinese = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
Font titleFont = new Font(bfChinese, 12, Font.BOLD);//标题的字体风格
Font contextFont = new Font(bfChinese, 10, Font.NORMAL);//正文的字体风格
Table table = new Table(7,(username.size()+4));
int [] withs = {1,2,1,1,3,4,2};//设置每个表头的宽度,以下是设计表格
table.setWidths(withs);
table.setWidth(100);
table.setAlignment(Element.ALIGN_CENTER);
table.setAutoFillEmptyCells(true);
String titleString = "2017年11月"+(c+6)+"日督员名单";
Paragraph title = new Paragraph(titleString);
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);//添加标题
String contextString = "考点院校名:东北师范大学人文学院东区1号楼B区 考场数:29个 巡考人员数:12名 考点联系人:李慧玲 联系电话:13844057575 考点地址:长春市净月区博硕路1488号 值班电话:0431-84537193";
Paragraph context = new Paragraph(contextString);
context.setAlignment(Element.ALIGN_CENTER);
context.setFont(contextFont);
context.setSpacingBefore(10);//设置与上一段落的间距
context.setFirstLineIndent(7);
document.add(context);
//表格部分
Cell[] cellHeaders = new Cell[7];//创建表头
cellHeaders[0] = new Cell(new Phrase("序号", contextFont));
cellHeaders[1] = new Cell(new Phrase("姓名",contextFont));
cellHeaders[2] = new Cell(new Phrase("级别",contextFont));
cellHeaders[3] = new Cell(new Phrase("性别",contextFont));
cellHeaders[4] = new Cell(new Phrase("电话",contextFont));
cellHeaders[5] = new Cell(new Phrase("工作单位",contextFont));
cellHeaders[6] = new Cell(new Phrase("分管考场",contextFont));
for (int i1 = 0; i1 < 7; i1++) {
cellHeaders[i1].setHorizontalAlignment(Element.ALIGN_CENTER);
cellHeaders[i1].setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellHeaders[i1]);
}
//向表格填充数据
String contextString1="领导审批: 审核: 制表人:魏建强 备注:扣税每月同工资一并计算扣缴。";
Paragraph context1 = new Paragraph(contextString1);
context.setAlignment(Element.ALIGN_CENTER);
context.setFont(contextFont);
context.setSpacingBefore(10);
for(int i=0;i<username.size();i++){
String name = username.get(i).toString();
name = name.substring(12);
name = name.substring(0,name.length()-1);
for(int lie=1;lie<8;lie++){
if(lie==2)
{
Cell celladd = new Cell(new Phrase(name,contextFont));
table.addCell(celladd);
}
else
{
Cell cell_null = new Cell(new Phrase("",contextFont));
table.addCell(cell_null);}
}
}
String s1 = " select from c_kdxsyb";
Cell cell1 = new Cell(new Phrase("乘坐车辆:1号大客车", contextFont));//添加表格的跨列部分
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setColspan(4);
table.addCell(cell1);
Cell cell2 = new Cell(new Phrase(" ", contextFont));//添加表格的跨列部分
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setColspan(3);
table.addCell(cell2);
Cell cell3 = new Cell(new Phrase(" 无委会",contextFont));
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setColspan(4);
table.addCell(cell3);
Cell cell4 = new Cell(new Phrase("组长:赵 琦18686420022",contextFont));
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setColspan(3);
table.addCell(cell4);
Cell cell5 = new Cell(new Phrase("安保人员",contextFont));
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setColspan(4);
cell5.setRowspan(2);
table.addCell(cell5);
Cell cell6 = new Cell(new Phrase("李卫东 15904405903",contextFont));
cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setColspan(3);
table.addCell(cell6);
Cell cell7 = new Cell(new Phrase("孙中富 15904407150",contextFont));
cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell7.setColspan(3);
table.addCell(cell7);
document.add(table);
document.add(context1);
document.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}