Smart Xls Java Example to Read Write Csv File

Reading and Writing files in java is a very basic thing that every programmer needs to know. In this article, we will focus on how to read and write Excel and CSV files in java. Following pointers volition be covered in this commodity,

  • Introduction
  • What is a CSV file?
  • How to write a CSV file in Coffee?
  • How to read CSV file in Java?
  • What is an Excel file?
  • How to write Excel file in Java?
  • How to read Excel file in Java?

Moving on with this article on Read and write Excel and CSV file in Java

Introduction

Allow'southward talk about the reading and writing file in Java. Let'southward take a unproblematic scenario where we want to write a file in JAVA. Suppose we have a simple text file and we want to write some data into that text file so how tin can a Java program write this information into the file.
1. Start, nosotros open the text file in our Coffee program in writing fashion because we want to write some data into the file.
2. At present we have our file object, after that, we append some string in the file object.
3. Finally, nosotros affluent the changes into the file and close the file.

Now let's talk about how we can read the file in Coffee suppose we have a text file that contains some information and we want to read this data.

1. Firstly nosotros need to open the file in the reading way and laissez passer that data to a file object.
2. Adjacent in need to read the data from the file line past line for this we create a loop and read the data line past line from the file.
iii. Subsequently completing the reading data from the file we need to close the file.

Moving on with this article on Read and write Excel and CSV file in Coffee

What is the CSV file?

Now let'due south talk near the CSV file, well CSV file is a normal file but contains the data with comma-separated values this comma-separated values. CSV file stores the table data into a elementary text file. In our instance, we created a CSVReadWrite.java file to read and write the file.

Moving on with this article on Read and write Excel and CSV file in Java

How to write a CSV file in Java?

Let's have a simple example where we write a CSV file. For this, we utilize openCSV library. Get-go, we load the file path into the CSVWriter so set up the cavalcade and and so button the data both are comma-separated. The writeNext method is used to push the data to the file.

Moving on with this article on Read and write Excel and CSV file in Java

How to read CSV file in Coffee?

Permit's talk about writing a CSF file in java. In our example, we created readingCSVFile method to read the CSV file. Nosotros use FileReader object to load the file and BudfferedReader form to read the file. And then nosotros commencement reading the file line by line readFilerow. Then we split up the data by comma and relieve it into the assortment and finally print the data by index.

CSVReadWrite.coffee

package com.excelcsv; import com.opencsv.CSVWriter; import java.io.*; public class CSVReadWrite { String filePath; CSVWriter file; CSVReadWrite(String filePath){ this.filePath = filePath; } // writing csv file function public void writingCSVFile(){ try { file = new CSVWriter(new FileWriter(new File(filePath))); Cord[] colName = { "Student ID", "Student Name", "Educatee Email" }; file.writeNext(colName); String[] data = {"001", "Frank", "frank@znx.com"}; String[] data1 = {"002", "Marker", "marking@znx.com"}; String[] data2 = {"003", "Martin", "martin@znx.com"}; file.writeNext(information); file.writeNext(data1); file.writeNext(data2); file.close(); } catch (Exception east) { e.printStackTrace(); } } // reading csv file public void readingCSVFile(){ try { BufferedReader readFile = new BufferedReader(new FileReader(filePath)); String readFilerow; while ((readFilerow = readFile.readLine()) != null) { Cord[] data = readFilerow.divide(","); Arrangement.out.println(data[0]+"|"+data[1]+"|"+data[ii]); } readFile.close(); } grab (FileNotFoundException due east) { e.printStackTrace(); } catch (IOException e) { eastward.printStackTrace(); } } }        

Output- Read and write Excel and CSV file in Java- Edureka

Output- Read and write Excel and CSV file in Java- Edureka

Moving on with this article on Read and write Excel and CSV file in Java

What is an Excel file?

Before talking about the excel file let's first talk about Microsoft Excel, well Microsoft Excel is a software program that used in a spreadsheet to read, write and calculate data from the tabular array. When we store a Microsoft Excel file that file calls the excel file that has the extension .xls(older) or .xlsx (newer).

Moving on with this article on Read and write Excel and CSV file in Java

How to write Excel file in Coffee?

At present allow'southward discuss the file writing in excel, well writing the file in xls format we utilize JExcel API because the excel file is the specific file that open and edits with Microsoft Excel software. nosotros demand to create a WritableWorkbook object and gear up the file path in Workbook.createWorkbook method. Side by side, we phone call the method createSheet to create a new sheet and ready the label. Finally, we need to add prison cell for this we call the method addCell and pass the characterization object with data nosotros want to put in the excel file. In the last, nosotros call the excelSheet.write a method to write the data to the excel file.

Moving on with this article on Read and write Excel and CSV file in Java

How to read Excel file in Java?

In the final phase of reading excel file, we first set up the file path in the Workbook.getWorkbook next we create a sail object and get the canvass.
Next, nosotros create a cell object and become a jail cell form the sheet object. Now we want to print the data so nosotros telephone call the getContents method on cell object this will return the information from the specific jail cell.

ExcelReadWrite.coffee

package com.excelcsv; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import java.io.File; import java.io.IOException; public class ExcelReadWrite { String filePath; ExcelReadWrite(String filePath){ this.filePath = filePath; } // writing excel file public void writeExcelFile(){ WritableWorkbook excelSheet = goose egg; try { excelSheet = Workbook.createWorkbook(new File(filePath)); WritableSheet excelFile = myFirstWbook.createSheet("Sheet 1", 0); Label label = new Label(0, 0, "Test Count"); excelFile.addCell(characterization); Number number = new Number(0, 1, 1); excelFile.addCell(number); label = new Label(one, 0, "Consequence"); excelFile.addCell(characterization); label = new Label(1, 1, "Passed"); excelFile.addCell(label); number = new Number(0, 2, ii); excelFile.addCell(number); label = new Label(1, 2, "Passed 2"); excelFile.addCell(label); excelSheet.write(); }take hold of (Exception e){ east.printStackTrace(); } } // reading excel file public void readExcelFile(){ endeavour { excelSheet = Workbook.getWorkbook(new File(filePath)); Sheet sheet = workbook.getSheet(0); Cell cell1 = canvas.getCell(0, 0); System.out.print(cell1.getContents() + ":"); Jail cell cell2 = sheet.getCell(0, 1); System.out.println(cell2.getContents()); Cell cell3 = sail.getCell(1, 0); Organization.out.print(cell3.getContents() + ":"); Cell cell4 = sheet.getCell(1, 1); Arrangement.out.println(cell4.getContents()); System.out.print(cell1.getContents() + ":"); cell2 = sheet.getCell(0, two); System.out.println(cell2.getContents()); System.out.impress(cell3.getContents() + ":"); cell4 = sheet.getCell(1, 2); System.out.println(cell4.getContents()); } take hold of (IOException e) { east.printStackTrace(); } } }        

Output- Read and write Excel and CSV file in Java- Edureka

For treatment the exception treatment nosotros write bothe classes code between the try-catch block.
The Principal.java main java file that contains the main method here we call both classes, then nosotros create a csvObj to handle read and write CSV file and create another object execObj to read and write excel file. This file handles the main functionality of our programme.

Primary.coffee

package com.excelcsv; public course Main { public static void main(String[] args) { /** * reading and writing CSV file here */ // creating CSVReadWrite course object CSVReadWrite csvObj = new CSVReadWrite(Organisation.getProperty("user.dir")+"output_csv.csv"); // writing CSV file csvObj.writingCSVFile(); // read csv file csvObj.readingCSVFile(); /** * reading and writing Excel file hither */ // creating ExcelReadWrite object ExcelReadWrite excObj = new ExcelReadWrite(System.getProperty("user.dir")+"output_excel.xls"); // writing excel file excObj.writeExcelFile(); // reading excel file excObj.readExcelFile(); } }        

With this, we come to an end of this Read and Write Excel and CSV File in Java article.

Check out the Java Certification Course past Edureka, a trusted online learning visitor with a network of more than 250,000 satisfied learners spread across the earth. Edureka's Java J2EE and SOA grooming and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head beginning into Java programming and train you for both cadre and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for united states? Please mention it in the comments section of this "Read and Write Excel and CSV File in Java" blog and we volition go back to you lot as soon as possible.

Smart Xls Java Example to Read Write Csv File

Source: https://www.edureka.co/blog/read-write-excel-file-in-java/

0 Response to "Smart Xls Java Example to Read Write Csv File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel