Saturday, July 16, 2011

Couldn't retrieve cell value in Excell by apache POI

Look at the below code,

-------------------------------------------------------------------------------------------------------

import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class test {

    public static void main(String[] args) throws IOException  {
        // Create a workbook
        Workbook workbook = new HSSFWorkbook();
        // Create new sheet for workbook
        Sheet sheet = workbook.createSheet("Sheet one");
        // Create row at 3
        Row row = sheet.createRow(3);
        Row row2 = sheet.createRow(4);
        Row row3 = sheet.createRow(5);


        row.createCell(7).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        row.getCell(7).setCellValue(276.89);

        row2.createCell(7).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        row2.getCell(7).setCellValue(500);
        row3.createCell(7);
        row3.getCell(7).setCellFormula("SUM(H4:H5)");

        row.createCell(8).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        row.getCell(8).setCellValue(row3.getCell(7).getNumericCellValue());

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream(
                "test.xls");
        workbook.write(fileOut);
        fileOut.close();

    }

}

-------------------------------------------------------------------------------------------------------

out come of above code


In the above created test.xls you will notice that the sum value 77.89 is displayed as 0 in cell I4 though I called row3.getCell(7).getNumericCellValue() method.
Later I realised that its because that cell H6 is i used here as a cell with a SUM formula. So I need to evluate it's value first before I get its value.
Finally the amended code is as below. 

-------------------------------------------------------------------------------------------------------

import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class test {

    public static void main(String[] args) throws IOException  {
        // Create a workbook
        Workbook workbook = new HSSFWorkbook();
        // Create new sheet for workbook
        Sheet sheet = workbook.createSheet("Sheet one");
        // Create row at 3
        Row row = sheet.createRow(3);
        Row row2 = sheet.createRow(4);
        Row row3 = sheet.createRow(5);


        row.createCell(7).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        row.getCell(7).setCellValue(276.89);

        row2.createCell(7).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        row2.getCell(7).setCellValue(500);
        row3.createCell(7);
        row3.getCell(7).setCellFormula("SUM(H4:H5)");


        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
        Cell cell2 = row3.getCell(7);
        evaluator.evaluateFormulaCell(cell2);

        row.createCell(8).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        row.getCell(8).setCellValue(cell2.getNumericCellValue());

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream(
                "test.xls");
        workbook.write(fileOut);
        fileOut.close();

    }

}

-------------------------------------------------------------------------------------------------------

out come of above code



Resources :

Friday, July 8, 2011

How to merge two html files and render as a one html

Hmmm it took a little while for me to google and find a good way of doing so.
I was fortunate enough to get to know about iframe tag in html.
But putting two iframes didn't satisfy my need. The two frames render on the left and right side of the final .html. But I wanted to show them downwords. A friend of mine suggested to use <div> and it worked.
But there was another issue. The two frames did not fit into the screen. for that i had to use style attribute.

So here's the final html code that did the trick.
The code explains itself what has happen, better than me saying it in words.


<div  >
<iframe src="http://www.google.lk/" scrolling="no" frameborder="0" style="width:100%; height:80%;"></iframe>
</div>
<div >
<iframe src="http://modernmerlins.blogspot.com/" scrolling="no" frameborder="0"  style="width:100%; height:250%;"></iframe>
</div>


put the above code in a .html (give any file name and let the file extension be .html ) file and open it up in a browser.... hurry do and see how cool it is. (figure 1)

Note: you can use any html file instead of the links given for src in the above code.

figure 1