java word 导出怎么合并表头 Word poi
我用Wordpoi导出Java文本文件但是怎么做出合并单元格的效果网上找没有相关的资料求大神求助...
我用Word poi 导出Java文本文件 但是怎么做出合并单元格的效果 网上找没有相关的资料 求大神求助
展开
展开全部
给你个例子
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;
}
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |