Note that there are some explanatory texts on larger screens.

plurals
  1. POusing HSSF to read an excel file
    primarykey
    data
    text
    <p>Emp ID Name Salary</p> <p>1.0 john 2000000.0</p> <p>2.0 dean 4200000.0</p> <p>3.0 sam 2800000.0</p> <p>4.0 cass 600000.0</p> <p>I have created this code:</p> <pre><code>import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; 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; public class sample2 { public static void main(String[] args) { new sample2().sample2(); } } FileInputStream file = new FileInputStream(new File("C:\\test.xls")); //Get the workbook instance for XLS file HSSFWorkbook workbook = new HSSFWorkbook(test); //Get first sheet from the workbook HSSFSheet sheet = workbook.getSheetAt(0); //Get iterator to all the rows in current sheet Iterator&lt;Row&gt; rowIterator = sheet.iterator(); //Get iterator to all cells of current row Iterator&lt;Cell&gt; cellIterator = row.cellIterator(); try { FileInputStream file = new FileInputStream(new File("C:\\test.xls")); //Get the workbook instance for XLS file HSSFWorkbook workbook = new HSSFWorkbook(file); //Get first sheet from the workbook HSSFSheet sheet = workbook.getSheetAt(0); //Iterate through each rows from first sheet Iterator&lt;Row&gt; rowIterator = sheet.iterator(); while(rowIterator.hasNext()) { Row row = rowIterator.next(); //For each row, iterate through each columns Iterator&lt;Cell&gt; cellIterator = row.cellIterator(); while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch(cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t\t"); break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t\t"); break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t\t"); break; } } System.out.println(""); } file.close(); FileOutputStream out = new FileOutputStream(new File("C:\\test.xls")); workbook.write(out); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); </code></pre> <p>}</p> <p>for reading the content from this excel file using POI library. My editor is Eclipse. But when i run the program I took this: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method sample2() is undefined for the type sample2</p> <pre><code>at sample2.main(sample2.java:17) </code></pre> <p>Any help? Thank u in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload