jsp 页面下的 PdfPTable 生成pdf报表
<%@ page contentType="charset=utf-8" language="java" import="java.io.*,java.awt.Color,java.awt.Color,com.itextpdf.text.*,com.itextpdf.text.pdf.*"%>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%@ include file="../datebase.jsp"%>
<%
String maxtemp="";
String Picure="";
String avgtemp="";
String presetname="";
String objectname="";
String time="";
String strsql="SELECT alarm.maxtemp,alarm.avgtemp,alarm.picture,alarm.time,presetname.objectname,presetname.presetname FROM alarm INNER JOIN presetname ON alarm.preset = presetname.id where alarm.type=14 and alarm.id=1";
Statement state = connection.createStatement();
ResultSet result = state.executeQuery(strsql);
ResultSetMetaData rme = result.getMetaData();
int num=rme.getColumnCount();
while(result.next())
{
maxtemp=result.getString(1);
avgtemp=result.getString(2);
Picure=result.getString(3);
time=result.getString(4);
presetname=result.getString(5);
objectname=result.getString(6);
}
result.close();
state.close();
connection.close();
Document doc = new Document();
try {
// 使用ByteArrayOutputStream 代替 FileOutputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfWriter.getInstance(doc, outputStream);
// 打开文档对象
doc.open();
Image wmf = Image.getInstance("../../../logo.png");
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font FontBig = new Font(bfChinese, 14, Font.NORMAL);
//表头
PdfPTable table2 = new PdfPTable(2);
PdfPCell cell=new PdfPCell(wmf);
cell.setBorder(0);
table2.addCell(cell);
cell=new PdfPCell(new Paragraph("检测报告",FontBig));
cell.setBorder(0);
float [] width={0.1f,0.1f};
width[0]=40f;
width[1]=10f;
table2.setWidths(width);
table2.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table2.addCell(cell);
doc.add(table2);
//时间
PdfPTable table = new PdfPTable(2);
Font FontBig2 = new Font(bfChinese, 12, Font.NORMAL);
cell=new PdfPCell(new Paragraph("报告日期: "+time,FontBig2));
cell.setBorder(0);
cell.setColspan(2);
table.addCell(cell);
//可见光图片和红外图片
//Image hw = Picure1;
//Image kj =Picure2;
Image hw = Image.getInstance("../../../BRT20120530155830.jpg");
Image kj = Image.getInstance("../../../BRT20120530155830.jpg");
table.addCell(hw);
table.addCell(kj);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
cell=new PdfPCell(new Paragraph("红外图片",FontChinese));
cell.setBorderColor(new BaseColor(102,102,102));
cell.setBackgroundColor(new BaseColor(102,102,102));
cell.setBorder(50);
table.addCell(cell);
cell=new PdfPCell(new Paragraph("可见光图片",FontChinese));
cell.setBorderColor(new BaseColor(102,102,102));
cell.setBackgroundColor(new BaseColor(102,102,102));
cell.setBorder(50);
table.addCell(cell);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
doc.add(table);
// 加入文字“Hello World”
table.addCell(cell);
PdfPTable table1 = new PdfPTable(2);
cell=new PdfPCell(new Paragraph("测温对象名称:",FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(255,255,255));
cell.setBackgroundColor(new BaseColor(255,255,255));
table1.addCell(cell);
cell=new PdfPCell(new Paragraph(presetname,FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(255,255,255));
cell.setBackgroundColor(new BaseColor(255,255,255));
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("图像日期:",FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(204,204,204));
cell.setBackgroundColor(new BaseColor(204,204,204));
table1.addCell(cell);
cell=new PdfPCell(new Paragraph(time,FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(204,204,204));
cell.setBackgroundColor(new BaseColor(204,204,204));
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("最高温度:",FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(255,255,255));
cell.setBackgroundColor(new BaseColor(255,255,255));
table1.addCell(cell);
cell=new PdfPCell(new Paragraph(maxtemp,FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(255,255,255));
cell.setBackgroundColor(new BaseColor(255,255,255));
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("平均温度:",FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(204,204,204));
cell.setBackgroundColor(new BaseColor(204,204,204));
table1.addCell(cell);
cell=new PdfPCell(new Paragraph(avgtemp,FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(204,204,204));
cell.setBackgroundColor(new BaseColor(204,204,204));
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("设备温度:",FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(255,255,255));
cell.setBackgroundColor(new BaseColor(255,255,255));
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("flr T200",FontChinese));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(255,255,255));
cell.setBackgroundColor(new BaseColor(255,255,255));
table1.addCell(cell);
//说明
cell=new PdfPCell(new Paragraph("说明",FontChinese));
cell.setColspan(2);
cell.setBackgroundColor(new BaseColor(102,102,102));
Color color=new Color(102,102,102);
//cell.setBackgroundColor(color);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph(objectname,FontChinese));
cell.setColspan(2);
//cell.setBackgroundColor(new Color(102,102,102));
cell.setBorder(0);
cell.setBackgroundColor(new BaseColor(204,204,204));
table1.addCell(cell);
float [] widths={0.1f,0.1f};
widths[0]=15f;
widths[1]=35f;
table1.setWidths(widths);
table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
doc.add(table1);
// 关闭文档对象,释放资源
doc.close();
//设置输出类型,这里我们需要以文件流的形式提供下载
response.addHeader("Content-Disposition", "attachment; filename=myfile.pdf");
response.setContentType("application/octet-stream");
response.setContentLength(outputStream.size());
ServletOutputStream out1 = response.getOutputStream();
outputStream.writeTo(out1);
out1.flush();
}catch (DocumentException e) {
e.printStackTrace();
}
%>