下面是使用Eclipse的Im的部分代码
<!--menu.jsp-->
<h1>Pass parameter to jasper report(CI_ID)</h1>
<form action = "call.jsp" target="_blank">
<input type= "text" name = "CI_ID" value = ""/><br/>
<input type="submit" value="Print"/>
</form>
<!--call.jsp-->
<%Connection conn = null;
int id=Integer.parseInt(request.getParameter("CI_ID"));
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","1234");
File reportFile = new File(application.getRealPath("//Report.jasper"));
Map parameters = new HashMap();
parameters.put("CI_ID",id);
byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,conn);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0 , bytes.length);
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e){
e.printStackTrace();
}%>
我预览了Jasper报表,没有任何问题,但是当我使用JSP将参数传递给Eclipse上的Jasper报表时,我得到了这个错误:“File does not begin with'%pdf-'。”adobe Reader弹出窗口。
我试过下面的解决方案,但还是不起作用
1)更改为
response.setContentType("application/pdf");
response.setContentType("application/x-pdf");
2)更新adobe reader
3)导入jasper的所有相关.jar
首先,您有会话属性和request.getParameters以便可以将这些参数发送到jasper报告。我有一个jsp页面,如searchdata.jsp
<form action="searchResult.jsp" method="POST">
<table border="0" width="" cellspacing="15" cellpadding="1">
<thead>
<tr>
<th>Search Using:</th>
<th><select name="search">
<option value=""> Select</option>
<option value="Company Name">Company Name</option>
<option value="Contact Person">Contact Person</option>
<option value="Phone">Phone Number</option>
</select></th>
<th><input type="text" name="search_tf" /></th>
<th><input type="submit" value=" " class="search_btn"/></th>
</tr>
</thead>
<tr>
<td></td><td></td><td></td><td></td>
<td><small>(Blank Search will result in Full Search Result)</small></td>
</tr>
</table>
</form>
单击submit按钮(因为我有“SearchResult.jsp”的操作)后,它将转到SearchResult.jsp
在searchresult.jsp页面中,我使用了request.getParameter
,以便在前一个jsp页面中输入的数据现在将进入当前jsp页面,然后我使用了会话属性,以便在以后的jsp页面中使用它,直到会话结束。如下所示
Search Result for <u><%= request.getParameter("search_tf")%></u> in <u><%= request.getParameter("search")%></u> is :
Object o2 = session.getAttribute("email");
String email = o2.toString();
String category = request.getParameter("category");
String search = request.getParameter("search");
session.setAttribute("search",search);
String search_tf = request.getParameter("search_tf");
session.setAttribute("search_tf",search_tf);
if(search.equals("")&& search_tf.equals("")){
sqlPgintn="SELECT SQL_CALC_FOUND_ROWS Id,Company_name,Contact_Person,Address,Phone,Company_Email,Review,Status,Lead_Date,Lead_Details,Lead_Value,followup_Date,Category from marketing_database.lead limit "+iPagNo+","+iSwRws+"";
}
else if(search.equals("Company Name"))
{
sqlPgintn="SELECT SQL_CALC_FOUND_ROWS Id,Company_name,Contact_Person,Address,Phone,Company_Email,Review,Status,Lead_Date,Lead_Details,Lead_Value,followup_Date,Category from marketing_database.lead where Company_Name ='"+search_tf+"' limit "+iPagNo+","+iSwRws+"";
<script type="text/javascript">
function generateReport() {
var e = document.getElementById("idOfYourSelectYouNeedToAddedIt");
var strPage = e.options[e.selectedIndex].value;
window.open(strPage);
return false; //This make you stay on this page;
//return true; //Set the action tag in the form to the page you like to go to!
}
</script>
<center> <form name="myForm" onsubmit="return generateReport()">
<select id = "idOfYourSelectYouNeedToAddedIt">
<option value=''> Generate Report </option>
<option value='samplePDF.jsp'> PDF</option>
<option value='sampleDOC.jsp'> DOC </option>
<option value='sampleXLS.jsp'> XLS</option>
<option value='sampleXLSX.jsp'> XLSX </option>
</select>
<br/>
<input type="submit" value="Submit">
</form></center>
当我单击假设“samplepdf.jsp”时,它将转到该页面,在那里我做了这个samplepdf.jsp
<%
Connection conn = null;
Object o2 = session.getAttribute("email");
String email = o2.toString();
String category=(String)session.getAttribute("category");
String status=(String)session.getAttribute("status");
String startDate=(String)session.getAttribute("startDate");
String endDate=(String)session.getAttribute("endDate");
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
String sql = "select * from lead where Email = '" + email + "' AND Status LIKE '%" + status + "%' AND Category LIKE '%" + category + "%' AND STR_TO_DATE(`Lead_Date`, '%d-%m-%Y') BETWEEN '"+startDate+"' AND '"+endDate+"'";
ServletContext context = request.getServletContext();
String fullPath = context.getRealPath("/WEB-INF/reports/report10.jrxml");
InputStream input = new FileInputStream(new File(fullPath));
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("sql",sql);
byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, map, conn);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0, bytes.length);
outStream.flush();
outStream.close();
conn.close();
}
catch(Exception e)
{e.printStackTrace();}
%>
我想使用不同项目的jasper报告生成一个账单。为此,我定义了一些字符串数组,希望在账单中显示为项目列表。 我填写jasper报告的代码如下 Sling数组用于填充ArrayList,这些ArrayList被传递到HashMap以填充我的报告。HasMap键是在文件中声明Invoice.jrxml参数名称。参数名称被赋予如下文本文件 我想打印项目列表,但我得到的输出是单行逗号分隔值。 此处为示例输
我正在用Java Swing和Jasper Report开发一个项目。 }}
我还访问了http://javaskeleton.blogspot.sg/2010/12/embedding-fonts-into-pdf-generated-by.html,但没有成功。 我错过了什么?如有任何帮助,不胜感激
我的JSF2.0web应用程序旨在生成PDF报告。问题是在资源管理器窗口中没有显示PDF报告。我正在使用eclipse kepler,带有apache-tomcat-7.0.52,并且jasper Ireport的版本是4.8。 我将提供整个java类: 我可以在控制台看到消息,并且页面被刷新,但是没有PDF报告出现在explorer屏幕上 我已经用下面的代码替换了上面的PDF方法,但是徒劳的是,
我对Jasper Sub报告有问题。我有一个报告模板,它有一个子报告;在主报表中,我有一些参数应该在子报表中也可用。
我使用XML用Java和Intellij IDE生成一个jasper报告,jasper版本是5.0.4。 在文件中有欧元符号,但是在pdf上创建报表时,我看到的是而不是 它是如何来的,我应该向文件添加什么来解决这个问题?