itext导出Word的问题,能解答的有高分
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了itext导出Word的问题,能解答的有高分相关的知识,希望对你有一定的参考价值。
求助itext如何设置cell的边框宽度,setBorderWidthTop(2)设置无效使用的是table,不是pdfptable,能解答的有高分。
参考技术A 1: 可以设置字体带下划线,也就是 new Phrase里面带上设置字体下划线2: 用表格实现,把表格的下边框线显示出来
3: 直接画线也可以package test;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.swing.JOptionPane;
/**
*
* @author julycn
*/
public class RTFCreate
/**
* Creates a new instance of RTFCreate
*/
public RTFCreate()
public static void main(String[] args)
RTFCreate rtfCreate=new RTFCreate();
try
rtfCreate.createRTF();
JOptionPane.showMessageDialog(null,"表格已经成功创建");
catch (MalformedURLException ex)
JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");
ex.printStackTrace();
catch (FileNotFoundException ex)
JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");
ex.printStackTrace();
catch (IOException ex)
JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");
ex.printStackTrace();
catch (DocumentException ex)
JOptionPane.showMessageDialog(null,"表格导出出错,错误信息:"+ex+"\n错误原因可能是表格已经打开!");
ex.printStackTrace();
public void createRTF() throws FileNotFoundException, DocumentException, MalformedURLException, IOException
//创建word文档
Document document=new Document(PageSize.A4);
//输入word文档
RtfWriter2.getInstance(document,new FileOutputStream("d:\\word.rtf"));
document.open();
//中文字体
BaseFont bfChinese=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font fontChinese=new Font(bfChinese,12,Font.HELVETICA);
//创建有3列的表格
Table table=new Table(3);
document.add(new Paragraph("生成rft文档!",fontChinese));
table.setBorderWidth(1);
table.setBorderColor(new Color(0,0,255));
table.setPadding(5);
table.setSpacing(5);
//添加表头元素
Cell cell=new Cell("header");
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
table.endHeaders();//表头结束
//表格主体
cell=new Cell("Example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new Color(255,0,0));
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell(new Paragraph("测试1",fontChinese));
table.addCell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
table.addCell(new Paragraph("测试2",fontChinese));
document.add(table);
//在表格末尾添加图片
Image png=Image.getInstance("d:\\duck.jpg");
document.add(png);
document.close();
看看这个有没有帮助.
关于java中用itext导出word的一点想法
这几天在项目组只做了很少的事情,主要还是自己不认真地说.我的部分是要负责用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();
}
}
}
}
以上是关于itext导出Word的问题,能解答的有高分的主要内容,如果未能解决你的问题,请参考以下文章
.net winfrom程序怎么把一个页面导出存为word文档