You are on page 1of 5

8/1/2018 Excel and Java - Read and Write Excel with Java - Tutorial

Tutorials (http://www.vogella.com/tutorials/) Training (http://www.vogella.com/training/) Consulting (http://www.vogella.com/consulting/)


Search

(http://www.vogella.com)
Downloads (http://www.vogella.com/downloads/) Books (http://www.vogella.com/books/) Company (http://www.vogella.com/company/)
Excel and Java - Read and Write Excel with Online Training
(https://learn.vogella.c

Java - Contact
Donate (http://www.vogella.com/support.html) Tutorial
us (http://www.vogella.com/contact.html)
QUICK LINKS
Lars Vogel (c) 2008, 2016 vogella GmbH – Version 1.4, 29.08.2016
15 OCT - RCP
Training
Table of Contents
(http://www.vogell
1. Installation vogella Training
2. Create an Excel Spreadsheet (http://www.vogell

3. Read an existing Excel Spreadsheet vogella Books


(http://www.vogell
4. About this website
5. Links and Literature SHARE
Appendix A: Copyright and License

This article demonstrate how to create and how to read Excel files
with the Java Excel API.

Escentric Molecules Escentric Molecules Lancôme


349,00 lei 260,00 lei 319,00 lei

Aflați mai multe Aflați mai multe Aflați mai multe

1. Installation
Download the Java Excel library (http://jexcelapi.sourceforge.net/) from the
webpage

To use this library in your Java program add the lib jxl.jar to your classpath in
your project. See Changing classpath in Eclipse
(http://www.vogella.com/tutorials/Eclipse/article.html#classpath).

2. Create an Excel Spreadsheet


Create a new Java project called de.vogella.java.excel. Create the
de.vogella.java.excel.writer package and the following class.

http://www.vogella.com/tutorials/JavaExcel/article.html 1/5
8/1/2018 Excel and Java - Read and Write Excel with Java - Tutorial

JAVA
package writer;
Tutorials (http://www.vogella.com/tutorials/) Training (http://www.vogella.com/training/) Consulting (http://www.vogella.com/consulting/)
Search

(http://www.vogella.com) import java.io.File;


Downloads (http://www.vogella.com/downloads/) Books (http://www.vogella.com/books/)
import java.io.IOException; Company (http://www.vogella.com/company/) Online Training
import java.util.Locale; (https://learn.vogella.c

Donate (http://www.vogella.com/support.html) Contact us (http://www.vogella.com/contact.html)


import jxl.CellView;
import jxl.Workbook; QUICK LINKS
import jxl.WorkbookSettings; 15 OCT - RCP
import jxl.format.UnderlineStyle;
Training
import jxl.write.Formula;
(http://www.vogell
import jxl.write.Label;
import jxl.write.Number; vogella Training
import jxl.write.WritableCellFormat; (http://www.vogell
import jxl.write.WritableFont; vogella Books
import jxl.write.WritableSheet;
(http://www.vogell
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException; SHARE

public class WriteExcel {

private WritableCellFormat timesBoldUnderline;


private WritableCellFormat times;
private String inputFile;

public void setOutputFile(String inputFile) {


this.inputFile = inputFile;
}

public void write() throws IOException, WriteException {


File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();

wbSettings.setLocale(new Locale("en", "EN"));

WritableWorkbook workbook = Workbook.createWorkbook(file,


wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);

workbook.write();
workbook.close();
}

private void createLabel(WritableSheet sheet)


throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES,
10);
// Define the cell format
times = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
times.setWrap(true);

// create create a bold font with unterlines


WritableFont times10ptBoldUnderline = new WritableFont(
WritableFont.TIMES, 10, WritableFont.BOLD, false,
UnderlineStyle.SINGLE);
timesBoldUnderline = new
WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);

CellView cv = new CellView();


cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true);

// Write a few headers


addCaption(sheet, 0, 0, "Header 1");
addCaption(sheet, 1, 0, "This is another header");

private void createContent(WritableSheet sheet) throws

http://www.vogella.com/tutorials/JavaExcel/article.html 2/5
8/1/2018 Excel and Java - Read and Write Excel with Java - Tutorial

WriteException,
Tutorials (http://www.vogella.com/tutorials/) Training
RowsExceededException {
(http://www.vogella.com/training/) Consulting (http://www.vogella.com/consulting/)
Search
// Write a few number
(http://www.vogella.com)
for (int i = 1; i < 10; i++) {
Downloads (http://www.vogella.com/downloads/) Books (http://www.vogella.com/books/)
// First column Company (http://www.vogella.com/company/) Online Training
addNumber(sheet, 0, i, i + 10); (https://learn.vogella.c
// Second column
Donate (http://www.vogella.com/support.html) Contact us (http://www.vogella.com/contact.html)
addNumber(sheet, 1, i, i * i);
QUICK LINKS
}
// Lets calculate the sum of it 15 OCT - RCP
StringBuffer buf = new StringBuffer(); Training
buf.append("SUM(A2:A10)"); (http://www.vogell
Formula f = new Formula(0, 10, buf.toString());
vogella Training
sheet.addCell(f);
buf = new StringBuffer(); (http://www.vogell
buf.append("SUM(B2:B10)"); vogella Books
f = new Formula(1, 10, buf.toString()); (http://www.vogell
sheet.addCell(f);

SHARE
// now a bit of text
for (int i = 12; i < 20; i++) {
// First column
addLabel(sheet, 0, i, "Boring text " + i);
// Second column
addLabel(sheet, 1, i, "Another text");
}
}

private void addCaption(WritableSheet sheet, int column, int row,


String s)
throws RowsExceededException, WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
}

private void addNumber(WritableSheet sheet, int column, int row,


Integer integer) throws WriteException, RowsExceededException
{
Number number;
number = new Number(column, row, integer, times);
sheet.addCell(number);
}

private void addLabel(WritableSheet sheet, int column, int row,


String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
}

public static void main(String[] args) throws WriteException,


IOException {
WriteExcel test = new WriteExcel();
test.setOutputFile("c:/temp/lars.xls");
test.write();
System.out
.println("Please check the result file under
c:/temp/lars.xls ");
}
}

I assume that the code is pretty much self-explaining. I tried to add lots of
comments to make it easier to understand.For more complex examples have a
look at the excellent documentation of the Java Excel API which is also part of
the distribution.

3. Read an existing Excel Spreadsheet


Reuse the project "de.vogella.java.excel". Create a package
"de.vogella.java.excelreader" and the following class "ReadExcel".

http://www.vogella.com/tutorials/JavaExcel/article.html 3/5
8/1/2018 Excel and Java - Read and Write Excel with Java - Tutorial

JAVA
package reader;
Tutorials (http://www.vogella.com/tutorials/) Training (http://www.vogella.com/training/) Consulting (http://www.vogella.com/consulting/)
Search

(http://www.vogella.com) import java.io.File;


Downloads (http://www.vogella.com/downloads/) Books (http://www.vogella.com/books/)
import java.io.IOException; Company (http://www.vogella.com/company/) Online Training
(https://learn.vogella.c
import jxl.Cell;
Donate (http://www.vogella.com/support.html) Contact us (http://www.vogella.com/contact.html)
import jxl.CellType;
import jxl.Sheet; QUICK LINKS
import jxl.Workbook; 15 OCT - RCP
import jxl.read.biff.BiffException;
Training
(http://www.vogell
public class ReadExcel {
vogella Training
private String inputFile; (http://www.vogell
vogella Books
public void setInputFile(String inputFile) {
(http://www.vogell
this.inputFile = inputFile;
}
SHARE
public void read() throws IOException {
File inputWorkbook = new File(inputFile);
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines

for (int j = 0; j < sheet.getColumns(); j++) {


for (int i = 0; i < sheet.getRows(); i++) {
Cell cell = sheet.getCell(j, i);
CellType type = cell.getType();
if (type == CellType.LABEL) {
System.out.println("I got a label "
+ cell.getContents());
}

if (type == CellType.NUMBER) {
System.out.println("I got a number "
+ cell.getContents());
}

}
}
} catch (BiffException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws IOException {


ReadExcel test = new ReadExcel();
test.setInputFile("c:/temp/lars.xls");
test.read();
}

Create an excel spreadsheet and save it somewhere, e.g. c:/temp/lars.xls .

I assume that the code is pretty much self-explaining. I tried to add lots of
comments to make it easier to understand.For more complex examples have a
look at the excellent documentation of the Java Excel API which is also part of
the distribution.

4. About this website

http://www.vogella.com/tutorials/JavaExcel/article.html 4/5
8/1/2018 Excel and Java - Read and Write Excel with Java - Tutorial

Support free content Questions and Tutorial & code license Get the source code
Tutorials (http://www.vogella.com/tutorials/) Training (http://www.vogella.com/training/)
discussion Consulting (http://www.vogella.com/consulting/)
Search

(http://www.vogella.com)
Downloads (http://www.vogella.com/downloads/) Books (http://www.vogella.com/books/) Company (http://www.vogella.com/company/) Online Training
(http://www.vogella.com/support.html) (http://www.vogella.com/license.html)
(http://www.vogella.com/code/index.html)(https://learn.vogella.c
(http://www.vogella.com/contact.html)
5. Links
Donate (http://www.vogella.com/support.html) and
Contact Literature
us (http://www.vogella.com/contact.html)
QUICK LINKS
5.1. Java Excel Resources 15 OCT - RCP
Training
Java Excel API - Homepage (http://jexcelapi.sourceforge.net)
(http://www.vogell
vogella Training
5.2. vogella GmbH training and consulting support (http://www.vogell
vogella Books
TRAINING SERVICE & SUPPORT (http://www.vogell
(http://www.vogella.com/training/) (http://www.vogella.com/consulting/)
SHARE
The vogella company provides The vogella company offers expert
comprehensive training and consulting
education services (http://www.vogella.com/consulting/)
(http://www.vogella.com/training/) from services, development support and
experts in the areas of Eclipse RCP, coaching. Our customers range from
Android, Git, Java, Gradle and Fortune 100 corporations to
Spring. We offer both public and individual developers.
inhouse training. Whichever course
you decide to take, you are
guaranteed to experience what
many before you refer to as “The
best IT class I have ever attended”
(http://www.vogella.com/training/).

Appendix A: Copyright and License


Copyright © 2012-2018 vogella GmbH. Free use of the software examples is
granted under the terms of the Eclipse Public License 2.0
(https://www.eclipse.org/legal/epl-2.0). This tutorial is published under the Creative
Commons Attribution-NonCommercial-ShareAlike 3.0 Germany
(http://creativecommons.org/licenses/by-nc-sa/3.0/de/deed.en) license.

See Licence (http://www.vogella.com/license.html).

Version 1.4
Last updated 2018-02-13 12:00:43 +01:00

http://www.vogella.com/tutorials/JavaExcel/article.html 5/5

You might also like