Read Excel file with Java

Today a friend asked me to help guide him how to read data from a file excel wiht apache poi.
I said ‘ok’ and write this example code.

package org.my.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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;
import org.junit.Test;

public class ReadXLS {
	public static void main(String[] args) {
		String path = "D:\\";
		String fileName = "exelfile.xls";
		String excelFile = path + fileName;
		InputStream inputStream = null;
		POIFSFileSystem fileSystem = null;

		try {
			inputStream = new FileInputStream(excelFile);
			fileSystem = new POIFSFileSystem(inputStream);

			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
			HSSFSheet sheet = null;
			int sheetCounter = 0;
			while ((sheet = workBook.getSheetAt(sheetCounter)) != null) {
				System.out.println("############### SHEET ["
						+ workBook.getSheetName(sheetCounter)
						+ "] #################");
				Iterator rows = sheet.rowIterator();

				while (rows.hasNext()) {
					HSSFRow row = rows.next();

					// display row number in the console.
					System.out.println("Row No.: " + row.getRowNum());

					// once get a row its time to iterate through cells.
					Iterator cells = row.cellIterator();
					StringBuilder sb = new StringBuilder();
					while (cells.hasNext()) {
						HSSFCell cell = cells.next();

						sb.append("Cell No.: " + cell.getColumnIndex());

						/*
						 * Now we will get the cell type and display the values
						 * accordingly.
						 */
						switch (cell.getCellType()) {
						case HSSFCell.CELL_TYPE_NUMERIC: {

							// cell type numeric.
							sb.append("\tNumeric value: "
									+ cell.getNumericCellValue());

							break;
						}

						case HSSFCell.CELL_TYPE_STRING: {

							// cell type string.
							HSSFRichTextString richTextString = cell
									.getRichStringCellValue();

							sb.append("\tString value: "
									+ richTextString.getString());

							break;
						}

						default: {

							// types other than String and Numeric.
							sb.append("\tType not supported.");

							break;
						}

						}
						sb.append(" \n");
					}
					System.out.println(sb.toString());
				}
				System.out.println("############### END READ SHEET ["
						+ workBook.getSheetName(sheetCounter)
						+ "] #################");
				sheetCounter++;

				// if index is out of range break and exit reading xls file
				try {
					workBook.getSheetAt(sheetCounter);
				} catch (Exception e) {
					break;
				}

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

, , ,

  1. ให้ความเห็น

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Connecting to %s

Follow

Get every new post delivered to your Inbox.