日期:2014-05-20  浏览次数:20850 次

打印问题(急。。。。)
package com.assfd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

public class ReportTest
{
@SuppressWarnings("unchecked")
public static void main(String[] args) 
{  
Connection conn = null;  
try 
{  
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");  
conn = DriverManager.getConnection(  
"jdbc:microsoft:sqlserver://localhost:1433;databaseName=reportTest","sa","" );  
 
String temp=System.getProperty("user.dir");
String dir = temp + "//src//file//reportTest";  
 
JasperCompileManager.compileReportToFile(dir+ ".jrxml",dir + ".jasper");  
 
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(dir + ".jasper");  
 
Map<String, String> map = new HashMap<String, String>();  
map.put("Facility", "Technology");  
 
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, conn); 
JasperPrintManager.printReport(jasperPrint,true);
   
exportReport(dir ,"xls",jasperPrint); 
exportReport(dir ,"pdf",jasperPrint);
 
JasperViewer jrview = new JasperViewer(jasperPrint,false);  
jrview.setVisible(true);  
 
conn.close();  
 
} catch (Exception e) {  
if(conn != null){  
try {  
conn.close();  
} catch (SQLException e1) {}  
}  
e.printStackTrace();  
}  
 
}  

private static void exportReport(String fileName, String format,JasperPrint print) throws JRException
{  
JRExporter exporter = null;  
if (format.equalsIgnoreCase("pdf"))  
{  
exporter = new net.sf.jasperreports.engine.export.JRPdfExporter(); 
fileName = fileName+".pdf";  
}  
else if (format.equalsIgnoreCase("csv"))  
{  
exporter = new net.sf.jasperreports.engine.export.JRCsvExporter();  
 
fileName = fileName+".csv";  
}  
else if (format.equalsIgnoreCase("html"))  
{  
exporter = new net.sf.jasperreports.engine.export.JRHtmlExporter();  
 
fileName = fileName+".html";  
 
}  
else if (format.equalsIgnoreCase("xls") || format.equalsIgnoreCase("xls2"))  
{  
 
if (format.equalsIgnoreCase("xls"))