用com.lowagie.text (2.1.7)生成DOC ,PDF文档(加入iTextAsian.jar,如果考虑中文情况),用jxl.jar生成excel

考虑到软件协议(AGPL,GPL)问题,避免可能产生的纠纷,建议itext的相关jar包(如:com.lowagie.text等)用2.1.7及以前的。

----------------------

首先导入需要的jar包(如:com.lowagie.text_2.1.7.v201004222200.jar,iTextAsian.jar,jxl.jar(Excel相关)),并在Eclipse下设置依赖。

----

生成DOC:
   Document document = new Document(PageSize.A4);
   RtfWriter2.getInstance(document, new FileOutputStream(filePath));
   document.open();

   setDataToDocument(document ,filePath);


生成PDF:
   Document document = new Document(PageSize.A4, 50, 50, 50, 50);
   PdfWriter.getInstance(document, new FileOutputStream(filePath));
   document.open();

setDataToDocument(document ,filePath);

---
//下面只是对API中的部分类做了事例性说明,如果想得到其Document更详细的操作,去官网查阅相关的API文档

setDataToDocument(xx,xx){//其实DOC,PDF 相似

// 章节头
  Font font_19 = new Font(Font.BOLD, 19, Font.BOLD);
  Paragraph titleParagraph = new Paragraph("Chip  Configuration ", font_19);//段落
  Chapter docChapter = new Chapter(titleParagraph, 1);
  docChapter.setNumberDepth(0);
  // 小节一
  Font font_16 = new Font(Font.BOLD, 16, Font.BOLD);
  Paragraph titleParagraph = new Paragraph("xxxx", font_16);
  Section section = docChapter.addSection(titleParagraph);
  Paragraph contentP = new Paragraph("xxx");
  section.add(contentP );

  section.add(xxx);
  // 小节二
  Paragraph descParagraph = new Paragraph("xxxx", font_16);
  Section descSection = docChapter.addSection(descParagraph);
  descSection.add(contentP);

  Table table = xxxx;

  descSection.add(table);

  document.add(section);// 添加两个小节
  document.add(descSection);

  document.close();

}
---------

生成:Excel (用jxl.jar的情况)

其中部分关键API操作类及方法:
  WritableWorkbook book = Workbook.createWorkbook(new File(filePath));//给一个文件路径,创建一个Excel文件
  WritableSheet sheet = book.createSheet("xxxxx", 0);//设置第一个sheet页

 //  设置冻结单元格   
   sheet.getSettings().setVerticalFreeze(1);   
   sheet.getSettings().setHorizontalFreeze(size+1); 

   //设置列/行宽

  sheet.setColumnView(columnIndex, 100);

  sheet.setRowView(rowIndex, value);

 //设置字体
  WritableFont font10bold = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
  WritableCellFormat format= new WritableCellFormat(font10bold);//设置单元格格式
  format.setAlignment(jxl.format.Alignment.CENTRE);//水平居中
  format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直居中
  format.setBorder(jxl.format.Border.BOTTOM, jxl.format.BorderLineStyle.THICK, Colour.BLACK);//设置border样式
 //添加单元格
  Label label = new Label(0, 0, "xxx", format);//添加单元格,参数依次为:列,行,值,格式
  sheet.addCell(label);
  //合并单元格
  sheet.mergeCells(startColumn, startRow,endColumn,endRow);//合并单元格,参数列表依次为:合并的开始行、列、合并的终点行、列  

  // -----------------------
  
  // 写入数据并关闭文件
  book.write();
  book.close();
----------------------------------
=================================================================
关于本文下面的附件,这里说明一下:
1.com.lowagie.txt.2.1.7 ----itext的核心jar包
2.iText-2.1.7.jar       ----itext的完整的jar包
3.iTextAsian.jar        ----itext的语言jar包(支持中文)
4.itext-rtf-2.1.7.jar   ----itext的Word文档的扩展jar包

5.jd-gui.exe是一款绿色的非常好用的反编译软件,也随便放到这里了,下载后去掉.jar后缀就可以用了。



猜你喜欢

转载自niub.iteye.com/blog/1787607