Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

getting cell values

807597Aug 21 2005 — edited Sep 19 2005
HEllo techies,
Iam reading an excel sheet which is having many number of rows.
I want to read only one perticular column data in the xcel sheet.
There may chances that each cell may or may not contain the data
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

class rowCount {
	public static void main(String args[]) {
		try {
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
					args[0]));
			HSSFWorkbook wb = new HSSFWorkbook(fs);
			System.out.println("Number of sheets: " + wb.getNumberOfSheets());
			HSSFSheet sheet = wb.getSheetAt(0);
			int numberOfRows = sheet.getLastRowNum();
			System.out.println("LastRowNumber in sheet0: "
					+ sheet.getLastRowNum());

			for (int i = 0; i <= numberOfRows; i++) {
				HSSFRow row = sheet.getRow(i);
				int cellCount = row.getLastCellNum();
				System.out.println("RowNumber" + i);
				System.out.println("Number of cells in each row" + cellCount);
				for (int j = 0; j < cellCount; j++) {
					HSSFCell cell = row.getCell((short) j);
					if (cell == null) {
						System.out.println("No cell Value");
						continue;
					}
					if (j == 2) {
						// System.out.println("cell");
						System.out.println("RowNumber" + i);
						System.out.println("cell value:  "
								+ cell.getStringCellValue() + "\n");
					}
				}
			}
		} catch (IOException ioe) {
			// TODO: handle exception
			System.out.println("IOException" + ioe.getMessage());
		}

	}
}
Here iam attaching my sample excel sheet also
 A                       B                     C                   D
test	afdjlksf	alsdfkjal	jadfj;asdkjf
sdfajsdfl	asfkajf	asdlfska	adflkal
test	test	test	test
test	test	test	test
test	test	test	
test	test	test	tess
test	test	test	



	test		



	test		

	test
I want to read only values in the column B only.

Can any body guide me how to do this.
If any body guides me i will be very thankfull to him


thanks(inadvance)
ramu.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 17 2005
Added on Aug 21 2005
10 comments
326 views