Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. /*
  2. * POIExcelReader.java
  3. *
  4. * Created on 7 October, 2007, 9:05 PM
  5. */
  6.  
  7.  
  8.  
  9. package com.ms.util;
  10.  
  11. //~--- non-JDK imports --------------------------------------------------------
  12.  
  13. import org.apache.poi.hssf.usermodel.HSSFCell;
  14. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  15. import org.apache.poi.hssf.usermodel.HSSFRow;
  16. import org.apache.poi.hssf.usermodel.HSSFSheet;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  18. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  19.  
  20. //~--- JDK imports ------------------------------------------------------------
  21.  
  22. import java.io.FileInputStream;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26.  
  27. import java.util.Iterator;
  28.  
  29. /**
  30. * This java program is used to read the data from a Excel file and display them
  31. * on the console output.
  32. *
  33. * @author dhanago
  34. */
  35. public class POIExcelReader
  36. {
  37.  
  38. /** Creates a new instance of POIExcelReader */
  39. public POIExcelReader ()
  40. {}
  41.  
  42. /**
  43. * This method is used to display the Excel content to command line.
  44. *
  45. * @param xlsPath
  46. */
  47. @SuppressWarnings ("unchecked")
  48. public void displayFromExcel (String xlsPath)
  49. {
  50. InputStream inputStream = null;
  51.  
  52. try
  53. {
  54. inputStream = new FileInputStream (xlsPath);
  55. }
  56. catch (FileNotFoundException e)
  57. {
  58. System.out.println ("File not found in the specified path.");
  59. e.printStackTrace ();
  60. }
  61.  
  62. POIFSFileSystem fileSystem = null;
  63.  
  64. try
  65. {
  66. fileSystem = new POIFSFileSystem (inputStream);
  67.  
  68. HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
  69. HSSFSheet sheet = workBook.getSheetAt (0);
  70. Iterator<HSSFRow> rows = sheet.rowIterator ();
  71.  
  72. while (rows.hasNext ())
  73. {
  74. HSSFRow row = rows.next ();
  75.  
  76. // display row number in the console.
  77. System.out.println ("Row No.: " + row.getRowNum ());
  78.  
  79. // once get a row its time to iterate through cells.
  80. Iterator<HSSFCell> cells = row.cellIterator ();
  81.  
  82. while (cells.hasNext ())
  83. {
  84. HSSFCell cell = cells.next ();
  85.  
  86. System.out.println ("Cell No.: " + cell.getCellNum ());
  87.  
  88. /*
  89. * Now we will get the cell type and display the values
  90. * accordingly.
  91. */
  92. switch (cell.getCellType ())
  93. {
  94. case HSSFCell.CELL_TYPE_NUMERIC :
  95. {
  96.  
  97. // cell type numeric.
  98. System.out.println ("Numeric value: " + cell.getNumericCellValue ());
  99.  
  100. break;
  101. }
  102.  
  103. case HSSFCell.CELL_TYPE_STRING :
  104. {
  105.  
  106. // cell type string.
  107. HSSFRichTextString richTextString = cell.getRichStringCellValue ();
  108.  
  109. System.out.println ("String value: " + richTextString.getString ());
  110.  
  111. break;
  112. }
  113.  
  114. default :
  115. {
  116.  
  117. // types other than String and Numeric.
  118. System.out.println ("Type not supported.");
  119.  
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. catch (IOException e)
  127. {
  128. e.printStackTrace ();
  129. }
  130. }
  131.  
  132. /**
  133. * The main executable method to test displayFromExcel method.
  134. *
  135. * @param args
  136. */
  137. public static void main (String[] args)
  138. {
  139. POIExcelReader poiExample = new POIExcelReader ();
  140. String xlsPath = "c://test//test.xls";
  141.  
  142. poiExample.displayFromExcel (xlsPath);
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement