apache poi导出Excel或Pdf文件
来源:互联网 发布:淘宝收到空包裹怎么办 编辑:程序博客网 时间:2024/04/29 16:02
package com.infrastructure.project.common.util;import java.awt.Color;import java.io.File;import java.io.FileOutputStream;import java.util.List;import com.infrastructure.project.common.SysConstant;import com.lowagie.text.Cell;import com.lowagie.text.Document;import com.lowagie.text.Element;import com.lowagie.text.Font;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.Phrase;import com.lowagie.text.Rectangle;import com.lowagie.text.Table;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.pdf.PdfWriter;public class ExportPdfUtil {public static void exportPdf(String fileName,String filePath,List listLable ,List listData) {Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);try {File file = new File(filePath);BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);Font FontChina = new Font(bfChinese, 24, Font.NORMAL);if (!file.exists()) {file.mkdirs();}PdfWriter.getInstance(document, new FileOutputStream(filePath+fileName+SysConstant.nameSuffix.NAMESUFFIX_PDF));document.open();Table datatable = new Table(listLable.size());datatable.setWidth(100);datatable.setPadding(3);Cell cell = new Cell(new Phrase(fileName,FontChina));cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setLeading(30);cell.setColspan(listLable.size());cell.setBorder(Rectangle.NO_BORDER);cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));datatable.addCell(cell);datatable.setBorderWidth(2);datatable.setAlignment(1);for(int i =0;i<listLable.size();i++){datatable.addCell(new Paragraph(listLable.get(i).toString(),FontChinese));}datatable.endHeaders();datatable.setBorderWidth(1);for (int j = 0; j < listData.size(); j++) {List a = (List) listData.get(j);for(int o = 0;o<a.size();o++){datatable.addCell(new Paragraph(a.get(o) == null ?"":a.get(o).toString(),FontChinese));}}document.add(datatable);} catch (Exception e) {e.printStackTrace();}document.close();}}
package poi.itext;
import java.io.FileOutputStream;import java.io.IOException;import java.awt.Color;
import com.lowagie.text.*;import com.lowagie.text.pdf.*;import com.lowagie.text.pdf.BaseFont;
/** * 创建Pdf文档 * @author Administrator * */
public class HelloPdf{ public static void main(String[] args)throws Exception { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
// 第一步,创建document对象 Rectangle rectPageSize = new Rectangle(PageSize.A4); //下面代码设置页面横置 //rectPageSize = rectPageSize.rotate(); //创建document对象并指定边距 Document doc = new Document(rectPageSize,50,50,50,50); Document document = new Document(); try { // 第二步,将Document实例和文件输出流用PdfWriter类绑定在一起 //从而完成向Document写,即写入PDF文档 PdfWriter.getInstance(document,new FileOutputStream("src/poi/itext/HelloWorld.pdf")); //第3步,打开文档 document.open(); //第3步,向文档添加文字. 文档由段组成 document.add(new Paragraph("Hello World"));
Paragraph par = new Paragraph("世界你好",FontChinese); document.add(par);
PdfPTable table = new PdfPTable(3); for(int i=0;i<12;i++) { if (i == 0) { PdfPCell cell = new PdfPCell(); cell.setColspan(3); cell.setBackgroundColor(new Color(180,180,180)); cell.addElement(new Paragraph("表格头" , FontChinese)); table.addCell(cell); } else { PdfPCell cell = new PdfPCell(); cell.addElement(new Paragraph("表格内容" , FontChinese)); table.addCell(cell); } } document.add(table);
} catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } //关闭document document.close(); System.out.println("生成HelloPdf成功!"); } }
- apache poi导出Excel或Pdf文件
- Apache POI导出Excel文件
- Apache POI实现Excel文件导出
- 关于apache POI excel文件导入导出
- apache poi导出excel
- Apache POI导出Excel
- POI导出Excel文件
- POI EXCEL 文件导出
- POI 导出excel文件
- POI导出Excel文件
- POI导出Excel文件
- POI导出Excel文件
- Apache POI导出Excel工具
- Apache POI实现导出EXcel
- Apache POI 导出Excel笔记
- Apache POI实现Excel文件上传、导出,工具类分享
- 用rdlc文件直接导出到excel或PDF
- java通过poi导出excel和pdf
- OC面向对象—继承 & 组合
- 从运维角度浅谈 MySQL 数据库优化
- 第1章第1节练习题6 删除重复值
- eclipse 安装svn插件
- [HNOI2004]宠物收养所 (Splay)
- apache poi导出Excel或Pdf文件
- ROW_NUMBER,自治事务--工作备忘2016/1/7
- mmap实现机制
- win10 安装程序显示“为了对电脑进行保护,已经阻止此应用” 管理员已阻止你运行此应用
- 基于C#的socket编程的TCP异步实现
- ImageView的ScaleType属性
- 常用设计模式——模版
- Spring声明式事务管理与配置介绍
- 何时用Delegate和何时用Block?