How To Read Excel and Text File In Java Code
Read Excel file in java
Excel is the very popular file format created by Microsoft. 
In this article i have discussing how to read Excel files using java code.
Java Code :
 public static void main(String[] args) throws IOException
{
String fileName = "C:\\file-path\\report.xls";
String cellContent = "Total";
int rownr=0, colnr = 10;
InputStream input = new FileInputStream(fileName);
HSSFWorkbook wb = new HSSFWorkbook(input);
HSSFSheet sheet = wb.getSheetAt(0);
rownr = findRow(sheet, cellContent);
finish();
}
private static int findRow(HSSFSheet sheet, String cellContent)
{
for (Row row : sheet) {
for (Cell cell : row)
{
Iterator cells = row.cellIterator ();
while (cells.hasNext ())
{
HSSFCell cell = cells.next ();
switch (cell.getCellType ())
{
case HSSFCell.CELL_TYPE_NUMERIC :
{
System.out.println ("Numeric value: " + cell.getNumericCellValue ());
break;
}
case HSSFCell.CELL_TYPE_STRING :
{
HSSFRichTextString richTextString = cell.getRichStringCellValue ();
System.out.println ("String value: " + richTextString.getString ());
break;
}
default :
{
System.out.println ("Type not supported.");
break;
}
}
}
}
return 0;
}
}
{
String fileName = "C:\\file-path\\report.xls";
String cellContent = "Total";
int rownr=0, colnr = 10;
InputStream input = new FileInputStream(fileName);
HSSFWorkbook wb = new HSSFWorkbook(input);
HSSFSheet sheet = wb.getSheetAt(0);
rownr = findRow(sheet, cellContent);
finish();
}
private static int findRow(HSSFSheet sheet, String cellContent)
{
for (Row row : sheet) {
for (Cell cell : row)
{
Iterator cells = row.cellIterator ();
while (cells.hasNext ())
{
HSSFCell cell = cells.next ();
switch (cell.getCellType ())
{
case HSSFCell.CELL_TYPE_NUMERIC :
{
System.out.println ("Numeric value: " + cell.getNumericCellValue ());
break;
}
case HSSFCell.CELL_TYPE_STRING :
{
HSSFRichTextString richTextString = cell.getRichStringCellValue ();
System.out.println ("String value: " + richTextString.getString ());
break;
}
default :
{
System.out.println ("Type not supported.");
break;
}
}
}
}
return 0;
}
}
Read Text file in java
Text is the very popular file format and The first notepad Text file was originally invented on 18th February, 1902, when a man named J. A. Birchall created the Silver City Writing Tablet for his stationery company in Tasmania, Australia.
In this article i have discussing how to read Text files using java code.
Simple Steps :
1) First step is very simple only import BufferReader and FileReader Input/Output java package in your class.
2) Create a BufferedReader object with your Text file path.
3) Check Text file is Not Empty.
4) Print your Text file lines.
Java Code :
import java.io.BufferedReader; 
import Java.io.FileReader; 
public class ReadLine
{
  BufferedReader br = new BufferedReader(new FileReader("youtFile.txt"));
  try
  {
    String line;
    while((line= br.readLine())!=null)
    {
        System.out.pringln(line)
    }
  }
 catch(Esception exo)
 {
 }
 finally
 {
    br.close();
  }
} 
I hope this article useful to you. So, Please like and share.
Thank you.

Comments
Post a Comment