Skip to main content

How To Read Excel and Text File In Java Code

How To Read Excel and Text File In Java Code


java


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;
}
}


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

Popular posts from this blog

Atal Pension Yojana

Atal Pension Yojana फाइनेंस मिनिस्टर अरुण जेटलीजी  ने 2015 के बजट में ये अटल पेंशन योजना( Atal Pension Yojana Scheme ) शामिल की हे । इस योजना से आप खुद आपकी बुढ़ापे की लाठी बन सकते हे , इस योजना गरीब और  अनस्ट्रक्चर्ड नोकरियत वर्ग के लिए लाभ दाई हे ।  इस  अटल पेंशन योजना ( Atal Pension Yojana (APY ) )  1 जून 2015 से शुरू हुई थी , सरकार का कहना हे की 2010 - 2011 में  स्वालंबन पेंशन योजना  शुरू हुए थी पर उसमे जो खामिया थी उसको सुधारके ये योजना शुरू करवाई गइ हे । साथ ही जो लोग  स्वालंबन पेंशन योजना में जुड़े थे उस लोगो को इस योजना से जोड़ दिया हे ।  -  इस पेंशन योजना( Pension Scheme ) में 18 - 40 A ge  के लोग जुड़ सकते हे ,  इस योजना में २० वर्षो तक योगदान रहेंगा ।  -  इस योजना( Atal Pension Scheme ) के लिए आधार कार्ड(Aadhaar Card)  होना आवस्यक हे जिनसे आसानी से इस योजना के लाभ नागरिक ले सके और सेविंग बैंक अकाउंट भी होना  आवस्यक हे अटल पेंशन योजना में 1000 से 5000 ...

10+ Ideas about beautiful rangoli designs | Diwali, New Year, Events

10+ Ideas about beautiful rangoli designs | 10+ Ideas about beautiful rangoli designs | Diwali, New Year, Corporate Event, Gudi Padwa, Pongal  Diwali rangoli patterns and designs started many centuries ago. Some reference of rangoli designs are also available in our scriptures and designed by prashant javia and misha javia.  Watch this wonderful rangoli patterns and designs video and download Rangoli Designs video for Diwali, Newyear, Commercial use and Competition and Decorate your home this diwali with special designs of rangoli. All Most Rangoli designs which you can make use of in all occasions. The occasions can be Diwali, New Year, Ugadi or Gudi Padwa, Pongal, Corporate Event

Courses after 12th Science: Career for Biology/Maths Students

Courses after 12th Science: Career for Biology/Maths Students Which is the Top Courses To Do After 12th Science ? What is your right carrier after 12th Science for Biology/Maths Students? Which are Courses after 12th Science ? Many Question arise on your mind but i am all doubts clear on your questions. what's your hobby and which course match in your hobby related course here i have discuss. Today, education is very much compulsory to build a good career. Students are used to wandering here and there for right career and best guidance. Here we are going to provide career counselling guidance for the students who wish to search their career after 12th. We refer to those students who are having science group in 12th standard. Sciences students can appear in JEE Main 2016 and AIPMT 2016, for admission to top engineering and medical colleges of the country receptively. There are more over lakh of students who are surrounded with (?) marks regarding their various ...