How to Write Data into Excel Sheet using Java?
Last Updated :
27 Nov, 2020
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
// 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();
}
}
Output:
Similar Reads
How to Read Write Object's Data in CSV Format Using Notepad in Java? The CSV stands for Comma-Separated Values. CSV files can be used with almost any spreadsheet program, such as Microsoft Excel or Google Spreadsheets. They differ from other spreadsheet file types because you can only have a single sheet in a file, they can not save cell, column, or row. Also, you ca
8 min read
How to Add Hyperlink to the Contents of a Cell using Java? Add a hyperlink to a content of the cell using Java and Apache POI. Apache POI is a Java library that is used to handle Microsoft Office Documents. Installation: There are two ways to install the Apache POI dependency in our java project: Download below mentioned Jar files from poi.apache.org/downlo
2 min read
How to Set Direction to the Text in Cell using Java? Apache POI is an open-source library by Apache which can be used to create, modify and display files MS office file in Java. It provides classes and methods to do so. This API provides various components such as POIFS (Poor Obfuscation Implementation File System), HSSF (Horrible Spreadsheet Format),
3 min read
How to Apply Fonts to the Contents of a Cell Using Java? In this article, we will learn how to apply the custom font and various styles associated with it using Java and Apache POI (a Java API which is very useful to handle the Microsoft Documents). Approach: Writing a file using POI is very simple and involve following steps: Create a workbook.Create a s
2 min read
How to Apply Different Styles to a Cell in a Spreadsheet using Java? Apache POI is a powerful API that enables the user to create, manipulate, and display various file formats based on Microsoft Office using java programs. Using POI, one should be able to perform create, modify, and display/read operations on the following file formats. For Example, Java doesnât prov
6 min read
Java Program to Draw a Shape in Excel Sheet using Apache POI Apache POI supports customized printing by allowing users to select a range of cells in order to configure the desired print area in a worksheet using Java Program. The top-most shape is the patriarch. This is not visible on the sheet at all. To start drawing you need to call createPatriarch on the
4 min read