Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views

How To Write Data Into Excel Sheet Using Java PDF

The document discusses how to write data to an Excel sheet using Java. It explains that the Apache POI library can be used to create Excel files. The key steps are to create a workbook and sheet, then add rows and cells to the sheet to write the data. A sample code is provided that maps data to rows and cells and saves the workbook as an .xlsx file.

Uploaded by

Aditya Kaushik
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

How To Write Data Into Excel Sheet Using Java PDF

The document discusses how to write data to an Excel sheet using Java. It explains that the Apache POI library can be used to create Excel files. The key steps are to create a workbook and sheet, then add rows and cells to the sheet to write the data. A sample code is provided that maps data to rows and cells and saves the workbook as an .xlsx file.

Uploaded by

Aditya Kaushik
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

How to Write Data into Excel Sheet using Java?

Handling files is an important part of any programming language. Java provides


various in-built methods for creating, reading, updating, and deleting files. These
methods are provided by the File class which is present in the java.io package. To
perform file operations, Java uses the stream class.
To do operations in excel sheets using JAVA, it comes in handy to use the CSV files
because CSV files can easily be used with Microsoft Excel, Google spreadsheets, and
almost all other spreadsheets available.

To write data into an excel sheet itself using poi :


1. Create a blank workbook.

XSSFWorkbook workbook = new XSSFWorkbook();


2. Create a sheet and name it.
XSSFSheet spreadsheet = workbook.createSheet(" Student Data ");
3. Create a row
Row row = sheet.createRow(rownum++);
4. Add cells to the sheet.

5. Repeat Steps 3 and 4 to write the complete data.


Prerequisite: Add all jar files downloaded from Apache POI download site in Java
Program’s build path.
Example:
// Java program to write data in excel sheet using java code

import java.io.File;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

public class WriteDataToExcel {

// any exceptions need to be caught

public static void main(String[] args) throws Exception

{
// workbook object

XSSFWorkbook workbook = new XSSFWorkbook();

// spreadsheet object

XSSFSheet spreadsheet

= workbook.createSheet(" Student Data ");

// creating a row object

XSSFRow row;

// This data needs to be written (Object[])

Map<String, Object[]> studentData

= new TreeMap<String, Object[]>();

studentData.put(

"1",

new Object[] { "Roll No", "NAME", "Year" });

studentData.put("2", new Object[] { "128", "Aditya",

"2nd year" });

studentData.put(

"3",

new Object[] { "129", "Narayana", "2nd year" });

studentData.put("4", new Object[] { "130", "Mohan",

"2nd year" });

studentData.put("5", new Object[] { "131", "Radha",

"2nd year" });

studentData.put("6", new Object[] { "132", "Gopal",

"2nd year" });


Set<String> keyid = studentData.keySet();

int rowid = 0;

// writing the data into the sheets...

for (String key : keyid) {

row = spreadsheet.createRow(rowid++);

Object[] objectArr = studentData.get(key);

int cellid = 0;

for (Object obj : objectArr) {

Cell cell = row.createCell(cellid++);

cell.setCellValue((String)obj);

// .xlsx is the format for Excel Sheets...

// writing the workbook into the file...

FileOutputStream out = new FileOutputStream(

new File("C:/savedexcel/GFGsheet.xlsx"));

workbook.write(out);

out.close();

}
}

You might also like