JasperReport动态列

JasperReport动态列-1
2009-03-31 16:00
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
  
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.PrinterName;
  
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.base.JRBaseLine;
import net.sf.jasperreports.engine.base.JRBasePrintText;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JRDesignBand;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
import net.sf.jasperreports.engine.util.JRSaver;
  
import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
  
import com.inqgen.iqlis.template.jsf.bean.TemplateDirectoryAdminBean;
  
/**
* JasperReport 動態列 打印
* @author hxy
*
*/
public class ReportProcess {
  
private static Log logger = LogFactory
   .getLog(TemplateDirectoryAdminBean.class);
/** 設置字段寬度 */
private final static int textWidth = 80;
/** 設置字段高度 */
private final static int textHeight = 20;
/** coulumnHeader區域字體大小 */
private final static int columnHeaderfontSize = 14;
/** detail 區域字體大小 */
private final static int fontSize = 12;
/** 設置間距 */
private final static int X = 80;
/** coulumnHeader區域高度 */
private final static int columnHeaderHeight = 20;
/** detail 區域高度 */
private final static int detailHeight = 20;
/** */
private static String aliasColumn = "column";
  
public static void main(String args[]){
try {
   String[] columns = {"字段1", "字段2", "字段3", "字段4"};
   File f = new File("d://test.pdf");
   List<Object[]> list = new ArrayList<Object[]>();
   Object[] obj = null;
   for(int j=0;j<50;j++){
    obj = new Object[columns.length];
    for(int i=0;i<columns.length;i++){
     obj[i] = columns[i]+j+","+i;
    }
    list.add(obj);
   }
   OutputStream out = new FileOutputStream(f);
   preview(columns, list, out);
   out.close();
   logger.info("成功~");
} catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
}
  
/**
* PDF打印
*
* @param headers
*            colimnHeaders
* @param list
*            數據來源
* @param out
*            輸出流
* @throws Exception
*/
public static void preview(String headers[], List<Object[]> list,
   OutputStream out) throws Exception {
long start = System.currentTimeMillis();
String[] alias = preaseAliasColumnHeaders(headers);
JasperReport jp = getJasperReport(headers, alias);
Map<String, Object> parameters = new HashMap<String,Object>();
JasperPrint jasperPrint = JasperFillManager.fillReport(jp, parameters,
    new JRBeanCollectionDataSource(getBaseList(alias, phrase(list))));
logger.info("Filling time : " + (System.currentTimeMillis() - start));
JasperExportManager.exportReportToPdfStream(jasperPrint, out);
logger.info("Printing time : " + (System.currentTimeMillis() - start));
}
  
/**
* 打印機打印報表
*
* @param headers
*            colimnHeaders
* @param list
*            數據來源
* @param parameters
*            Map 參數
* @param printerName
*            打印機名稱
* @param printerName
*            打印機份數
* @param tempFileName
*            创建报表打印临时文件的路径
* @throws Exception
*/
public static void print(String headers[], List<Object[]> list,
   Map<String, Object> parameters, String printerName, int copies,
   String tempFileName) throws Exception {
long start = System.currentTimeMillis();
/** 获取打印报表数据 */
String[] alias = preaseAliasColumnHeaders(headers);
JasperReport jp = getJasperReport(headers, alias);
JasperPrint jasperPrint = JasperFillManager.fillReport(jp, parameters,
    new JRBeanCollectionDataSource(getBaseList(alias, phrase(list))));
/** 保存向打印机发送的临时报表数据 */
JRSaver.saveObject(jasperPrint, tempFileName);
logger.info("Filling time : " + (System.currentTimeMillis() - start));
/** 设置打印参数 */
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
/** 设置A4纸张 */
printRequestAttributeSet.add(MediaSizeName.ISO_A4);
/** 設置打印份數 */
printRequestAttributeSet.add(new Copies(copies));
/** 设置打印机 */
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
/** 添加打印机名称 */
// "Epson Stylus 800 ESC/P// 2"
printServiceAttributeSet.add(new PrinterName(printerName, null));
/** 打印机对象 */
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
/** 向打印机发送的临时报表数据 */
exporter
    .setParameter(JRExporterParameter.INPUT_FILE_NAME, tempFileName);
exporter.setParameter(
    JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,
    printRequestAttributeSet);
exporter.setParameter(
    JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
    printServiceAttributeSet);
/** 是否弹出打印页数的提示对话框 */
exporter.setParameter(
    JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,
    Boolean.FALSE);
/** 是否弹出打印机设置属性的提示对话框 */
exporter.setParameter(
    JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,
    Boolean.FALSE);
/** 执行打印机打印报表 */
exporter.exportReport();
logger.info("Printing time : " + (System.currentTimeMillis() - start));
}

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值