For this API to be Useful, Each Excel Sheet Report Should be Generated before calling this and the file names are Stored in ArrayList .
StyleBuffer is Initialized by calling the getStyleIds() of MultiSheetHelper Class which is given Below:
Here
fileNames is a ArrayList containing FileNames with Absolute Pathnames for eg:/home/user1/xls/abc.xls,/home/user1/xls/abc1.xls
ArrayList fileNames=new ArrayList();
fileNames.add("/home/user1/xls/abc.xls");
fileNames.add("/home/user1/xls/abc1.xls");
String fileNm=null;
String outputFileName="/home/user1/xls/out.xls"
String[] sheetNames={"Sheet1","Sheet2"};
MultiSheetExcelHelper multiSheetExcelHelper=new MultiSheetExcelHelper();
//To Replace the StyleIds of Each file and Storing the Style id details into a Buffer.
StringBuffer styleBuffer=multiSheetExcelHelper.getStyleIds(fileNames);
fileNm=multiSheetExcelHelper.generateMultiSheet(fileNames,outputFileName,styleBuffer,sheetNames);
// Below Java Code is used to Merge all the Files into a Single Workbook containing the Sheets( Each Sheet is Containing the Contents of Each File which is in ArrayList). This is For BIRT 2.2.2,Since There is no Multiple Excel Sheet Generation in that tool.
import java.io.*;
import java.util.*;
public class MultiSheetExcelHelper
{
public String generateMultiSheet(ArrayList fileNames,String outputFile,StringBuffer styleBuffer,String[] sheetNames) throws Exception
{
String strTemp="";
int sheetNo=2;
BufferedWriter bufferWriter= null;
BufferedReader bufferReader=null;
try
{
bufferWriter=new BufferedWriter(new FileWriter(outputFile));
String tempFile=(String)fileNames.get(0);
bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile)));
boolean styleFlag=false;
while((strTemp=bufferReader.readLine())!=null)
{
//System.out.println(st);
if((strTemp.indexOf("
{
bufferWriter.write(strTemp);
bufferWriter.write("\n");
styleFlag=true;
}
if(styleFlag)
{
if(strTemp.indexOf("
{
bufferWriter.write(styleBuffer.toString());
bufferWriter.write("\n");
bufferWriter.write(strTemp);
bufferWriter.write("\n");
styleFlag=false;
}
}
else if(strTemp.indexOf("")<=-1) { if(strTemp.indexOf("
{
strTemp=strTemp.replaceAll("Sheet1",sheetNames[0]);
}
if(strTemp.indexOf("ss:StyleID=")>=0)
{
int index=strTemp.indexOf("ss:StyleID=");
String temp=strTemp.substring(index+12);
int lastIndex=temp.indexOf("\"");
String styleId=temp.substring(0,lastIndex);
String inTemp="ss:StyleID=\""+styleId;
String outTemp="ss:StyleID=\""+styleId+"_sheet1";
strTemp=strTemp.replaceAll(inTemp,outTemp);
bufferWriter.write(strTemp);
inTemp=null;
outTemp=null;
}
else
{
bufferWriter.write(strTemp);
}
bufferWriter.write("\n");
}
strTemp=null;
}
tempFile=null;
if(fileNames!=null && fileNames.size()>1)
{
int i=1;
while(i
{
flag=true;
strTemp=strTemp.replaceAll("Sheet1",sheetNames[i]);
sheetNo++;
}
if(flag)
{
if(strTemp.indexOf("
{
int index=strTemp.indexOf("ss:StyleID=");
String tempStr=strTemp.substring(index+12);
int lastIndex=tempStr.indexOf("\"");
String styleId=tempStr.substring(0,lastIndex);
String inTemp="ss:StyleID=\""+styleId;
String outTemp="ss:StyleID=\""+styleId+"_sheet"+(sheetNo-1);
strTemp=strTemp.replaceAll(inTemp,outTemp);
bufferWriter.write(strTemp);
inTemp=null;
outTemp=null;
}
else
{
bufferWriter.write(strTemp);
}
bufferWriter.write("\n");
}
}
strTemp=null;
}
i++;
}
}
bufferWriter.write("
}
catch(Exception e)
{
outputFile=null;
e.printStackTrace();
throw new Exception(e);
}
finally
{
try
{
if(bufferReader!=null)
{
bufferReader.close();
bufferReader=null;
}
if(bufferWriter!=null)
{
bufferWriter.close();
bufferWriter=null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
return outputFile;
}
public StringBuffer getStyleIds(ArrayList fileNames)
{
StringBuffer styleBuffer=new StringBuffer("");
BufferedReader bf=null;
String st="";
try
{
int sheetNo=1;
for(int i=0;i
{
styleFlag=true;
st="";
}
if((st.indexOf("
{
styleFlag=false;
st="";
break;
}
if(styleFlag)
{
if(st!=null && st.trim().length()>0)
{
if(st.indexOf("Style ss:ID=")>=0)
{
/* int firstIndex=st.indexOf("ss:ID=");
String tempStr=st.substring(firstIndex+7);
int secondIndex=tempStr.indexOf("\"");
String styleId=tempStr.substring(0,secondIndex);
st=st.replaceAll()*/
st=st.replaceAll("\">","_sheet"+sheetNo+"\">");
}
tempBuffer.append(st);
tempBuffer.append("\n");
}
}
}
if(!styleFlag)
{
styleBuffer.append(tempBuffer);
}
sheetNo++;
if(bf!=null)
{
bf.close();
bf=null;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(bf!=null)
{
bf.close();
bf=null;
}
}
catch(Exception e)
{
System.out.println("Exception in Finally"+e);
}
}
return styleBuffer;
}
}
This is Written in the Concept Since the BIRT 2.2.2 is generating the XLS File as XML Format
Which is supported by Microsoft Excel 2003 and 2007