http://valley913.blogdriver.com/valley913/306395.html
http://www.cneportal.com/blog/detail.asp?blog_id=1&content_id=119
http://jasperreports.sourceforge.net/samples/index.html
http://www.cneportal.com/blog/detail.asp?blog_id=1&content_id=119
http://jasperreports.sourceforge.net/samples/index.html
解决方案 »
- swfupload 后台取值问题
- 怎样在Jsp里面编写留言版的翻页效果!!!完整的代码
- JSP后台能添加类似JAVA的线程吗?
- struts2.0显示日期的标签问题
- 大家有没有做过这样的网站发布系统?
- HttpServletRequest和HttpServletResponse这两个定义分别代表什么含义呢?我知道String是代表定义字符串的。
- ~低级问题?在线等!!!
- JAVA1.6编译的applet在JAVA1.7下边不能打开 是什么原因?
- 又有事情向大家请教,问题可能简单的点,大家不要笑!!
- tomcat启动提示 not found on the java.library.path
- 如何让同一个账号,同一时间只能允许他登录一次!
- 救命!boss要求上午就要实现的一个用javascrip的小功能
String str=JasperRunManager.runReportToHtmlFile(reportFile.getPath(),parameters,ds);
response.setContentType("application/excel");
response.addHeader("Content-Disposition","inline;filename=report.xls");
//在浏览器中打开pdf文件的关键所在
response.setContentLength(str.length() );
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(str.getBytes(), 0, str.length());
ouputStream.flush();
ouputStream.close();
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.engine.util.*;
import net.sf.jasperreports.view.*;
import java.util.*;
import java.sql.*;
import java.io.*;
import java.awt.*;
/**
* @author Teodor Danciu ([email protected])
* @version $Id: JasperApp.java,v 1.5 2005/02/03 15:31:47 teodord Exp $
*/
public class JasperApp
{
/**
*
*/
private static final String TASK_COMPILE = "compile";
private static final String TASK_FILL = "fill";
private static final String TASK_PRINT = "print";
private static final String TASK_PDF = "pdf";
private static final String TASK_XML = "xml";
private static final String TASK_XML_EMBED = "xmlEmbed";
private static final String TASK_HTML = "html";
private static final String TASK_XLS = "xls";
private static final String TASK_CSV = "csv";
private static final String TASK_RUN = "run";
/**
*
*/
public static void main(String[] args)
{
String fileName = null;
String taskName = null; if(args.length == 0)
{
usage();
return;
}
int k = 0;
while ( args.length > k )
{
if ( args[k].startsWith("-T") )
taskName = args[k].substring(2);
if ( args[k].startsWith("-F") )
fileName = args[k].substring(2);
k++;
} try
{
long start = System.currentTimeMillis();
if (TASK_COMPILE.equals(taskName))
{
JasperCompileManager.compileReportToFile(fileName);
System.err.println("Compile time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else if (TASK_FILL.equals(taskName))
{
//Preparing parameters
Image image = Toolkit.getDefaultToolkit().createImage("dukesign.jpg");
MediaTracker traker = new MediaTracker(new Panel());
traker.addImage(image, 0);
try
{
traker.waitForID(0);
}
catch (Exception e)
{
e.printStackTrace();
}
Map parameters = new HashMap();
parameters.put("ReportTitle", "The First Jasper Report Ever");
parameters.put("MaxOrderID", new Integer(10500));
parameters.put("SummaryImage", image);
JasperFillManager.fillReportToFile(fileName, parameters, getConnection());
System.err.println("Filling time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else if (TASK_PRINT.equals(taskName))
{
JasperPrintManager.printReport(fileName, true);
System.err.println("Printing time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else if (TASK_PDF.equals(taskName))
{
JasperExportManager.exportReportToPdfFile(fileName);
System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else if (TASK_XML.equals(taskName))
{
JasperExportManager.exportReportToXmlFile(fileName, false);
System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else if (TASK_XML_EMBED.equals(taskName))
{
JasperExportManager.exportReportToXmlFile(fileName, true);
System.err.println("XML creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else if (TASK_HTML.equals(taskName))
{
JasperExportManager.exportReportToHtmlFile(fileName);
System.err.println("HTML creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else if (TASK_XLS.equals(taskName))
{
File sourceFile = new File(fileName);
JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls");
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporter.exportReport(); System.err.println("XLS creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else if (TASK_CSV.equals(taskName))
{
File sourceFile = new File(fileName);
JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".csv");
JRCsvExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.exportReport(); System.err.println("CSV creation time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else if (TASK_RUN.equals(taskName))
{
//Preparing parameters
Image image = Toolkit.getDefaultToolkit().createImage("dukesign.jpg");
MediaTracker traker = new MediaTracker(new Panel());
traker.addImage(image, 0);
try
{
traker.waitForID(0);
}
catch (Exception e)
{
e.printStackTrace();
}
Map parameters = new HashMap();
parameters.put("ReportTitle", "The First Jasper Report Ever");
parameters.put("MaxOrderID", new Integer(10500));
parameters.put("SummaryImage", image);
JasperRunManager.runReportToPdfFile(fileName, parameters, getConnection());
System.err.println("PDF running time : " + (System.currentTimeMillis() - start));
System.exit(0);
}
else
{
usage();
System.exit(0);
}
}
catch (JRException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/**
*
*/
private static void usage()
{
System.out.println( "JasperApp usage:" );
System.out.println( "\tjava JasperApp -Ttask -Ffile" );
System.out.println( "\tTasks : compile | fill | print | pdf | xml | xmlEmbed | html | xls | csv | run" );
}
/**
*
*/
private static Connection getConnection() throws ClassNotFoundException, SQLException
{
//Change these settings according to your local configuration
String driver = "org.hsqldb.jdbcDriver";
String connectString = "jdbc:hsqldb:hsql://localhost";
String user = "sa";
String password = "";
Class.forName(driver);
Connection conn = DriverManager.getConnection(connectString, user, password);
return conn;
}
}
JasperRunManager.runReportToHtmlFile(reportFile.getPath(),parameters,ds)
返回的是文件名字
JasperExportManager.exportReportToHtmlFile(fileName);
也是把数据存在一个临时文件中
可以参考jasperreport的自带demo,使用JRHtmlExporter来直接导出JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
Map parameters = new HashMap();
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
conn
);
JRHtmlExporter exporter = new JRHtmlExporter();
Map imagesMap = new HashMap();
request.getSession().setAttribute("IMAGES_MAP", imagesMap);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image.jsp?image=");
exporter.exportReport();