⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 output.java

📁 WAP PUSH后台源码,WAP PUSH后台源码
💻 JAVA
字号:
package com.sxit.wap.pdf;
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;

public class Output {
	/*
	 * 把ResultSet 写到OutputStream中
	 */
	public void toPdf ( javax.servlet.http.HttpServletResponse response, ResultSet result ) {
		try {
			response.setContentType ( "application/pdf" );
			ByteArrayOutputStream buffer = getPdfByteArrayOutputStream ( result );
			response.setContentLength ( buffer.size() );
			//将buffer写到out中
			java.io.OutputStream out = response.getOutputStream () ;
			buffer.writeTo(out );
			out.flush();
		}catch (Exception e1) {
		}
	}

	/*
	 * 返回将一个Document,result转换成pdf表格后,得到pdf文件的byte流
	 */
	public ByteArrayOutputStream getPdfByteArrayOutputStream ( ResultSet result ) {
		Document document = new Document ();
		ByteArrayOutputStream buffer = null;
		try {
			//将result写到buffer中
			buffer = new ByteArrayOutputStream();
			PdfWriter writer = PdfWriter.getInstance( document, buffer );

			document.open ();
			toPdfDocument ( document, result);

		} catch ( Exception e1 ) {
			e1.printStackTrace();
		}
		document.close ();
		return buffer;
	}

	/*
	 *把ResultSet 写到Document中,即构成pdf文档
	 */
	private void toPdfDocument ( Document document, ResultSet result ) throws Exception {
		try {

			BaseFont bfChinese = BaseFont.createFont ( "STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED );
			Font FontChinese = new Font ( bfChinese, 9, Font.BOLD ); ;
			String[] tableHeads = result.getHeader();
			//生成表头
			int columns = tableHeads.length ;
			Table table = new Table(columns);
			//table.setLastHeaderRow(2);
			table.setPadding(3);
			table.setBorderWidth(1);


			Cell cell = new Cell(new Phrase(result.getTitle(), FontChinese ));
			cell.setHorizontalAlignment(cell.ALIGN_CENTER);
			cell.setHeader(true);
			cell.setColspan(columns);
			table.addCell(cell);
			for ( int iHead = 0; iHead < columns; iHead++ ) {
				System.out.println(tableHeads[iHead] ) ;
				cell = new Cell(new Phrase( tableHeads[iHead] , FontChinese ));
				cell.setBackgroundColor(new java.awt.Color(190,190,190));
				cell.setHeader(true);
				table.addCell ( cell );
			}

			table.endHeaders();
			//填写表记录
			String[] str = null;
			while ( (str = result.next ()) !=null ) {
				for ( int iRow = 0; iRow < columns; iRow++ ) {
					System.out.println(str[iRow] ) ;
					table.addCell ( new Cell(new Phrase( str[iRow] , FontChinese ) ));
				}
			}
			document.add(table);

		} catch ( DocumentException e1 ) {
			e1.printStackTrace ();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -