Writing results to XLS ( MS EXCEL ) using Apache POI.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing results to XLS ( MS EXCEL ) using Apache POI.

Illusion0Reality
This post was updated on .
This code has been verified:


public void writePOI(String flag ,String pagename, String fieldname, String fvalue, int excelrow, String fname, String desc)
throws Exception
{
try{
boolean res= isFileExist(); // a small function should be written with this name --- isFileExist --- to verify the file existance

if (res==false)
{
// a small function should be written with this name --- createNewFile --- to create a new file if file not there:
createNewFile ( "c:\\resultsall.xls");

}
HSSFWorkbook sampleWorkbook = null;
HSSFSheet sampleDataSheet = null;
FileInputStream fi = new FileInputStream (fname);
sampleWorkbook = new HSSFWorkbook(fi);

sampleDataSheet = sampleWorkbook.getSheet("resultsall");
HSSFRow dataRow = sampleDataSheet.createRow(excelrow);

HSSFCell firstHeaderCell = dataRow.createCell(2);

firstHeaderCell.setCellValue(flag);
HSSFCell secondHeaderCell = dataRow.createCell(3);
secondHeaderCell.setCellValue(pagename);
HSSFCell thirdHeaderCell = dataRow.createCell(4);
thirdHeaderCell.setCellValue(fieldname);
HSSFCell fourthHeaderCell = dataRow.createCell(5);
fourthHeaderCell.setCellValue(fvalue);
HSSFCell fifthHeaderCell = dataRow.createCell(6);
fifthHeaderCell.setCellValue( desc);
HSSFCellStyle style = sampleWorkbook.createCellStyle();

HSSFFont font = sampleWorkbook.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short) 10);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setColor(HSSFColor.BLACK.index);
style.setFont(font);

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

firstHeaderCell.setCellStyle(style);
secondHeaderCell.setCellStyle(style);
thirdHeaderCell.setCellStyle(style);
fourthHeaderCell.setCellStyle(style);
fifthHeaderCell.setCellStyle(style);
FileOutputStream fo = new FileOutputStream(fname);
sampleWorkbook.write(fo);
fo.flush();
fo.close();
}
catch(Exception e)
{ System.out.println( e.toString()); }

excelrow ++;
AllVariables.excelrow= excelrow;
}

Reply | Threaded
Open this post in threaded view
|

Re: Writing results to XLS ( MS EXCEL ) using Apache POI.

softwaretestingforum
Administrator
Thanks for posting it. One small suggestion though, it would help if you could post code comments along with with.
It would help newbie in understanding as to what is going on.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Writing results to XLS ( MS EXCEL ) using Apache POI.

Illusion0Reality
I agree and will try !
Reply | Threaded
Open this post in threaded view
|

Re: Writing results to XLS ( MS EXCEL ) using Apache POI.

greenzeto
In reply to this post by Illusion0Reality
help please.
I am new with Apache POI

I dun get these


oolean res= isFileExist(); // a small function should be written with this name --- isFileExist --- to verify the file existance

if (res==false)
{
// a small function should be written with this name --- createNewFile --- to create a new file if file not there:
createNewFile ( "c:\\resultsall.xls");



also,

AllVariables.excelrow= excelrow

i keep getting RED LINE with ALLVARIABLES