Problem in Apache POI for reading a Excel
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
im开发者_Python百科port java.io.FileInputStream;
import java.lang.Iterable;
public class ReadExcel {
public static String fileToBeRead = "C:/Documents and Settings/Developer/Desktop/Anand exmps/Anand.xls";
public static void main(String argv[]) {
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
HSSFSheet sheet = workbook.getSheetAt(0);
//HSSFRow row = sheet.getRow(0);
//HSSFCell cell = row.getCell((short) 0);
for (Row row : sheet) {
for (Cell cell : row) {
System.out.println("THE TOP LEFT CELL–> "+ cell.getRichStringCellValue());
}
}
} catch (Exception e) {
System.out.println("!! Bang !! xlRead() : " + e);
}
}
}
The following error occurs when compiling the above program. What must be th reason? Please fix. Am a beginner in java.
ReadExcel.java:16: cannot find symbol
symbol : class Row
location: class ReadExcel
for (Row row : sheet) {
^
ReadExcel.java:17: cannot find symbol
symbol : class Cell
location: class ReadExcel
for (Cell cell : row) {
You forgot to import the Row
and Cell
classes.
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
Resources :
- HSSFSheet.getRow(int) returns a Row
- Row iterates on Cell
精彩评论