Java Code for Storing Data directly in Excel File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davidson1
    New Member
    • Feb 2008
    • 144

    Java Code for Storing Data directly in Excel File

    I have a below code which saves 4 html page separately with data and a empty Excel file. This empty Excel file is dependent on this 4 html page data. Only While Opening this Excel file ,Each 4 html page is loading its data into each 4 spreadsheet of this Empty Excel file. Is there any way to modify this code, so that each 4 html page data will be directly load into Excel file in each 4 spreadsheet. Can you please help me? Please let me know if you have any questions? If you know Please modify this below code. I will try in my end.

    Code:
    public void RunReportAndSaveExcelOutput(String reportSearchPath, String saveLocation) 
    	{		
    		
    
                    Option runOptions[] = new Option[5];
                    
    	//runOptions = new option[5];
    
    		RunOptionBoolean saveOutput = new RunOptionBoolean();
    		saveOutput.setName(RunOptionEnum.saveOutput);
    		saveOutput.setValue(false);
    		runOptions[0] = saveOutput;
    	
    		RunOptionStringArray outputFormat = new RunOptionStringArray();
    		outputFormat.setName(RunOptionEnum.outputFormat);
    		outputFormat.setValue(new String[]{"XLS" });
    		runOptions[1] = outputFormat;
    	
    		RunOptionBoolean prompt = new RunOptionBoolean();
    		prompt.setName(RunOptionEnum.prompt);
    		prompt.setValue(false);
    		runOptions[2] = prompt;
    			
    		RunOptionInt primaryWaitThreshold = new RunOptionInt();
    		primaryWaitThreshold.setName(RunOptionEnum.primaryWaitThreshold);
    		primaryWaitThreshold.setValue(0);
    		runOptions[3] = primaryWaitThreshold;
    
    		RunOptionInt secondaryWaitThreshold = new RunOptionInt();
    		secondaryWaitThreshold.setName(RunOptionEnum.secondaryWaitThreshold);
    		secondaryWaitThreshold.setValue(0);
    		runOptions[4] = secondaryWaitThreshold;
    	        
    		SearchPathSingleObject repPath = new SearchPathSingleObject();
    		repPath.setValue( reportSearchPath );
    		
    		
    try
    {
    
    AsynchReply res = getReportService().run(repPath, new ParameterValue[]{}, runOptions);
    
    		// If it has not yet completed, keep waiting until it is done.
    		int count = 1;
    		while(res.getStatus() != AsynchReplyStatusEnum.complete &&
    				res.getStatus() != AsynchReplyStatusEnum.conversationComplete)
    		{
    			System.out.println("large report - waiting..." + count);
    			res = getReportService().wait(res.getPrimaryRequest(), new ParameterValue[]{}, new Option[]{});
    			count++;
    		}
    
    
    		if (res.getStatus().equals(AsynchReplyStatusEnum.complete))
    		{
    			AsynchDetailReportOutput reportOutput = null;
    System.out.println(res.getDetails().length);
    			
    			for (int i = 0; i < res.getDetails().length; i++)
    			{
    				if (res.getDetails()[i] instanceof AsynchDetailReportOutput)
    				{
    					reportOutput = (AsynchDetailReportOutput)res.getDetails()[i];
    					break;
    				}
    			}
    
    			String excelFile = null;
    			String[] data = reportOutput.getOutputPages();
    			excelFile = data[0];
    			//look for output pages of the excel report.
    			BaseClass bcOutput[] = reportOutput.getOutputObjects();
    
    				
    			if (bcOutput != null && bcOutput.length > 0) 
    			{	
    				Output latestOutput = (Output)bcOutput[bcOutput.length - 1];
    							
    				PropEnum [] pageProperties = new PropEnum [] {PropEnum.searchPath,PropEnum.defaultName,														  PropEnum.data};
    				SearchPathMultipleObject outPage = new SearchPathMultipleObject();
    				outPage.setValue(latestOutput.getSearchPath().getValue() + "//page");
                                    BaseClass[] bcPage = new BaseClass[100000];
    					
    				bcPage = cmService.query(outPage, pageProperties, new Sort[]{}, new QueryOptions() );
    			
    
    				if (bcPage != null)
    				{	
    					for (int i = 0; i < bcPage.length; i++)
    					{	
    						Page dataPage = (Page)bcPage[i];
    						dataPageName = dataPage.getDefaultName().getValue(); 
                                                    //String dataPageName1 = dataPage.getDefaultName() + "_" + day + "-" + (month + 1) + "-" + year;
    						String saveDataPageFile = saveLocation + "/" + dataPageName ; 
                                                    System.out.println(saveDataPageFile);
    						File file = new File(saveDataPageFile);
    						FileOutputStream fos = new FileOutputStream(file);
    						fos.write(dataPage.getData().getValue());
    						fos.flush();
    						fos.close();
    						//System.out.println("Saving report page - " + saveDataPageFile);
    					}
    				}
    
    				// query to get the report name
    				PropEnum [] reportProperties = new PropEnum [] {PropEnum.defaultName};
    Thread.sleep(700);
    				BaseClass [] reportInfo = cmService.query(new SearchPathMultipleObject(reportSearchPath), reportProperties, new Sort[]{}, new QueryOptions());
    String saveExcelFile = saveLocation + "/" + reportInfo[0].getDefaultName().getValue() + ".xls"; 
    				File file = new File(saveExcelFile);
    				FileOutputStream fos = new FileOutputStream(file);
    				Base64 b64 = new Base64();	
    				byte[] binaryOutput= null; 
    byte[] buffer = new byte[22528];
    buffer=excelFile.getBytes();
    				fos.write(buffer); 
    				fos.flush();
    				fos.close();
    				System.out.println("Saving report " + saveExcelFile);
    			}
    
    			else
    				System.out.println("No output found for report " + reportSearchPath);	
    
    
    		}
    
    
    
    }
    catch(Exception e)
    		{
    System.out.println(e);	
    		}
    	}
Working...