Archive for Tháng Bảy, 2005
Where should I put resources (like images and font…
Where should I put resources (like images and font files)?
The report is run as a new java process that’s executed in the directory where the file is located. That means that Jasper Reports can manage absolute or relative paths because the local directory is the directory where the file is located.
Can I run reports in a web application? File reportFile =
Yes.I t’s very easy to write a servlet (or a jsp) with some code like the following:
new File(application.getRealPath(" /reports/WebappReport.jasper"));Map parameters = new HashMap();parameters.put("ReportTitle", "Address Report");byte[] bytes = JasperRunManager.runReportToPdf( reportFile.getPath(), parameters, new WebappDataSource() );
response.setContentType("application/pdf");response.setContentLength(bytes.length);ServletOutputStream ouputStream = response.getOutputStream();ouputStream.write(bytes, 0, bytes.length);ouputStream.flush();ouputStream.close();
Add comment Tháng Bảy 23, 2005
http://www-128.ibm.com/developerworks/forums/dw_th…
http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?forum=185&message=13696316&thread=71535&cat=11
Now Java part of it here are code snippets
JasperReportHelper helper = new JasperReportHelper();
Map properties = new HashMap();
properties.put( "TITLE", "");
properties.put( "BEGIN_DATE", beginCal.getTime() );
properties.put( "END_DATE", endCal.getTime());
......
this.createReport(.......);
/**
* Generate an ActivityReport report in PDF format.
* properties: is the map that is created above with all the
* user entered variables
* reportDesign: is the compiled file name like someRep.jasper
* fileName: ouputfileName
* outputType like pdf
*/
public void createReport( Map properties, String reportDesign, String fileName, String outputType ) throws Exception {
InputStream is = null;
Connection conn = null;
try {
// Get an input stream to the compiled report defesign
is = this.getClass().getResourceAsStream(reportDesign);
JasperReport jasperReport = (JasperReport)JRLoader.loadObject( is );
/* Open a connection to the odbc data source entered by the user */
conn = Torque.getConnection();
if ( conn == null || conn.isClosed() ) {
throw new Exception( "Unable to get a valid connection to the database. ");
}
// populate the report
JasperPrint jasperPrint = JasperManager.fillReport( jasperReport, properties, conn );
if ( outputType.equalsIgnoreCase( "xml")) {
JasperExportManager.exportReportToXmlFile( jasperPrint, fileName, false );
} else if ( outputType.equalsIgnoreCase( "csv")) {
JRCsvExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName );
exporter.exportReport();
} else if ( outputType.equalsIgnoreCase( "xls")) {
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName );
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporter.exportReport();
} else {
// Write out the pdf file
JasperManager.printReportToPdfFile( jasperPrint, fileName );
}
} finally {
if ( is != null ) {
try {
is.close();
} catch(Exception ex ) {
}
}
try {
if ( conn != null && ! conn.isClosed() ) {
Torque.closeConnection( conn );
}
} catch ( Exception ex ) {
}
}
}
The variables put in the properties map will be available in the report xml and can be referenced as $P{BEGIN_DATE or whatever}
Add comment Tháng Bảy 12, 2005
Another Sample code for JasperReport using Servlet
JasperReport tren web
/CODE/
ServletContext context = this.getServletConfig().getServletContext();
File report_file = context.getRealPath(path);//get file
Map parameters = new HashMap();
parameters.put(“title”,reporttile);// set para
…
JasperPrint jasperPrint = null;
try {
//load jasper file
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(report_file .getPath());
//get jasperPrint instance
jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters, new JREmptyDataSource());
} catch (JRException e) {
e.printStackTrace();
}
if (jasperPrint != null) {//out put
response.setContentType(“application/octet-stream”);
ServletOutputStream ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
} else {
response.setContentType(“text/html”);
}
/ENDCODE/
Add comment Tháng Bảy 11, 2005
Dùng JRResultSetDataSource?
Dùng Resultset như là JRResultSetDataSource? Hãy xem thử ví dụ sau:
/CODE/
String connectString = “jdbc:oracle:thin:@192.168.10.40:1521:ORA92″;
String user = “SCOTT”;
String password = “tiger”;
Class.forName(driver);
Connection conn = DriverManager.getConnection(connectString, user, password);
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery(“select * from customers”);
//Preparing parameters
Map parameters = new HashMap();
parameters.put(“mytitle”, “Customer Details”);
JasperFillManager.fillReportToFile(fileName, parameters, getConnection());
/ENDCODE/
Add comment Tháng Bảy 5, 2005
Weeks calculation tips
To group by week you need to write a week grouping expression. This expression should assign a unique number for each week. Such a week calculation routine involves the use of java.util.Calendar and is best placed in a utility class. The routine would look like this (I am writing this from my head):
| Code: |
| public static Integer getWeekNumber(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return new Integer(calendar.get(Calendar.WEEK_OF_YEAR) * calendar.get(Calendar.YEAR)); } |
The second problem is to calculate the date of the first day in the week. This is also should be placed in a utility class and like the above it involves the use of the Calendar class:
| Code: |
| public static Date getWeekStartDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return calendar.getTime(); } |
Add comment Tháng Bảy 1, 2005
Another resources for Java server-side developers
Liệt kê những resource dùng để develop Java server-side, theo từng thể loại.
Xem ở đây : http://www.java201.com/
Trong đó còn có “List of free Java books for download” : http://www.java201.com/books.html
Add comment Tháng Bảy 1, 2005
JasperReport Servlet library, Jaspanese language :-(
Publish JasperReports trên Web?, muốn có paging ?
http://sourceforge.jp/projects/jrservlet/
hoặc tham khảio thêm ở đây nếu rành tiếng Nhật
:http://www.ponpo.com/jrs/
Add comment Tháng Bảy 1, 2005
Java Excel API – A Java API to read, write, and modify Excel spreadsheets
eBao dùng library này để tạo Excel từ report. Mình cũng phải học thôi chứ đâu biết làm sao bây giờ
.
Xem thư viện jexcelapi ở đây:
http://jexcelapi.sourceforge.net/
Một số thư viện tương tự:
Jakarta POI: đây là thư viện jasperreports dùng để tạo ra file Excels
XlsJdbc : Một thư viện tương thích với JDBC, dùng để hỗ trợ java đọc Exel file
Add comment Tháng Bảy 1, 2005