用iReport导出的txt文件中中文部分成了乱码,这是什么原因,如何解决? 10
展开全部
安装就不用说了,但是要理解Ireport和JasperReport的关系十分重要,Ireport是设计报表的图形界面,在Ireport里新建一个报表是以.jrxml的形式保存的,而最终要用到的是将.jrxml的文件编译为.jsper的文件。.jasper文件就是JaserReport需要调用的报表模版,JasperReport的强大功能就在于它能将.jasper转化为各种文件的格式,如xls,rtf,pdf等等最常用的文件格式。
要将.jsper文件应用在项目中,首先要做几样准备工作:
1.将安装好的Ireport,找到它下面的lib目录,再把jasperreports-1.3.0.jar这个包复制出来(这个包是JasperReport导出的关键),然后将这个包导入到需要应用的项目中,然后在eclipse中重新构建整个项目,这是最关键的第一步。
2.如果导出出现乱码的问题,需要引入iText亚洲语言包,可以在 http://www.lowagie.com/iText/ 查看关于iText的相关信息,包括源代码,文档.
3.设计好报表,将报表编译为.jsper文件,这些文件在iReport的安装路径下,很容易找到。
现在万事俱备,只欠东风了。我在开始写程序的的时候,在网上找了好些代码,而导出的过程中用到的类 JRRtfExporter 的源代码也踩过,并且发现了 http://www.koders.com 这个非常好的网站,这是开源项目的必备,在上面基本上很多类的源码也能查到。
接着自己封装了一个JasperReport类,而这个类可以实现导出各种格式的文件,代码如下:
packagecommon.ireport;
importnet.sf.jasperreports.engine.*;
importnet.sf.jasperreports.engine.export.*;
importnet.sf.jasperreports.engine.export.JRRtfExporter;
importjava.util.List;
importjava.io.*;
import com.log.Logger;
/**
*IReport+JasperReport导出报表类接口
*<p>Title:导出报表类</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2007</p>
*<font class=companyfont>
*CES
*</font>
*@author 野谷子
*@version 1.0.2007.02.14
*/
publicclassJasperReport{
privateLogger logger= newLogger("common.ireport.JasperReport");
/**CreatesanewinstanceofMyReport*/
publicJasperReport(){
}
/*
* 导出单个报表为rtf或word
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("application/msword");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.doc");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToRTF(JasperPrint jasperPrint,OutputStreamout){
JRRtfExporter rtfExporter= newJRRtfExporter();
rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
try{
rtfExporter.exportReport();
logger.debug("Genertate the RTF report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the RTF report file ERROR!");
}
}
/*
* 导出多个报表为rtf或word
* @param List,OutputStream
* @ 页面设置response.setContentType("application/msword");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.doc");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportListToRTF(List jasperPrints,OutputStream out){
JRRtfExporter rtfExporter= newJRRtfExporter();
rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints);
rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
try{
rtfExporter.exportReport();
logger.debug("Genertate the mutiply RTF report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the mutiply RTF report file ERROR!");
}
}
/*
* 导出单个报表为PDF
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("application/pdf");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.pdf");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToPDF(JasperPrint jasperPrint,OutputStreamout){
JRPdfExporter pdfExporter = newJRPdfExporter();
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
pdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
try{
pdfExporter.exportReport();
logger.debug("Genertate the PDF report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the PDF report file ERROR!");
}
}
/*
* 导出单个报表为XLS
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("application/vnd.ms-excel");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.xls");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToXLS(JasperPrint jasperPrint,OutputStreamout){
JRXlsExporter xlsExporter= newJRXlsExporter();
xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
try{
xlsExporter.exportReport();
logger.debug("Genertate the XLS report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the XLS report file ERROR!");
}
}
/*
* 导出单个报表为HTML
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("text/html");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.html");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToHTML(JasperPrint jasperPrint,OutputStreamout){
JRHtmlExporter htmlExporter = newJRHtmlExporter();
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
htmlExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
htmlExporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.FALSE);
try{
htmlExporter.exportReport();
logger.debug("Genertate the HTML report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the HTML report file ERROR!");
}
}
/*
* 导出单个报表为CSV
* @param JasperPrint,OutputStream
*/
publicvoidexportReportToCSV(JasperPrint jasperPrint,OutputStreamout){
JRCsvExporter csvExporter = newJRCsvExporter();
csvExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
csvExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
try{
csvExporter.exportReport();
logger.debug("Genertate the CSV report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the CSV report file ERROR!");
}
}
/*
* 导出单个报表为TXT
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("text/html");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.txt");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToTXT(JasperPrint jasperPrint,OutputStreamout){
JRTextExporter txtExporter = newJRTextExporter();
txtExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
txtExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
txtExporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, newInteger(80));
txtExporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, newInteger(25));
try{
txtExporter.exportReport();
logger.debug("Genertate the TXT report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the TXT report file ERROR!");
}
}
/*
* 导出单个报表为XML
* @param JasperPrint,OutputStream
*/
publicvoidexportReportToXML(JasperPrint jasperPrint,OutputStreamout){
try{
JasperExportManager.exportReportToXmlStream(jasperPrint, out);
logger.debug("Genertate the XML report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the XML report file ERROR!");
}
}
}
然后写了一个Control类用于封装.jasper文件,并获取其对象,代码如下:
packagecommon.ireport;
importnet.sf.jasperreports.engine.*;
importnet.sf.jasperreports.engine.export.*;
importnet.sf.jasperreports.engine.export.JRRtfExporter;
importnet.sf.jasperreports.engine.util.*;
importnet.sf.jasperreports.view.*;
importjava.util.*;
importjava.util.List;
importjava.sql.*;
importjava.io.*;
import java.net.URL;
/**
*导出报表控制类*<p>Title:导出报表控制类</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2007</p>
*<font class=companyfont>
*CES
*</font>
*@author 野谷子
*@version 1.0.2007.02.14
*/
publicclassJsperReportControl {
privateLogger logger= newLogger("common.ireport.JsperReportControl");
privateQuery query= newQuery();
//定义报表模版路径
privateString PREFIX="/ireportFile/";
//定义后缀名
privateString SUFFIX=".jasper";
publicJsperReportControl(){
}
/*
* 取得报表模版的对象
* 单个参数的情况
* @param 报表名称:strFileName
* @param 参数名称:strParamName
* @param 参数值:strParamValue
* @return JasperPrint
*/
publicJasperPrint getJsperPrint(String strFileName,String strParamName,String strParamValue) throwsSQLException{
JasperPrint jasperPrint = null;
String strJasper= this.getClassPath() + PREFIX + strFileName + SUFFIX ;
Map params = newHashMap();
params.put(strParamName,strParamValue);
//定义数据源连接对象
Connection conn = null;
query.createConnection(CommonManage.CONN);
conn = query.getConnection();
try{
//用JasperFillManager类导入.jasper文件,参数和数据源
jasperPrint = JasperFillManager.fillReport(strJasper, params, conn);
try{
if(conn==null){
conn.close();
}
}catch(SQLException sqle){
logger.error("关闭数据库连接错误"+sqle);
}
}catch(Exception e){
logger.error("getJsperPrint(String strFileName,String strParamName,String strParamValue) Error"+e);
}
returnjasperPrint;
}
/**
*在类中取得当前文件所在的相对路径与绝对路径
*
*@return String
*/
publicStringgetClassPath(){
StringstrClassName=getClass().getName();
StringstrPackageName="";
StringstrURL= "";
if(getClass().getPackage()!=null)
{
strPackageName=getClass().getPackage().getName();
}
//System.out.println("ClassName:"+strClassName);
//System.out.println("PackageName:"+strPackageName);
StringstrClassFileName="";
if(!"".equals(strPackageName))
{
strClassFileName=strClassName.substring(strPackageName.length()+1,strClassName.length());
}
else
{
strClassFileName=strClassName;
}
//System.out.println("ClassFileName:"+strClassFileName);
String strTempName = strClassFileName+".class";
try
{
URLurl=null;
url=getClass().getResource(strClassFileName+".class");
strURL=url.toString();
System.out.println(strURL);
logger.info("strURL="+strURL);
}
catch(Exceptionex)
{
ex.printStackTrace();
}
//取.jasper文件的绝对路径
strURL= strURL.substring("file:".length(),strURL.length()-strTempName.length());
System.out.println("strURL="+strURL);
returnstrURL;
}
}
期间在取绝对路径时出现了问题,在本机测试可以通过,但放在服务器上报错,原因是本机是windows系统,而服务器是linux系统,到现在我觉得最大的原因是getClass().getResource()这个方法在linux系统上取不到资源的位置而报错。
最后就是所写的 jsp 页面了 , 代码如下:
<%@ page import="java.util.Vector"%>
<%@ page import="java.io.*" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.util.List"%>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="common.ireport.JsperReportControl"%>
<%@ page import="common.ireport.JasperReport"%>
<%
String strMode = WebUtil.getParam(request,"mode").equals("")?"":WebUtil.getParam(request,"mode");
JsperReportControl jsperReportControl = newJsperReportControl();
JasperReport jasperReport = newJasperReport();
strFileName1 ="quanlitySupervise_page1";
strFileName2 ="quanlitySupervise_page2";
strFileName3 ="quanlitySupervise_page3";
strFileName4 ="quanlitySupervise_page4";
strFileName5 ="quanlitySupervise_page5";
strFileName6 ="quanlitySupervise_page6";
strFileName7 ="quanlitySupervise_page7";
strFileName8 ="quanlitySupervise_page8";
String strParamName="ID";
String strParamValue=strProjectID;
ByteArrayOutputStream baos = newByteArrayOutputStream();
String strQueryConditionTmp="projectName="+strProjectName;
QueryCtrl queryCtrl = newQueryCtrl();
String str="";
if(strMode.equals("out")){
JasperPrint jasperPrint1= jsperReportControl.getJsperPrint(strFileName1,strParamName,strParamValue);
JasperPrint jasperPrint2= jsperReportControl.getJsperPrint(strFileName2,strParamName,strParamValue);
JasperPrint jasperPrint3= jsperReportControl.getJsperPrint(strFileName3,strParamName,strParamValue);
JasperPrint jasperPrint4= jsperReportControl.getJsperPrint(strFileName4,strParamName,strParamValue);
JasperPrint jasperPrint5= jsperReportControl.getJsperPrint(strFileName5,strParamName,strParamValue);
JasperPrint jasperPrint6= jsperReportControl.getJsperPrint(strFileName6,strParamName,strParamValue);
JasperPrint jasperPrint7= jsperReportControl.getJsperPrint(strFileName7,strParamName,strParamValue);
JasperPrint jasperPrint8= jsperReportControl.getJsperPrint(strFileName8,strParamName,strParamValue);
List reportlist = (List)newjava.util.ArrayList();
reportlist.add(jasperPrint1);
reportlist.add(jasperPrint2);
reportlist.add(jasperPrint3);
reportlist.add(jasperPrint4);
reportlist.add(jasperPrint5);
reportlist.add(jasperPrint6);
reportlist.add(jasperPrint7);
reportlist.add(jasperPrint8);
//导出单页测试用
//jasperReport.exportReportToRTF(jasperPrint1,baos);
//整合多页导出
jasperReport.exportReportListToRTF(reportlist,baos);
byte[] bytes;
bytes = baos.toByteArray();
response.setContentType("application/ms_word");
String strDocName ="attachment;filename="+strProjectID+".doc";
response.setHeader("Content-disposition",strDocName);
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
}
%>
结尾:在本机测试通过了,但放于服务器出现了错误: Can't connect to X11 window server using':0.0' as the value of the DISPLAY
而在 windows 系统中却能正确运行,原因是程序调用了图形界面的功能,而 windows 很好的支持图形界面, linux 却不能很好的支持,需要修改 linux 中的 DISPLAY 的变量:
下面有几种方法:
1. 对于 tomcat 服务器:找到脚本: catalina.sh ,
在 /usr/local/tomcat/bin/catalina.sh 中修改为
JAVA_OPTS="-Djava.awt.headless=true" 或者 CATALINA_OPTS="-Djava.awt.headless=true"
echo "Using CATALINA_BASE:$CATALINA_BASE"
echo "Using CATALINA_HOME:$CATALINA_HOME"
echo "Using CATALINA_TMPDIR:$CATALINA_TMPDIR"
echo "Using RUNJAVA:$_RUNJAVA"
echo "Using JAVA_OPTS:$JAVA_OPTS"
2 .对于服务器中 oracle 自带的 JSP server ,找到 jserv.properties 这个配置文件,在上面添加:
wrapper.env=DISPLAY=<SERVERNAME>:0
3 .或者用root身份执行一下xhost +命令,
第一种情况:DISPLAY在远程数据库中没有正确定义,需要:
set DISPLAY=localComputer:0
export DISPLAY(取决于shell的不同而命令不同)
第二种情况:图形界面的功能不能在远程登陆的用户中定义和使用,需要:
使用oracle用户登录xwindow:应用xhost +命令
哥们你看我说的对你有帮助嘛我就知道那么多了
要将.jsper文件应用在项目中,首先要做几样准备工作:
1.将安装好的Ireport,找到它下面的lib目录,再把jasperreports-1.3.0.jar这个包复制出来(这个包是JasperReport导出的关键),然后将这个包导入到需要应用的项目中,然后在eclipse中重新构建整个项目,这是最关键的第一步。
2.如果导出出现乱码的问题,需要引入iText亚洲语言包,可以在 http://www.lowagie.com/iText/ 查看关于iText的相关信息,包括源代码,文档.
3.设计好报表,将报表编译为.jsper文件,这些文件在iReport的安装路径下,很容易找到。
现在万事俱备,只欠东风了。我在开始写程序的的时候,在网上找了好些代码,而导出的过程中用到的类 JRRtfExporter 的源代码也踩过,并且发现了 http://www.koders.com 这个非常好的网站,这是开源项目的必备,在上面基本上很多类的源码也能查到。
接着自己封装了一个JasperReport类,而这个类可以实现导出各种格式的文件,代码如下:
packagecommon.ireport;
importnet.sf.jasperreports.engine.*;
importnet.sf.jasperreports.engine.export.*;
importnet.sf.jasperreports.engine.export.JRRtfExporter;
importjava.util.List;
importjava.io.*;
import com.log.Logger;
/**
*IReport+JasperReport导出报表类接口
*<p>Title:导出报表类</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2007</p>
*<font class=companyfont>
*CES
*</font>
*@author 野谷子
*@version 1.0.2007.02.14
*/
publicclassJasperReport{
privateLogger logger= newLogger("common.ireport.JasperReport");
/**CreatesanewinstanceofMyReport*/
publicJasperReport(){
}
/*
* 导出单个报表为rtf或word
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("application/msword");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.doc");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToRTF(JasperPrint jasperPrint,OutputStreamout){
JRRtfExporter rtfExporter= newJRRtfExporter();
rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
try{
rtfExporter.exportReport();
logger.debug("Genertate the RTF report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the RTF report file ERROR!");
}
}
/*
* 导出多个报表为rtf或word
* @param List,OutputStream
* @ 页面设置response.setContentType("application/msword");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.doc");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportListToRTF(List jasperPrints,OutputStream out){
JRRtfExporter rtfExporter= newJRRtfExporter();
rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints);
rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
try{
rtfExporter.exportReport();
logger.debug("Genertate the mutiply RTF report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the mutiply RTF report file ERROR!");
}
}
/*
* 导出单个报表为PDF
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("application/pdf");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.pdf");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToPDF(JasperPrint jasperPrint,OutputStreamout){
JRPdfExporter pdfExporter = newJRPdfExporter();
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
pdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
try{
pdfExporter.exportReport();
logger.debug("Genertate the PDF report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the PDF report file ERROR!");
}
}
/*
* 导出单个报表为XLS
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("application/vnd.ms-excel");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.xls");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToXLS(JasperPrint jasperPrint,OutputStreamout){
JRXlsExporter xlsExporter= newJRXlsExporter();
xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
try{
xlsExporter.exportReport();
logger.debug("Genertate the XLS report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the XLS report file ERROR!");
}
}
/*
* 导出单个报表为HTML
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("text/html");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.html");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToHTML(JasperPrint jasperPrint,OutputStreamout){
JRHtmlExporter htmlExporter = newJRHtmlExporter();
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
htmlExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
htmlExporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.FALSE);
try{
htmlExporter.exportReport();
logger.debug("Genertate the HTML report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the HTML report file ERROR!");
}
}
/*
* 导出单个报表为CSV
* @param JasperPrint,OutputStream
*/
publicvoidexportReportToCSV(JasperPrint jasperPrint,OutputStreamout){
JRCsvExporter csvExporter = newJRCsvExporter();
csvExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
csvExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
try{
csvExporter.exportReport();
logger.debug("Genertate the CSV report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the CSV report file ERROR!");
}
}
/*
* 导出单个报表为TXT
* @param JasperPrint,OutputStream
* @ 页面设置response.setContentType("text/html");
* @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.txt");
* @ 页面设置response.setContentLength(bytes.length);
*/
publicvoidexportReportToTXT(JasperPrint jasperPrint,OutputStreamout){
JRTextExporter txtExporter = newJRTextExporter();
txtExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
txtExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
txtExporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, newInteger(80));
txtExporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, newInteger(25));
try{
txtExporter.exportReport();
logger.debug("Genertate the TXT report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the TXT report file ERROR!");
}
}
/*
* 导出单个报表为XML
* @param JasperPrint,OutputStream
*/
publicvoidexportReportToXML(JasperPrint jasperPrint,OutputStreamout){
try{
JasperExportManager.exportReportToXmlStream(jasperPrint, out);
logger.debug("Genertate the XML report ok! :"+ out);
} catch(JRException e) {
e.printStackTrace();
logger.error("Generate the XML report file ERROR!");
}
}
}
然后写了一个Control类用于封装.jasper文件,并获取其对象,代码如下:
packagecommon.ireport;
importnet.sf.jasperreports.engine.*;
importnet.sf.jasperreports.engine.export.*;
importnet.sf.jasperreports.engine.export.JRRtfExporter;
importnet.sf.jasperreports.engine.util.*;
importnet.sf.jasperreports.view.*;
importjava.util.*;
importjava.util.List;
importjava.sql.*;
importjava.io.*;
import java.net.URL;
/**
*导出报表控制类*<p>Title:导出报表控制类</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2007</p>
*<font class=companyfont>
*CES
*</font>
*@author 野谷子
*@version 1.0.2007.02.14
*/
publicclassJsperReportControl {
privateLogger logger= newLogger("common.ireport.JsperReportControl");
privateQuery query= newQuery();
//定义报表模版路径
privateString PREFIX="/ireportFile/";
//定义后缀名
privateString SUFFIX=".jasper";
publicJsperReportControl(){
}
/*
* 取得报表模版的对象
* 单个参数的情况
* @param 报表名称:strFileName
* @param 参数名称:strParamName
* @param 参数值:strParamValue
* @return JasperPrint
*/
publicJasperPrint getJsperPrint(String strFileName,String strParamName,String strParamValue) throwsSQLException{
JasperPrint jasperPrint = null;
String strJasper= this.getClassPath() + PREFIX + strFileName + SUFFIX ;
Map params = newHashMap();
params.put(strParamName,strParamValue);
//定义数据源连接对象
Connection conn = null;
query.createConnection(CommonManage.CONN);
conn = query.getConnection();
try{
//用JasperFillManager类导入.jasper文件,参数和数据源
jasperPrint = JasperFillManager.fillReport(strJasper, params, conn);
try{
if(conn==null){
conn.close();
}
}catch(SQLException sqle){
logger.error("关闭数据库连接错误"+sqle);
}
}catch(Exception e){
logger.error("getJsperPrint(String strFileName,String strParamName,String strParamValue) Error"+e);
}
returnjasperPrint;
}
/**
*在类中取得当前文件所在的相对路径与绝对路径
*
*@return String
*/
publicStringgetClassPath(){
StringstrClassName=getClass().getName();
StringstrPackageName="";
StringstrURL= "";
if(getClass().getPackage()!=null)
{
strPackageName=getClass().getPackage().getName();
}
//System.out.println("ClassName:"+strClassName);
//System.out.println("PackageName:"+strPackageName);
StringstrClassFileName="";
if(!"".equals(strPackageName))
{
strClassFileName=strClassName.substring(strPackageName.length()+1,strClassName.length());
}
else
{
strClassFileName=strClassName;
}
//System.out.println("ClassFileName:"+strClassFileName);
String strTempName = strClassFileName+".class";
try
{
URLurl=null;
url=getClass().getResource(strClassFileName+".class");
strURL=url.toString();
System.out.println(strURL);
logger.info("strURL="+strURL);
}
catch(Exceptionex)
{
ex.printStackTrace();
}
//取.jasper文件的绝对路径
strURL= strURL.substring("file:".length(),strURL.length()-strTempName.length());
System.out.println("strURL="+strURL);
returnstrURL;
}
}
期间在取绝对路径时出现了问题,在本机测试可以通过,但放在服务器上报错,原因是本机是windows系统,而服务器是linux系统,到现在我觉得最大的原因是getClass().getResource()这个方法在linux系统上取不到资源的位置而报错。
最后就是所写的 jsp 页面了 , 代码如下:
<%@ page import="java.util.Vector"%>
<%@ page import="java.io.*" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.util.List"%>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="common.ireport.JsperReportControl"%>
<%@ page import="common.ireport.JasperReport"%>
<%
String strMode = WebUtil.getParam(request,"mode").equals("")?"":WebUtil.getParam(request,"mode");
JsperReportControl jsperReportControl = newJsperReportControl();
JasperReport jasperReport = newJasperReport();
strFileName1 ="quanlitySupervise_page1";
strFileName2 ="quanlitySupervise_page2";
strFileName3 ="quanlitySupervise_page3";
strFileName4 ="quanlitySupervise_page4";
strFileName5 ="quanlitySupervise_page5";
strFileName6 ="quanlitySupervise_page6";
strFileName7 ="quanlitySupervise_page7";
strFileName8 ="quanlitySupervise_page8";
String strParamName="ID";
String strParamValue=strProjectID;
ByteArrayOutputStream baos = newByteArrayOutputStream();
String strQueryConditionTmp="projectName="+strProjectName;
QueryCtrl queryCtrl = newQueryCtrl();
String str="";
if(strMode.equals("out")){
JasperPrint jasperPrint1= jsperReportControl.getJsperPrint(strFileName1,strParamName,strParamValue);
JasperPrint jasperPrint2= jsperReportControl.getJsperPrint(strFileName2,strParamName,strParamValue);
JasperPrint jasperPrint3= jsperReportControl.getJsperPrint(strFileName3,strParamName,strParamValue);
JasperPrint jasperPrint4= jsperReportControl.getJsperPrint(strFileName4,strParamName,strParamValue);
JasperPrint jasperPrint5= jsperReportControl.getJsperPrint(strFileName5,strParamName,strParamValue);
JasperPrint jasperPrint6= jsperReportControl.getJsperPrint(strFileName6,strParamName,strParamValue);
JasperPrint jasperPrint7= jsperReportControl.getJsperPrint(strFileName7,strParamName,strParamValue);
JasperPrint jasperPrint8= jsperReportControl.getJsperPrint(strFileName8,strParamName,strParamValue);
List reportlist = (List)newjava.util.ArrayList();
reportlist.add(jasperPrint1);
reportlist.add(jasperPrint2);
reportlist.add(jasperPrint3);
reportlist.add(jasperPrint4);
reportlist.add(jasperPrint5);
reportlist.add(jasperPrint6);
reportlist.add(jasperPrint7);
reportlist.add(jasperPrint8);
//导出单页测试用
//jasperReport.exportReportToRTF(jasperPrint1,baos);
//整合多页导出
jasperReport.exportReportListToRTF(reportlist,baos);
byte[] bytes;
bytes = baos.toByteArray();
response.setContentType("application/ms_word");
String strDocName ="attachment;filename="+strProjectID+".doc";
response.setHeader("Content-disposition",strDocName);
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
}
%>
结尾:在本机测试通过了,但放于服务器出现了错误: Can't connect to X11 window server using':0.0' as the value of the DISPLAY
而在 windows 系统中却能正确运行,原因是程序调用了图形界面的功能,而 windows 很好的支持图形界面, linux 却不能很好的支持,需要修改 linux 中的 DISPLAY 的变量:
下面有几种方法:
1. 对于 tomcat 服务器:找到脚本: catalina.sh ,
在 /usr/local/tomcat/bin/catalina.sh 中修改为
JAVA_OPTS="-Djava.awt.headless=true" 或者 CATALINA_OPTS="-Djava.awt.headless=true"
echo "Using CATALINA_BASE:$CATALINA_BASE"
echo "Using CATALINA_HOME:$CATALINA_HOME"
echo "Using CATALINA_TMPDIR:$CATALINA_TMPDIR"
echo "Using RUNJAVA:$_RUNJAVA"
echo "Using JAVA_OPTS:$JAVA_OPTS"
2 .对于服务器中 oracle 自带的 JSP server ,找到 jserv.properties 这个配置文件,在上面添加:
wrapper.env=DISPLAY=<SERVERNAME>:0
3 .或者用root身份执行一下xhost +命令,
第一种情况:DISPLAY在远程数据库中没有正确定义,需要:
set DISPLAY=localComputer:0
export DISPLAY(取决于shell的不同而命令不同)
第二种情况:图形界面的功能不能在远程登陆的用户中定义和使用,需要:
使用oracle用户登录xwindow:应用xhost +命令
哥们你看我说的对你有帮助嘛我就知道那么多了
2011-07-27
展开全部
我自己解决了,在区哉语言选项里面,把非Unicode程序使用语言由中文(简体)改为英语(美国),重启。这样问题虽然解决了,但是有些地方又乱码了,比如DOS中。
另外,站长团上有产品团购,便宜有保证
另外,站长团上有产品团购,便宜有保证
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-07-27
展开全部
我自己解决了,在区哉语言选项里面,把非Unicode程序使用语言由中文(简体)改为英语(美国),重启。这样问题虽然解决了,但是有些地方又乱码了,比如DOS中。
另外,虚机团上产品团购,超级便宜
另外,虚机团上产品团购,超级便宜
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询