-- 作者:cydcom
-- 发布时间:9/27/2005 5:16:00 PM
-- 我写了个,生成pdf报表的方法,可是cell中的字段老是折行,各位大侠有没有办法解决下
我写了个,生成pdf报表的方法,可是cell中的字段老是折行,各位大侠有没有办法解决下 /** * 文件名称: * 公司: Butone * 作者: cyd * 版本号: Copyright (c) 2005 v 2.0 * 日期: 2005-9-26 * 更新: */ package com.butone.pdf.util; import java.awt.Color; import java.io.*; import java.util.Hashtable; import java.util.Vector; import javax.servlet.*; import javax.servlet.http.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; public class ExportPDF { public void exportNew(HttpServletResponse response, Vector vc, String tableName, String[] listName, String[] listName_Name) throws IOException { Document doc = new Document(PageSize.A2, 2, 2, 2, 2); try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(doc, buffer); //定义输出位置并把文档对象装入输出对象中 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); Font FontChinese = new Font(bfChinese, 32, Font.BOLD); String title = "我爱喝咖啡"; if (tableName != null && !tableName.equals("")) title = tableName; Paragraph t = new Paragraph(title, FontChinese); //表名居中 t.setAlignment(Paragraph.ALIGN_CENTER); //**** //定义一个表格 Table table = new Table(listName_Name.length , vc.size()); //设置表格边框 table.setBorderWidth(0); //table.setCellpadding(5); //table.setCellspacing(5); for (int j = 0; j < listName_Name.length; j++) { //设置中文字体 BaseFont _bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font _FontChinese = new Font(_bfChinese, 24, Font.NORMAL); String list_Name = listName_Name[j]; Paragraph par = new Paragraph(list_Name, _FontChinese); par.setAlignment(Paragraph.ALIGN_CENTER); par.setAlignment(Paragraph.CHUNK); //建立各cell将对象放入 Cell cell = new Cell(par); cell.setHeader(true); //分列 cell.setColspan(1); cell.setRowspan(1); cell.setNoWrap(true); cell.setBackgroundColor(Color.cyan); table.addCell(cell); table.setCellsFitPage(true); } for (int i = 0; i < vc.size(); i++) { Hashtable ht = (Hashtable) vc.get(i); for (int j = 0; j < listName_Name.length; j++) { BaseFont _bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font _FontChinese = new Font(_bfChinese, 18, Font.NORMAL); String list_col = (String) ht.get(listName[j]); String list_col_type = (String) ht.get(listName[j]+"_TYPE"); Paragraph par = new Paragraph(list_col, _FontChinese); //判断是数字还是字符 if(list_col_type.equals("NUMBER")) par.setAlignment(Paragraph.ALIGN_LEFT); else par.setAlignment(Paragraph.ALIGN_CENTER); Cell cell = new Cell(par); //分列 cell.setColspan(1); cell.setRowspan(1); cell.setNoWrap(true); //cell.setMarkupAttribute("class", "line2"); //cell.setHorizontalAlignment(cell.ALIGN_RIGHT); table.addCell(cell); table.setCellsFitPage(true); } } //***** //打开文档对象 doc.open(); doc.add(t); doc.add(table); // 关闭文档对象,释放资源 doc.close(); DataOutput output = new DataOutputStream(response.getOutputStream()); byte[] bytes = buffer.toByteArray(); response.setContentLength(bytes.length); for (int i = 0; i < bytes.length; i++) { output.writeByte(bytes[i]); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }
|