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;
public class ReportProcess {
private static Log logger = LogFactory
.getLog(TemplateDirectoryAdminBean. class );
private final static int textWidth = 80 ;
private final static int textHeight = 20 ;
private final static int columnHeaderfontSize = 14 ;
private final static int fontSize = 12 ;
private final static int X = 80 ;
private final static int columnHeaderHeight = 20 ;
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 list = new ArrayList();
Object[] obj = null ;
for ( int j= 0 ;j< 50 ;j++){
obj = new Object[columns.length];
for ( int i= 0 ;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) {
e.printStackTrace();
}
}
public static void preview(String headers[], List list,
OutputStream out) throws Exception {
long start = System.currentTimeMillis();
String[] alias = preaseAliasColumnHeaders(headers);
JasperReport jp = getJasperReport(headers, alias);
Map parameters = new HashMap();
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));
}
public static void print(String headers[], List list,
Map 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();
printRequestAttributeSet.add(MediaSizeName.ISO_A4);
printRequestAttributeSet.add( new Copies(copies));
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
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));
}
public static List phrase(List list){
List temps = new ArrayList();
String []s= null ;
for (Object[] obj : list){
s = new String[obj.length];
for ( int i = 0 ;i
s[i] = obj[i].toString();
}
temps.add(s);
}
return temps;
}
private static String[] preaseAliasColumnHeaders(String headers[]) {
int size = headers.length;
String[] alias = new String[size];
for ( int i = 0 ; i < size; i++) {
alias[i] = aliasColumn + i;
}
return alias;
}
@SuppressWarnings ( "deprecation" )
private static JasperReport getJasperReport(String[] headers,
String alias[]) throws JRException {
JasperDesign design = new JasperDesign();
design.setName( "statistics" );
design.setPrintOrder(JRReport.PRINT_ORDER_VERTICAL);
design.setOrientation(JRReport.ORIENTATION_PORTRAIT);
design.setPageWidth( 595 );
design.setPageHeight( 842 );
design.setColumnWidth( 535 );
design.setColumnSpacing( 0 );
design.setLeftMargin( 30 );
design.setRightMargin( 30 );
design.setTopMargin( 20 );
design.setBottomMargin( 20 );
design.setWhenNoDataType(JRReport.WHEN_NO_DATA_TYPE_BLANK_PAGE);
design.setTitleNewPage( false );
design.setSummaryNewPage( false );
JRDesignBand columnHeader = new JRDesignBand();
columnHeader.setHeight(columnHeaderHeight);
JRDesignBand detail = new JRDesignBand();
detail.setHeight(detailHeight);
for ( int i = 0 ; i < headers.length; i++) {
JRDesignStaticText staticText = new JRDesignStaticText();
staticText.setText(headers[i]);
staticText.setFontSize(columnHeaderfontSize);
staticText.setHeight(textHeight);
staticText.setWidth(textWidth);
staticText.setX(X * i);
staticText.setPdfFontName( "MHei-Medium" );
staticText.setPdfEmbedded( true );
staticText.setPdfEncoding( "UniCNS-UCS2-H" );
staticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
staticText.setLeftBorder(JRBaseLine.PEN_1_POINT);
staticText.setTopBorder(JRBaseLine.PEN_1_POINT);
staticText.setRightBorder(JRBaseLine.PEN_1_POINT);
staticText.setBottomBorder(JRBaseLine.PEN_1_POINT);
columnHeader.addElement(staticText);
JRDesignField field = new JRDesignField();
field.setName(alias[i]);
field.setValueClass(String. class );
design.addField(field);
JRDesignTextField textField = new JRDesignTextField();
JRDesignExpression expression = new JRDesignExpression();
expression.setText( "$F{" + alias[i] + "}" );
expression.setValueClass(String. class );
textField.setExpression(expression);
textField.setFontSize(fontSize);
textField.setHeight(textHeight);
textField.setWidth(textWidth);
textField.setX(X * i);
textField.setPdfFontName( "MHei-Medium" );
textField.setPdfEmbedded( true );
textField.setPdfEncoding( "UniCNS-UCS2-H" );
textField.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
textField.setLeftBorder(JRBaseLine.PEN_1_POINT);
textField.setTopBorder(JRBaseLine.PEN_1_POINT);
textField.setRightBorder(JRBaseLine.PEN_1_POINT);
textField.setBottomBorder(JRBaseLine.PEN_1_POINT);
detail.addElement(textField);
}
design.setColumnHeader(columnHeader);
design.setDetail(detail);
return JasperCompileManager.compileReport(design);
}
private static List getBaseList(String[] headers,
List list) throws Exception {
List result = new ArrayList();
int length = headers.length;
DynaProperty[] dynaProps = new DynaProperty[length];
for ( int i = 0 ; i < length; i++) {
dynaProps[i] = new DynaProperty(headers[i], String. class );
}
BasicDynaClass dynaClass = new BasicDynaClass( "first" ,
BasicDynaBean. class , dynaProps);
for (Object[] obj : list) {
DynaBean employee = dynaClass.newInstance();
for ( int i = 0 ; i < length; i++) {
employee.set(headers[i], obj[i]);
}
result.add(employee);
}
return result;
}
}
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 list = new ArrayList();
Object[] obj = null;
for(int j=0;j<50;j++){
obj = new Object[columns.length];
for(int i=0;i list,
OutputStream out) throws Exception {
long start = System.currentTimeMillis();
String[] alias = preaseAliasColumnHeaders(headers);
JasperReport jp = getJasperReport(headers, alias);
Map parameters = new HashMap();
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 list,
Map 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));
}
public static List phrase(List list){
List temps = new ArrayList();
String []s= null;
for(Object[] obj : list){
s = new String[obj.length];
for(int i = 0;i getBaseList(String[] headers,
List list) throws Exception {
List result = new ArrayList();
int length = headers.length;
DynaProperty[] dynaProps = new DynaProperty[length];
for (int i = 0; i < length; i++) {
dynaProps[i] = new DynaProperty(headers[i], String.class);
}
BasicDynaClass dynaClass = new BasicDynaClass("first",
BasicDynaBean.class, dynaProps);
for (Object[] obj : list) {
DynaBean employee = dynaClass.newInstance();
for (int i = 0; i < length; i++) {
employee.set(headers[i], obj[i]);
}
result.add(employee);
}
return result;
}
}