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?
Yes.I t’s very easy to write a servlet (or a jsp) with some code like the following:

File reportFile =

 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();

Advertisements

Tháng Bảy 23, 2005 at 8:55 sáng Bạn nghĩ gì về bài viết này?

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}

Tháng Bảy 12, 2005 at 12:02 chiều Bạn nghĩ gì về bài viết này?

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/

Tháng Bảy 11, 2005 at 11:57 sáng Bạn nghĩ gì về bài viết này?

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/

Tháng Bảy 5, 2005 at 9:07 sáng Bạn nghĩ gì về bài viết này?

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();
}

Tháng Bảy 1, 2005 at 9:04 sáng Bạn nghĩ gì về bài viết này?

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

Tháng Bảy 1, 2005 at 4:24 sáng Bạn nghĩ gì về bài viết này?

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/

Tháng Bảy 1, 2005 at 2:28 sáng Bạn nghĩ gì về bài viết này?

Bài đăng cũ hơn


Tháng Bảy 2005
H B T N S B C
« Th6   Th8 »
 123
45678910
11121314151617
18192021222324
25262728293031

Bài viết mới

RSS Jason Rudolph (GRails)

  • Lỗi: có thể dòng không tin đang không hoạt động. Hãy thử lại sau.