josepco Posted January 27, 2007 Share Posted January 27, 2007 Hi all,I've to export a changing table from a web site to a .xls file through a hashmap.So no static .jrxml and no database query possible.I tried to create a Parameter section, then to use the $P{..} in the detail section, but all is blank.Then I tried to directly input values as staticText, but still blank (even in .pdf!). If anyone has any suggestion or idea I would really appriciate as I'm stuck with this and new user of JasperReport. Thanks. Here is my JasperDesign:public static JasperDesign CreateJaperDesign(){try { jasperDesign = new JasperDesign(); jasperDesign.setName("SampleReport"); jasperDesign.setPageWidth(595); jasperDesign.setPageHeight(842); jasperDesign.setColumnWidth(555); jasperDesign.setColumnSpacing(0); jasperDesign.setLeftMargin(20); jasperDesign.setRightMargin(20); jasperDesign.setTopMargin(30); jasperDesign.setBottomMargin(30); //Set the JasperReports style JRDesignStyle normalStyle = new JRDesignStyle(); normalStyle.setName("Arial_Normal"); normalStyle.setDefault(true); normalStyle.setFontName("Arial"); normalStyle.setFontSize(12); normalStyle.setPdfFontName("Helvetica"); normalStyle.setPdfEncoding("Cp1252"); normalStyle.setPdfEmbedded(false); jasperDesign.addStyle(normalStyle); JRDesignStyle boldStyle = new JRDesignStyle(); boldStyle.setName("Arial_Bold"); boldStyle.setFontName("Arial"); boldStyle.setFontSize(12); boldStyle.setBold(true); boldStyle.setPdfFontName("Helvetica-Bold"); boldStyle.setPdfEncoding("Cp1252"); boldStyle.setPdfEmbedded(false); jasperDesign.addStyle(boldStyle); //Parameters int paramSize = param.size(); //param = HashMap (1st Object = title, then values) String[] value = new String[paramSize]; Class[] classObject = new Class[paramSize]; ///JRDesignParameter parameter; for(int i=0;i<paramOrder.length;i++){ Object o = param.get(paramOrder); classObject = o.getClass(); value = o.toString(); } //Title JRDesignBand band = new JRDesignBand(); band.setHeight(50); JRDesignStaticText staticTitle = new JRDesignStaticText(); staticTitle.setX(280); staticTitle.setY(0); staticTitle.setWidth(300); staticTitle.setHeight(50); staticTitle.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER); staticTitle.setStyle(normalStyle); staticTitle.setFontSize(22); staticTitle.setText(value[0]);//"$P{ReportTitle}"); band.addElement(staticTitle); jasperDesign.setTitle(band); //Page header band = new JRDesignBand(); jasperDesign.setPageHeader(band); //Column header band = new JRDesignBand(); band.setHeight(20); JRDesignStaticText staticText; for(int j=0; j<elementPerLine; j++){ staticText = new JRDesignStaticText(); staticText.setX(150*j); staticText.setY(0); staticText.setWidth(125); staticText.setHeight(20); //15 staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT); staticText.setStyle(boldStyle); staticText.setText(value[j+1]); band.addElement(staticText); } jasperDesign.setColumnHeader(band); //Detail band = new JRDesignBand(); int row = (paramSize-1)/elementPerLine; band.setHeight(20*(row-1)); JRDesignStaticText staticDesign; for(int i=1; i<row; i++){ for(int j=0; j<elementPerLine; j++){ //textField = new JRDesignTextField(); staticDesign = new JRDesignStaticText(); staticDesign.setX(150*j); staticDesign.setY(20*(i-1)); staticDesign.setWidth(125); staticDesign.setHeight(20); //15 staticDesign.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT); staticDesign.setStyle(normalStyle); staticDesign.setText(value[elementPerLine*i+(j+1)]); band.addElement(staticDesign); } } jasperDesign.setDetail(band); //Column footer band = new JRDesignBand(); jasperDesign.setColumnFooter(band); //Page footer band = new JRDesignBand(); jasperDesign.setPageFooter(band); //Summary band = new JRDesignBand(); jasperDesign.setSummary(band); } catch (JRException e) { e.printStackTrace();}} ---------------------------------------- Here the implementation:JasperDesign design = CreateJaperDesign();JasperReport report = JasperCompileManager.compileReport(design); JasperPrint print = JasperFillManager.fillReport(report, null); //not filling data 'cause already filledJRXlsExporter exporterXLS = new JRXlsExporter();ByteArrayOutputStream xlsReport = new ByteArrayOutputStream(); exporterXLS.setParameter(JRExporterParameter.JASPER_PRINT, print); exporterXLS.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport); exporterXLS.exportReport();OutputStream ouputStream = new FileOutputStream(new File(outputFilePath)); ouputStream.write(xlsReport.toByteArray(),0,xlsReport.toByteArray().length);ouputStream.flush();ouputStream.close(); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now