1. 程式人生 > >關於java中用itext導出word的一點想法

關於java中用itext導出word的一點想法

車輛 ++ 沒有 map 自己 前臺 i++ 出錯 https

這幾天在項目組只做了很少的事情,主要還是自己不認真地說.我的部分是要負責用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();
}
}
}

}

技術分享

關於java中用itext導出word的一點想法