呼!!
之前完全沒有iREPORT的經驗, 這段語法測了好久,
現在已經可以使用兩個範例,
一個是直接使用MOC001.jasper,
一個就是這個可以把SQL語法帶入報表中.
<%@ page contentType="text/html;charset=UTF-8"%>
<%
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
%>
<%@page import="net.sf.jasperreports.engine.*"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.JasperRunManager"%>
<%@page import="net.sf.jasperreports.engine.*"%>
<%@page import="net.sf.jasperreports.engine.util.*"%>
<%@page import="net.sf.jasperreports.engine.export.*"%>
<%@page import="net.sf.jasperreports.j2ee.servlets.*"%>
<%@page import="net.sf.jasperreports.engine.JasperPrintManager"%>
<%@page import="net.sf.jasperreports.view.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
/* 連線部分 */
Connection conn=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:pg","pg","_pg");
Statement stmt = con.createStatement();
ResultSet rs;
try {
//SQL語法
String StrSQL = " select * from test ";
//out.println(StrSQL);
rs= stmt.executeQuery(StrSQL);
if(!rs.next()){return;}
else{
out.println(rs.getString("訂單日期"));
out.println("有資料");
JRResultSetDataSource jrds=null;
JasperPrint jasperPrint=null;
Map reportParams = new HashMap();
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(application.getRealPath("MOC001.jasper"));
jrds = new JRResultSetDataSource(rs);
jasperPrint =JasperFillManager.fillReport(jasperReport, reportParams,jrds);
//產出PDF
JasperPrintManager.printReportToPdfFile(jasperPrint, application.getRealPath("MOC001.pdf"));
/* 把報表轉換成 PDF, 給定 ( RecordSet, 輸出路徑 ) */
//JasperExportManager.exportReportToPdfFile(jasperPrint,outPath);
/* 把報表轉換成 PDF, 給定 ( 輸入路徑, 輸出路徑, 參數, 連線 ) */
//JasperRunManager.runReportToPdfFile(filePath, outPath, map, con);
}
} catch (JRException e) {
e.printStackTrace();
}
con.close();
%>
<% out.println("讀取ERP每日進度表");%>
沒有留言:
張貼留言