java word 导出怎么合并表头 Word poi

我用Wordpoi导出Java文本文件但是怎么做出合并单元格的效果网上找没有相关的资料求大神求助... 我用Word poi 导出Java文本文件 但是怎么做出合并单元格的效果 网上找没有相关的资料 求大神求助 展开
 我来答
准脏面1
推荐于2016-11-29
知道答主
回答量:23
采纳率:0%
帮助的人:12.5万
展开全部
给你个例子

iText-2.1.5.jar、iTextAsian.jar、iText-rtf-2.1.4.jar
public String CreateWord() {

try {
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, new FileOutputStream("c:\\"));
document.open();

for (int i = 0; i < datalist.size(); i++) {
Lawmanager lawmanager = (Lawmanager) datalist.get(i);

Paragraph title = new Paragraph("行 政 执 法 人 员 登 记 表");
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(new RtfFont("仿 宋__GB2312", 18, Font.BOLD, Color.BLACK));
document.add(title);

Table table = new Table(7);
table.setWidth(100);
table.setBorderWidth(1);
table.setBorderColor(Color.BLACK);
table.setPadding(0);
table.setSpacing(0);
table.setAlignment("CENTER");
table.setWidths(new float[]{16.0f,16.0f,15.0f,15.0f,16.0f,22.0f,0.0f});
table.setAutoFillEmptyCells(true);

RtfFont font = new RtfFont("仿 宋__GB2312", 12, Font.COURIER, Color.BLACK);

Cell cell = new Cell(new Phrase("姓 名", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getLawname(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(2);
table.addCell(cell);
cell = new Cell(new Phrase("性 别", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getLawsex(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
try {
Image lawPic = Image.getInstance(publicTools.getPath() + "/" + lawmanager.getLawpic());
lawPic.scaleAbsolute(105, 135);
cell = new Cell(lawPic);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(4);
table.addCell(cell);
} catch (IOException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
Image lawPic = Image.getInstance(publicTools.getPath("upfile/pic") + "pic.jpg");
lawPic.scaleAbsolute(105, 135);
cell = new Cell(lawPic);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(4);
table.addCell(cell);
}
cell = new Cell("\n\n");
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(255, 255, 255));
table.addCell(cell);

cell = new Cell(new Phrase("出生年月日", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getLawbirthdate(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(2);
table.addCell(cell);
cell = new Cell(new Phrase("民 族", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getVolkdictionaryname(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell("\n\n");
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(255, 255, 255));
table.addCell(cell);

cell = new Cell(new Phrase("工作单位", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(" " + lawmanager.getDeptname(), font));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(4);
table.addCell(cell);
cell = new Cell("\n\n");
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(255, 255, 255));
table.addCell(cell);

cell = new Cell(new Phrase("职 务", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getPostdictionaryname(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase("参加工作时间", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getLawworkdate(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(2);
table.addCell(cell);
cell = new Cell("\n\n");
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(255, 255, 255));
table.addCell(cell);

document.add(table);

Phrase phrase = new Phrase("\n注:如未取得行政执法证件、行政执法资格的,在表中“行政执法证件编号”、“行政执法资格编号”栏中显示“0”。", new RtfFont("黑 体", 10, Font.TIMES_ROMAN, Color.red));
document.add(phrase);

if(i < datalist.size() - 1){
document.newPage();
}
}
document.close();
publicTools.insertLog("5", "下载Word文件", userSession);
} catch (FileNotFoundException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
e.printStackTrace();
} catch (DocumentException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
e.printStackTrace();
} catch (MalformedURLException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
e.printStackTrace();
} catch (IOException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
e.printStackTrace();
}
return "upfile/download/" + fileName;
}
缄默相依
2014-09-26 · TA获得超过224个赞
知道小有建树答主
回答量:338
采纳率:0%
帮助的人:226万
展开全部
如果是这类的有合并单元格的,建议还是提前定义好模板,导出的时候相当于直接往模板里填入数据就好了,当然模板太多的话就算了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式