Advertisement
Guest User

Untitled

a guest
Apr 14th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. <%@ page import="java.io.*"%>
  2. <%@ page import="java.sql.Connection"%>
  3. <%@ page import="java.sql.DriverManager"%>
  4. <%@ page import="java.util.HashMap"%>
  5. <%@ page import="java.util.Map"%>
  6. <%@ page import="net.sf.jasperreports.engine.*"%>
  7. <%@ page import="java.io.ByteArrayOutputStream"%>
  8. <%@ page import="net.sf.jasperreports.view.JasperViewer"%>
  9. <%@ page import="net.sf.jasperreports.engine.export.*"%>
  10.  
  11. <%
  12. Connection conn = null;
  13. String no1 = request.getParameter("no1");
  14. String no2 = request.getParameter("no2");
  15.  
  16. System.out.println("get value " + no1 + " " +no2);
  17. try {
  18. Class.forName("com.mysql.jdbc.Driver").newInstance();
  19. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ams2"
  20. ,"root","passwd1234");
  21. File reportFile = new File (application.getRealPath("//jasper//report//Blank_A4_2.jasper"));
  22. Map parameters = new HashMap();
  23.  
  24. parameters.put("no1",no1);
  25. parameters.put("no2",no2);
  26. System.out.println("123 "+parameters);
  27.  
  28. ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
  29. JRXlsExporter exporter = new JRXlsExporter();
  30. exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
  31. exporter.setParameter(JRExporterParameter.OUTPUT_FILE, "C:\");
  32. exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "sample.xls");
  33. exporter.exportReport();
  34.  
  35. byte bytes[] = new byte[10];
  36. bytes = xlsReport.toByteArray();
  37. response.setContentType("application/vnd.ms-excel");
  38.  
  39. response.setContentLength(bytes.length);
  40. xlsReport.close();
  41. ServletOutputStream outStream = response.getOutputStream();
  42. outStream.write(bytes,0,bytes.length);
  43. outStream.flush();
  44. outStream.close();
  45.  
  46. } catch (Exception ex) {
  47. out.println("Error " + ex);
  48. }
  49. %>
  50.  
  51. net.sf.jasperreports.engine.JRRuntimeException: No input source supplied to the exporter.
  52.  
  53. JasperDesign jasperDesign = JRXmlLoader.load(new FileInputStream(reportFile));
  54. JasperPrint jasperPrint = JasperFillManager.fillReport(jasperDesign, parameters, conn);
  55.  
  56. JRXlsExporter exporter = new JRXlsExporter();
  57. exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); //The JasperPrint, filled report
  58. exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(xlsReport)); //Your ByteArrayOutputStream
  59.  
  60. SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
  61. configuration.setOnePagePerSheet(true);
  62. configuration.setDetectCellType(true);
  63. configuration.set //The other properties you like to set
  64. exporter.setConfiguration(configuration);
  65.  
  66. exporter.exportReport();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement