3

JapserReports用于在 Java 中显示报告。我能够以PDF格式向网络浏览器发送报告。

现在我想以HTML格式发送报告,以下是我以HTML格式发送报告的方法。

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    File reportFile = new File(getServletConfig().getServletContext().getRealPath("/rpts/report1.jasper"));
    ServletOutputStream servletOutputStream = response.getOutputStream();
    byte[] bytes = null;
    Map<String,Object> parameter = new HashMap<String,Object>();
    Connection con = DBConnection.getConnection("");
    try
    {
        bytes = JasperRunManager.runReportToHtmlFile(reportFile.getPath(),parameter,con).getBytes();
        response.setContentType("text/html");
        response.setContentLength(bytes.length);
        servletOutputStream.write(bytes, 0, bytes.length);
        servletOutputStream.flush();
        servletOutputStream.close();
    }
    catch (JRException e)
    {
        System.out.println(e);
    }
}

以上方法是打印path生成的HTML文件。当我检查路径时,会HTML在那里生成带有数据的文件。

那么我在代码中遗漏了什么吗?

提前致谢....

4

2 回答 2

3

尝试设置 Content-Disposition 和 html 文件名。

    response.setHeader("Content-Disposition","inline, filename=myReport.html");
    response.setContentType("text/html");
    response.setContentLength(bytes.length);
    servletOutputStream.write(bytes, 0, bytes.length);
    servletOutputStream.flush();
    servletOutputStream.close();

runReportToHtmlFile 方法返回绝对文件路径。在写入输出流之前,需要将生成的 html 文件读入字节数组。

下面的代码可能对你有用。

        String reportPath =JasperRunManager.runReportToHtmlFile(reportFile.getPath(), parameter);
        File reportHtmlFile = new File(reportPath);
        FileInputStream fis = new FileInputStream(reportHtmlFile);
        byte[] bytes =  new byte[(int)reportHtmlFile.length()];
        fis.read(bytes);
        resp.setHeader("Content-Disposition","inline; filename=myReport.html");
        resp.setContentType("text/html");
        resp.setContentLength(bytes.length);
        servletOutputStream.write(bytes, 0, bytes.length);
        servletOutputStream.flush();
        servletOutputStream.close();
于 2013-03-14T12:09:39.720 回答
-1

试试这个

public class ReportExample {



/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ArrayList<City> cityList=new ArrayList<City>();

City city=new City();

city.setCityName("Kolkata");

city.setCountry("Country");

cityList.add(city);

InputStream inputStream;

try {

inputStream = new FileInputStream ("report1.jrxml");

DataBeanMaker dataBeanMaker = new DataBeanMaker();

JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(cityList);

Map parameters = new HashMap();



JasperDesign jasperDesign = JRXmlLoader.load(inputStream);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);

JasperExportManager.exportReportToPdfFile(jasperPrint, "testjasper.pdf"); 

   JasperViewer.viewReport(jasperPrint);

   JasperPrintManager.printReport(jasperPrint, true);

}catch(Exception e)

{e.printStackTrace();

}

}



}

 check the link http://javadispute.com/content/jasper-report-example
于 2013-03-14T18:27:54.957 回答