我在struts2下用jasperreport时候,html显示总是很多红叉,查了些网上的解决方案,比如加入px.gif都不能解决。看到还有一个方案是改源码,请问怎么样修改。
另外,在网页生成excel格式显示时候,我的表格等也都乱了,如果选择另存为保存,下载后打开excel后也是这样,关闭时还会报错,无法正常关闭excel,请问下是怎么样解决。
另外,在网页生成excel格式显示时候,我的表格等也都乱了,如果选择另存为保存,下载后打开excel后也是这样,关闭时还会报错,无法正常关闭excel,请问下是怎么样解决。
解决方案 »
- Error occured during reequest processing: Java heap space
- struts中涉及到多表的建立几个actionform?
- ajax与DispatchAction问题大家帮帮忙
- 难道CSDN上就没有人能回答这个问题吗?
- 请问怎样输出总和?
- web-inf 目录下的jsp引用外部文件css问题
- 为何sqlserver数据库中ntext数据类型中字段值为中文时显示不正常?
- 菜菜送分---jsp中页面刷新
- 关于连接池的应用,我看不懂它到底是连接的哪里的服务器和数据库?是在程序里写还是外部设置的?
- session的传值问题
- struts2的一个action对应多个success result的问题
- jive回复的附件不是链接?
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);
但是你用的struts2好像已经被封装了,看看怎么设置了哦,没用过,不过jasperreport出的excel报表效果都不敢恭维,看看有没有高手有宝贵的建议了
------------------------------------------------------------------------------------------------------------pdf-----------------------------------------------------------------------------------------------------------------:sSQL = String.format(sSQL, sWhere);
Connection con = null;
String strMessage="";
ResultSet rs=null;
//String path1=request.getContextPath();
//String path2="E:\\workspace\\IreportDemo\\WebRoot\\report\\SecondDemo.jasper";
String path= request.getRealPath("/reports/jb7.jasper");
System.out.println(path);
File reportFile = new File(path);
byte[] bytes=new byte[200];
try {
con = DBConnection.currentConnection();
Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// ResultSet rs=DBConnection.getConnection();
rs=stat.executeQuery(sSQL);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
JRResultSetDataSource ds = new JRResultSetDataSource(rs);
System.out.println("-------------------------------------------------------------");
try {
System.out.println(ds.next());
} catch (JRException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("-------------------------------------------------------------");
try {
//生成pdf文件
bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),null, ds);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//pdf设置
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream;
try {
ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.write(0);
ouputStream.flush();
ouputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null; ------------------------------------------------------------------------------------------------------------html-----------------------------------------------------------------------------------------------------------------:
sSQL = String.format(sSQL, sWhere);
Connection con = null;
String strMessage="";
ResultSet rs=null;
response.setCharacterEncoding("UTF-8");
File reportFile = new File(request.getRealPath("/reports/jb7.jasper"));
if (!reportFile.exists())
throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first."); JasperReport jasperReport = null;
try {
jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
} catch (JRException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Map parameters = new HashMap();
parameters.put("ReportTitle", "Address Report");
parameters.put("BaseDir", reportFile.getParentFile());
try
{
con = DBConnection.currentConnection();
Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=stat.executeQuery(sSQL);
System.out.println(rs.next());
JRResultSetDataSource jrds = new JRResultSetDataSource(rs);
System.out.println("--------------------------------------------------");
System.out.println(jrds.next());
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
jrds
);
response.setContentType("text/html");
JRHtmlExporter exporter = new JRHtmlExporter();
PrintWriter out=response.getWriter();
request.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/test/images/px.");
exporter.exportReport();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;------------------------------------------------------------------------------------------------------------excel-----------------------------------------------------------------------------------------------------------------:sSQL = String.format(sSQL, sWhere);
Connection con = null;
String strMessage="";
ResultSet rs=null;
//html版本方法
//String path1=request.getContextPath();
//将文件保存在工程目录下,会有bug,必须先生成,然后才能做转向跳转,那么就不同步。
//String path="E:\\workspace\\test\\WebRoot\\reports\\jb7.jasper";
//System.out.println(path);
//现在考虑将文件保存在服务器端
String path= request.getRealPath("/reports/jb7.jasper");
System.out.println(path);
File reportFile = new File(path);
try {
con = DBConnection.currentConnection();
Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=stat.executeQuery(sSQL);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JRDataSource dataSource=new JRResultSetDataSource(rs);
try {
System.out.println(dataSource.next());
} catch (JRException e1) {
e1.printStackTrace();
}
//生成EXCEL
try {
Map parameters = new HashMap();
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(),null,dataSource);
JRXlsExporter exporter = new JRXlsExporter();
byte[] bytes;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); // 删除记录最下面的空行
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);// 删除多余的ColumnHeader
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);// 显示边框
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,baos);
exporter.exportReport();
bytes = baos.toByteArray();
if (bytes != null && bytes.length > 0){
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel;charset=GB2312");
response.setHeader("Content-disposition","attachment; filename=\"" + URLEncoder.encode("jb7", "UTF-8") + ".xls\"");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//跳转
return null;