How to Open CSV Files in a Microsoft Excel Application Using Java Code

CSV files are comma delimited files created by Microsoft Excel or SQL Server. The file is a list of records with each field separated with a comma. Reading these files in Java is accomplished with a few extra lines of code and an extended library that is downloaded on the Internet. Each file is imported, read, and the information is extracted either for use within the application or displayed to the user.

Instructions

    • 1

      Download the extraction tool at the following location:
      http://sourceforge.net/projects/datafile/files
      Extract the downloaded files into your Java directory.

    • 2

      Instantiate the class and assign it to a variable. Once the class is instantiated, you can use its methods and properties:
      DataFile dataclass = DataFile.createReader("8859_1");

    • 3

      Indicate the file format. The following code opens the file and tells the class that the file format is comma delimited, with the first row indicating the column header names:
      dataclass.setDataFormat(new CSVFormat());
      dataclass.containsHeader(true);

    • 4

      Import the data into memory. The following syntax opens the CSV file from the hard drive:
      dataclass.open(new File("c:\\myfile.csv"));

    • 5

      Read the first row of data. The code below uses the opened file variable to read the first record and store it into a variable for display:
      DataRow firstrow = dataclass.next();
      String firstrecord = firstrow.getString(0);

    • 6

      Print the record to the console for display. Now the data can be used within the code or displayed to the use. The following code prints it to the screen:
      System.out.println(firstrecord);

Related Searches:

References

Comments

You May Also Like

  • How to Make a CSV File in Java

    Comma-separated values (CSV) files are simple databases consisting of a single table whose data appear in columns defined by commas. You can...

  • How to Use CSV Files

    CSV files are useful when you want a spreadsheet format in a text document, since a CSV file is merely a text...

  • How to Read a CSV File Into Java

    CSV, or Comma Separated Value, files are commonly used as a universal format for exchanging simple data, especially between spreadsheet applications. The...

  • How to Open Microsoft Excel Using Java Code

    Java is a popular enterprise level programming language which runs on a cross-platform runtime, similar to the competing .NET runtime. It's often...

  • How to Convert an XLSX File to CSV

    Included in Microsoft's Office suite of productivity products, Excel is a widely used spreadsheet applications. It lets users create and edit rows...

  • How to Open a CSV File With VBA

    A Comma Separated Value (CSV) file is a plain text file that has a single record on each line. Each line is...

  • How to Remove CSV Columns

    The comma separated values, or CSV, format is used by all spreadsheet programs and is a nearly universal means of transferring data...

  • How to Open CSV Files With OpenOffice

    Spreadsheet programs have their own proprietary file formats. This makes it difficult to share files unless both people are using the same...

  • How to Open Multiple CSV Files in One Excel Spreadsheet

    Join multiple CSV files in separate Excel spreadsheets into one to reduce paging between different spreadsheets and increase productivity. Merging CSV files...

  • How to Open an Application Using MS Excel

    The best way to open an external application when you're working with Microsoft Excel is using Visual Basic for Applications (VBA). VBA...

Related Ads

Featured