How to Generate Excel in Java

preview_player
Показать описание
In this video tutorial we will learn a part of Java and Excel integration and see how to generate Excel in Java.

Microsoft excel is a very powerful spreadsheet program created by Microsoft, and is very widely used. It provides numerous functions in addition to programming capability through which users can develop their own functions and procedures. Excel can also be integrated with other programming languages to build powerful software applications.

Follow this step by step easy tutorial to learn how to integrate Java and Excel and see how to generate Excel file in Java.

Step 1 - Creating a new Java Project

First of all, let's create a java project with a class.

Step 2 - Downloading API

To generate excel in java, we need the Jxl library which does the I/O operation of the excel files.

On that page, click on jexcelapi and download it.

Step 3 - Importing the Library

Once downloaded, the next step is to import the library to our project. For that, go to eclipse, move over to the file menu and click on import.

Step 4 - Giving Import Source

Select the File system directory here and click on next.

Step 5 - Giving the Library Path

Now let us locate the JXL library which we have just downloaded

Step 6 - Inserting Library into the project

After that done we have to browse and select our project where we want to import the library.

Step 7 - Adding build Path

The excel java api has now been imported in our library.

After that we have to right click on the Library icon, hover over to Build Path and click on the Add to Build Path option.

Step 8 - Code

After that, write the code in editor

Now let's quickly go through the code. On the headers, we included the JXL library and its write functions. In the main method, first we created a writable Workbook object and initialized it with the createWorkbook method using the filename and the path where we want to save our file.

Next we created a sheet in the excel file by using the createSheet method. Label constructor gets the position of the cell and the content of the cell, same as the Number constructor. The addCell method over here adds a label or number in the cell. And finally the write method writes the contents to the file.

Step 9 - Debug

Now let's run it and open up the location specified in the code.

Step 10 - File generated

And we can see that a new file has been created.

And this is how java and excel can be integrated.
Рекомендации по теме
Комментарии
Автор

Excellent!! Very well explained! thanks a lot!.. Regards from Argentina..

maurod
Автор

Incredible tutorial. Thank you so much.

AnonTheRant
Автор

Hi, Thanks for the helpful video. On the net I found Jspreadsheet. It’s Java API for Microsoft Excel from Independentsoft company. There are a number of use cases on the site and the API itself is well documented.

vladankos
Автор

Typed in the code and got an error: A line under createSheetWritableSheet sheet = Workbook.createSheet("Sheet1", 0);Any suggestions?

jsmarks
Автор

WritableSheet sheet = Workbook.createSheet("Sheet1", 0);

Got CreateSheet error can u plz help me

sairam