daily pastebin goal
28%
SHARE
TWEET

Untitled

a guest Jan 16th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.FileInputStream;
  2. import java.io.InputStream;
  3. import java.util.Iterator;
  4.  
  5. import org.apache.poi.hssf.usermodel.HSSFCell;
  6. import org.apache.poi.hssf.usermodel.HSSFRow;
  7. import org.apache.poi.hssf.usermodel.HSSFSheet;
  8. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9. import org.apache.poi.ss.usermodel.Row;
  10.  
  11. public class ReadExcelFile {
  12.  
  13.     public static final String EXCEL_FILE = "C:/Test.xls";
  14.  
  15.     public static void readXLSFile() {
  16.         try {
  17.             InputStream ExcelFileToRead = new FileInputStream(EXCEL_FILE);
  18.             HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);
  19.  
  20.             HSSFSheet sheet = wb.getSheetAt(0);
  21.             HSSFRow row;
  22.             HSSFCell cell;
  23.  
  24.             Iterator rows = sheet.rowIterator();
  25.  
  26.             while (rows.hasNext()) {
  27.                 row = (HSSFRow) rows.next();
  28.                 Iterator cells = row.cellIterator();
  29.  
  30.                 while (cells.hasNext()) {
  31.                     cell = (HSSFCell) cells.next();
  32.                     System.out.print(cell.toString()+" ");
  33.                 }
  34.                 System.out.println();
  35.             }
  36.  
  37.         } catch (Exception e) {
  38.             e.printStackTrace();
  39.         }
  40.  
  41.     }
  42.    
  43.     public static void readXLSFileWithBlankCells() {
  44.         try {
  45.             InputStream ExcelFileToRead = new FileInputStream(EXCEL_FILE);
  46.             HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);
  47.  
  48.             HSSFSheet sheet = wb.getSheetAt(0);
  49.             HSSFRow row;
  50.             HSSFCell cell;
  51.  
  52.             Iterator rows = sheet.rowIterator();
  53.  
  54.             while (rows.hasNext()) {
  55.                 row = (HSSFRow) rows.next();
  56.                
  57.                 for(int i=0; i<row.getLastCellNum(); i++) {
  58.                     cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK);
  59.                     System.out.print(cell.toString()+" ");
  60.                 }
  61.                 System.out.println();
  62.             }
  63.  
  64.         } catch (Exception e) {
  65.             e.printStackTrace();
  66.         }
  67.  
  68.     }
  69.  
  70.     public static void main(String[] args) {
  71.         readXLSFile();
  72.         readXLSFileWithBlankCells();
  73.     }
  74.  
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top