public void exportToExcel(HttpServletRequest request,
HttpServletResponse response, String jrxmlFilePath,
String reportFilePath, Map params, Connection con) throws Exception {
try {
JasperPrint jasperPrint = new JasperPrintWithConnection(
jrxmlFilePath, reportFilePath, params, con)
.getJasperPrint();
JRXlsExporter jrxlsexporter = new JRXlsExporter();
jrxlsexporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
// 设置输出流
jrxlsexporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
response.getOutputStream());
jrxlsexporter
.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);
jrxlsexporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,
"GB2312"); jrxlsexporter.exportReport();
response.setContentType("application/vnd.ms-excel");
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
} }页面
<%
JDBCConnection jdbcCon=new JDBCConnection();
jdbcCon.setJdbcDriver("com.microsoft.jdbc.sqlserver.SQLServerDriver");
jdbcCon.setJdbcUrl("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test");
jdbcCon.setDbUser("sa");
jdbcCon.setDbPassword("sa");
Connection con=jdbcCon.getConnection();
Map parameters=new HashMap();
parameters.put("rid",4);
HTMLExport exportReport=new HTMLExport();
exportReport.exportToExcel(request,response,request.getRealPath("/myfirst/myexport.jrxml"),request.getRealPath("/myfirst/myexport.jasper"),parameters,con);
%>显示的页面全是乱码,而是我是想excel格式输出,不知道为什么
HttpServletResponse response, String jrxmlFilePath,
String reportFilePath, Map params, Connection con) throws Exception {
try {
JasperPrint jasperPrint = new JasperPrintWithConnection(
jrxmlFilePath, reportFilePath, params, con)
.getJasperPrint();
JRXlsExporter jrxlsexporter = new JRXlsExporter();
jrxlsexporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
// 设置输出流
jrxlsexporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
response.getOutputStream());
jrxlsexporter
.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);
jrxlsexporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,
"GB2312"); jrxlsexporter.exportReport();
response.setContentType("application/vnd.ms-excel");
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
} }页面
<%
JDBCConnection jdbcCon=new JDBCConnection();
jdbcCon.setJdbcDriver("com.microsoft.jdbc.sqlserver.SQLServerDriver");
jdbcCon.setJdbcUrl("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test");
jdbcCon.setDbUser("sa");
jdbcCon.setDbPassword("sa");
Connection con=jdbcCon.getConnection();
Map parameters=new HashMap();
parameters.put("rid",4);
HTMLExport exportReport=new HTMLExport();
exportReport.exportToExcel(request,response,request.getRealPath("/myfirst/myexport.jrxml"),request.getRealPath("/myfirst/myexport.jasper"),parameters,con);
%>显示的页面全是乱码,而是我是想excel格式输出,不知道为什么
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货