快捷搜索:

自己封装的poi,itext报表导出代码

页面代码:

对不起,没有你要查询的记录!!!

户人编号

户编号

姓名

性别

现栖身地

户籍所在地

公夷易近身份证号码

出生日期

夷易近族

文化程度

户口性子

婚姻状况

导出PDF |

导出Excel

共${page.rowCounts}条

当前${page.currPageNum}/${page.pageCount}页

首页

首页

1}">

上一页

上一页

下一页

=page.pageCount}">下一页

未页

未页

转到

${varName}

${varName}

/exportServlet" method="post" id="report_form">

后台代码:

package com.future.util;

import java.awt.Color;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.lowagie.text.Cell;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Element;

import com.lowagie.text.Font;

import com.lowagie.text.HeaderFooter;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Phrase;

import com.lowagie.text.Table;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.PdfWriter;

/** *//**

* 功能描述:此servlet用于将当前页面表格内容导出到pdf和excel

* 参数阐明:

*String type 为导出类型

*String title 报表中title

*String tableHeader 页面中表格的表头

*String tableBody 表格内容

* 评释:此中页面数据因此特殊字符分隔通报过来的

*逗号(,)用来连接表头,后台用逗号来解析页面通报过来的表头

*@@用来分隔一条数据,后台用@@来解析每条数据

*##用来分隔单元格,后台用##来解析每条数据对应的单元格

* 创建光阴:2010-07-01

* @author sxyx2008

* @version 1.0.0

*/

@SuppressWarnings("serial")

public class ExportServlet extends HttpServlet {

@Override

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding("utf-8");

response.reset();

//导出类型

String type=request.getParameter("type");

//报表标题

String title=request.getParameter("title");

//表头

String tableHeader=request.getParameter("tableHeader");

//表格内容

String tableBody=request.getParameter("tableBody");

//文件名

String fileName="";

if("pdf".equals(type)){

try {

exportToPdf(title,tableHeader,tableBody,response);

} catch (Exception e) {

e.printStackTrace();

}

}else if("excel".equals(type)){

fileName = new String(new String((title + ".xls").getBytes(),"ISO-8859-1"));

response.setContentType("application/vnd.ms-excel;charset=utf-8");

response.setHeader("Content-Disposition", "attachment;filename="+ fileName);

InputStream inputStream=exportToExcel(title, tableHeader,tableBody);

ServletOutputStream out = response.getOutputStream();

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try {

bis = new BufferedInputStream(inputStream);

bos = new BufferedOutputStream(out);

byte[] buff = new byte[2048];

int bytesRead;

while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

bos.write(buff, 0, bytesRead);

}

} catch (final IOException e) {

throw e;

} finally {

if (bis != null)

bis.close();

if (bos != null)

bos.close();

}

}

}

/** *//**

* 导出到excel表格

*/

private InputStream exportToExcel(String title,String tableHeader,String tableBody) {

// 创建事情表

HSSFWorkbook wb = new HSSFWorkbook();

// 创建第一个事情区

HSSFSheet sheet = wb.createSheet(title);

// 创建标题行

HSSFRow row = sheet.createRow(0);

// 获取表头

String[] header = tableHeader.split(",");

// 分隔每一行记录

String[] content = tableBody.split("@@");

// 创建表头

HSSFCell cell = null;

for (int i = 0; i < header.length; i++) {

cell = row.createCell(i);

cell.setCellValue(header[i]);

}

//轮回每笔记录

for (int i = 0; i < content.length; i++) {

row = sheet.createRow(i + 1);

//掏出每个单元格的值

String[] value=content[i].split("##");

for (int j = 0; j < value.length; j++) {

cell = row.createCell(j);

cell.setCellValue(value[j]);

}

}

ByteArrayOutputStream os = new ByteArrayOutputStream();

try {

wb.write(os);

} catch (IOException e) {

e.printStackTrace();

}

byte[] contents = os.toByteArray();

InputStream is = new ByteArrayInputStream(contents);

return is;

}

/** *//**

* 导出到pdf

*

*/

private void exportToPdf(String title,String tableHeader,String tableBody,HttpServletResponse response) throws Exception{

Document doc=new Document(PageSize.A4,50,50,50,50);

ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();

PdfWriter writer=PdfWriter.getInstance(doc,byteArrayOutputStream);

doc.addCreationDate();

HeaderFooter footer=new HeaderFooter(new Phrase(), true);

//设置页脚是否有边框

//0表示无

//1上边框

//2下边框

//3高低边框都有 默认都有

footer.setBorder(0);

//footer.setBorder(1);

//footer.setBorder(2);

//footer.setBorder(3);

footer.setAlignment(Element.ALIGN_CENTER);

doc.setFooter(footer);

doc.open();

// 获取表头

String[] header = tableHeader.split(",");

// 分隔每一行记录

String[] content = tableBody.split("@@");

//根据通报进来的表头创建pdf的表头

Paragraph paragraph=new Paragraph(title,ChineseFont());

paragraph.setAlignment(Element.ALIGN_CENTER);

doc.add(paragraph);

Table table=new Table(header.length);

table.setWidth(100);

table.setBorder(1);

for (int i = 0; i < header.length; i++) {

Cell cell=new Cell(new Phrase(header[i],ChineseFont()));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_CENTER);

cell.setHeader(true);

cell.setBackgroundColor(new Color(231,231,231));

table.addCell(cell);

}

table.endHeaders();

//轮回每笔记录

for (int i = 0; i < content.length; i++) {

Cell cell=null;

//掏出每个单元格的值

String[] value=content[i].split("##");

for (int j = 0; j < value.length; j++) {

cell = new Cell(new Phrase(value[j],ChineseFont()));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

}

}

doc.add(table);

doc.close();

writer.close();

String fileName = new String(new String((title + ".pdf").getBytes(),"ISO-8859-1"));

response.setContentType("application/pdf;charset=utf-8");

response.setHeader("Content-Disposition", "attachment;filename="+ fileName);

ServletOutputStream outputStream=response.getOutputStream();

byteArrayOutputStream.writeTo(outputStream);

outputStream.flush();

outputStream.close();

}

/** *//**

* pdf中翰墨体设置

* @return

*/

public static Font ChineseFont()

{

BaseFont baseFont=null;

try {

baseFont=BaseFont.createFont("STSong-Light","UniGB-UCS2-H", true);

} catch (DocumentException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

Font chineseFont=new Font(baseFont,8,Font.BOLD,Color.BLACK);

return chineseFont;

}

}

您可能还会对下面的文章感兴趣: