Reading excel file in java apache poi

How to Read Excel files in Java using Apache POI

How to Read Excel files in Java using Apache POI

Excel files (spreadsheets) are widely used by people all over the world for various tasks related to organization, analysis, and storage of tabular data.

Since excel files are so common, we developers often encounter use-cases when we need to read data from an excel file or generate a report in excel format.

In this article, I’ll show you how to read excel files in Java using a very simple yet powerful open source library called Apache POI.

And in the next article, You’ll learn how to create and write to an excel file using Apache POI.

First of all, We need to add the required dependencies for including Apache POI in our project. If you use maven, you need to add the following dependencies to your pom.xml file —

 dependency> groupId>org.apache.poigroupId> artifactId>poiartifactId> version>3.17version> dependency>  dependency> groupId>org.apache.poigroupId> artifactId>poi-ooxmlartifactId> version>3.17version> dependency>

If you use gradle then you can add the following to your build.gradle file

compile "org.apache.poi:poi:3.17" compile "org.apache.poi:poi-ooxml:3.17"

The first dependency poi is used to work with the old Microsoft’s binary file format for excel. These file formats have .xls extension.

The second dependency poi-ooxml is used to work with the newer XML based file format. These file formats have .xlsx extension.

Sample Excel file that We’ll read

Following is a sample excel file that we’ll read in our code. It is created using Google Sheets and has .xlsx extension.

Note that, Although the sample file is of the newer XML based file format ( .xlsx ). The code that we’ll write will work with both types of file formats — .xls and .xlsx

Apache Poi Sample Excel File

Apache POI excel library revolves around following four key interfaces —

  1. Workbook: A workbook is the high-level representation of a Spreadsheet.
  2. Sheet: A workbook may contain many sheets. The sample excel file that we looked at in the previous section has two sheets — Employee and Department
  3. Row: As the name suggests, It represents a row in the spreadsheet.
  4. Cell: A cell represents a column in the spreadsheet.

HSSF and XSSF implementations —

Apache POI library consists of two different implementations for all the above interfaces.

  1. HSSF (Horrible SpreadSheet Format): HSSF implementations of POI’s high-level interfaces like HSSFWorkbook , HSSFSheet , HSSFRow and HSSFCell are used to work with excel files of the older binary file format — .xls
  2. XSSF (XML SpreadSheet Format): XSSF implementations are used to work with the newer XML based file format — .xlsx .

Apache POI classes and interfaces - XSSF HSSF implementations

Program to Read an excel file using Apache POI

The following program shows you how to read an excel file using Apache POI. Since we’re not using any file format specific POI classes, the program will work for both types of file formats — .xls and .xlsx .

The program shows three different ways of iterating over sheets, rows, and columns in the excel file —

import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; import java.io.File; import java.io.IOException; import java.util.Iterator; public class ExcelReader  public static final String SAMPLE_XLSX_FILE_PATH = "./sample-xlsx-file.xlsx"; public static void main(String[] args) throws IOException, InvalidFormatException  // Creating a Workbook from an Excel file (.xls or .xlsx) Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH)); // Retrieving the number of sheets in the Workbook System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : "); /* ============================================================= Iterating over all the sheets in the workbook (Multiple ways) ============================================================= */ // 1. You can obtain a sheetIterator and iterate over it IteratorSheet> sheetIterator = workbook.sheetIterator(); System.out.println("Retrieving Sheets using Iterator"); while (sheetIterator.hasNext())  Sheet sheet = sheetIterator.next(); System.out.println("=> " + sheet.getSheetName()); > // 2. Or you can use a for-each loop System.out.println("Retrieving Sheets using for-each loop"); for(Sheet sheet: workbook)  System.out.println("=> " + sheet.getSheetName()); > // 3. Or you can use a Java 8 forEach with lambda System.out.println("Retrieving Sheets using Java 8 forEach with lambda"); workbook.forEach(sheet ->  System.out.println("=> " + sheet.getSheetName()); >); /* ================================================================== Iterating over all the rows and columns in a Sheet (Multiple ways) ================================================================== */ // Getting the Sheet at index zero Sheet sheet = workbook.getSheetAt(0); // Create a DataFormatter to format and get each cell's value as String DataFormatter dataFormatter = new DataFormatter(); // 1. You can obtain a rowIterator and columnIterator and iterate over them System.out.println("\n\nIterating over Rows and Columns using Iterator\n"); IteratorRow> rowIterator = sheet.rowIterator(); while (rowIterator.hasNext())  Row row = rowIterator.next(); // Now let's iterate over the columns of the current row IteratorCell> cellIterator = row.cellIterator(); while (cellIterator.hasNext())  Cell cell = cellIterator.next(); String cellValue = dataFormatter.formatCellValue(cell); System.out.print(cellValue + "\t"); > System.out.println(); > // 2. Or you can use a for-each loop to iterate over the rows and columns System.out.println("\n\nIterating over Rows and Columns using for-each loop\n"); for (Row row: sheet)  for(Cell cell: row)  String cellValue = dataFormatter.formatCellValue(cell); System.out.print(cellValue + "\t"); > System.out.println(); > // 3. Or you can use Java 8 forEach loop with lambda System.out.println("\n\nIterating over Rows and Columns using Java 8 forEach with lambda\n"); sheet.forEach(row ->  row.forEach(cell ->  String cellValue = dataFormatter.formatCellValue(cell); System.out.print(cellValue + "\t"); >); System.out.println(); >); // Closing the workbook workbook.close(); > >

Note that we’re not even using the concrete classes like HSSFWorkbook and XSSFWorkbook to create an instance of the Workbook. We’re creating the workbook using a WorkbookFactory instead. This makes our program format independent and it works for both types of files — .xls and .xlsx .

The program shows three different ways to iterate over sheets, rows, and columns. I prefer the Java 8 forEach loop with a lambda expression. You may use whichever method you like.

Note that, I’ve used a DataFormatter to format and get each cell’s value as String.

Retrieving Cell values by CellType

Instead of using a DataFormatter to format and get each cell’s value as String regardless of the Cell type, You may check each cell’s type and then retrieve its value using various type-specific methods like this —

private static void printCellValue(Cell cell)  switch (cell.getCellTypeEnum())  case BOOLEAN: System.out.print(cell.getBooleanCellValue()); break; case STRING: System.out.print(cell.getRichStringCellValue().getString()); break; case NUMERIC: if (DateUtil.isCellDateFormatted(cell))  System.out.print(cell.getDateCellValue()); > else  System.out.print(cell.getNumericCellValue()); > break; case FORMULA: System.out.print(cell.getCellFormula()); break; case BLANK: System.out.print(""); break; default: System.out.print(""); > System.out.print("\t"); >

You may now call the above method in the main program to print each cell’s value —

sheet.forEach(row ->  row.forEach(cell ->  printCellValue(cell); >); System.out.println(); >);

That’s all folks! In this article, You learned how to read excel files in Java using Apache POI library. You can find the entire source code on the github repository.

Also, Don’t forget to check out the next article to learn how to create and write to an excel file using Apache POI

Thank you for reading. Until next time!

Источник

Работа с Excel в Java через Apache POI

Обложка: Работа с Excel в Java через Apache POI

Из этой статьи вы сможете узнать о записи и чтении данных из Excel файлов в Java (будет рассмотрен как XLS , так и XLSX формат). Мы будем использовать библиотеку Apache POI и сосредоточимся на работе с типами String и Date , работа с последним происходит достаточно хитро. Напомню, что работу с числами мы уже рассмотрели в другой статье.

Библиотеку poi-XX.jar вы можете использовать для всех старых ( xls , doc , ppt ) файлов Microsoft Office, для новых ( xlsx , docx , pptx ) вам понадобится poi-ooxml-XX.jar . Очень важно понимать, что к чему относится, т.к. используемые классы тоже разные — для старых расширений это HSSFWorkbook , а для новых — XSSFWorkbook .

Подготовка: загрузка библиотек и зависимостей

Конечно, существует достаточно много открытых библиотек, которые позволяют работать с Excel файлами в Java, например, JXL, но мы будем использовать имеющую самый обширный API и самую популярную — Apache POI. Чтобы её использовать, вам нужно скачать jar файлы и добавить их через Eclipse вручную, или вы можете предоставить это Maven.

Во втором случае вам нужно просто добавить следующие две зависимости:

  org.apache.poi poi 3.12  org.apache.poi poi-ooxml 3.12   

Самое удобное в Maven — что он загрузит не только указанные poi.jar и poi-ooxml.jar , но и все jar файлы, которые используются внутри, то есть xmlbeans-2.6.0.jar , stax-api-1.0.1.jar , poi-ooxml-schemas-3.12.jar и commons-codec-1.9.jar .

Если вы будете добавлять библиотеки вручную — не забудьте о вышеназванных файлах. Скачать всё можно отсюда. Помните — если вы загрузите только poi-XX.jar , то ваш код скомпилируется без ошибок, но потом упадёт с java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject , так как внутри будет вызываться xmlbeans.jar .

Запись

В этом примере мы запишем в xls файл следующие данные: в первую ячейку — строку с именем, а во вторую — дату рождения. Вот пошаговая инструкция:

  • Создаём объект HSSFWorkBook ;
  • Создаём лист, используя на объекте, созданном в предыдущем шаге, createSheet() ;
  • Создаём на листе строку, используя createRow() ;
  • Создаём в строке ячейку — createCell() ;
  • Задаём значение ячейки через setCellValue();
  • Записываем workbook в File через FileOutputStream ;
  • Закрываем workbook , вызывая close() .

Для записи строк или чисел этого вполне достаточно, но чтобы записать дату, нам понадобится сделать ещё кое-что:

  • Создать DateFormat ;
  • Создать CellStyle ;
  • Записать DateFormat в CellStyle ;
  • Записать CellStyle в ячейку;
  • Теперь в эту ячейку можно записать объект Date через всё тот же setCellValue ;
  • Чтобы дата поместилась в ячейку, нам нужно добавить столбцу свойство автоматически менять размер: sheet.autoSizeColumn(1) .

Всё вместе это будет выглядеть так:

@SuppressWarnings("deprecation") public static void writeIntoExcel(String file) throws FileNotFoundException, IOException < Workbook book = new HSSFWorkbook(); Sheet sheet = book.createSheet("Birthdays"); // Нумерация начинается с нуля Row row = sheet.createRow(0); // Мы запишем имя и дату в два столбца // имя будет String, а дата рождения --- Date, // формата dd.mm.yyyy Cell name = row.createCell(0); name.setCellValue("John"); Cell birthdate = row.createCell(1); DataFormat format = book.createDataFormat(); CellStyle dateStyle = book.createCellStyle(); dateStyle.setDataFormat(format.getFormat("dd.mm.yyyy")); birthdate.setCellStyle(dateStyle); // Нумерация лет начинается с 1900-го birthdate.setCellValue(new Date(110, 10, 10)); // Меняем размер столбца sheet.autoSizeColumn(1); // Записываем всё в файл book.write(new FileOutputStream(file)); book.close(); >

Чтение

Теперь мы считаем из только что созданного файла то, что мы туда записали.

  • Для начала создадим HSSFWorkBook , передав в конструктор FileInputStream ;
  • Получаем лист, передавая в getSheet() его номер или название;
  • Получаем строку, используя getRow() ;
  • Получаем ячейку, используя getCell() ;
  • Узнаём тип ячейки, используя на ней getCellType() ;
  • В зависимости от типа ячейки, читаем её значения, используя getStringCellValue() , getNumericCellValue() или getDateCellValue() ;
  • Закрываем workbook используя close() .

Напомню, что дату Excel хранит как числа, т.е. тип ячейки всё равно будет CELL_TYPE_NUMERIC .

В виде кода это будет выглядеть следующим образом:

public static void readFromExcel(String file) throws IOException < HSSFWorkbook myExcelBook = new HSSFWorkbook(new FileInputStream(file)); HSSFSheet myExcelSheet = myExcelBook.getSheet("Birthdays"); HSSFRow row = myExcelSheet.getRow(0); if(row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_STRING)< String name = row.getCell(0).getStringCellValue(); System.out.println("name : " + name); >if(row.getCell(1).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) < Date birthdate = row.getCell(1).getDateCellValue(); System.out.println("birthdate :" + birthdate); >myExcelBook.close(); > 

В заключение

Как уже упомналось выше, чтение из xlsx файлов ничем принципиально не отличается — нужно только вместо HSSFWorkBook , HSSFSheet , HSSFRow (и прочих) из poi-XX.jar использовать XSSFWorkBook , XSSFSheet , XSSFRow из poi-ooxml-XX.jar . Это всё, что вам нужно знать для чтения и записи в файлы Excel. Разумеется, с помощью библиотеки Apache POI вы можете сделать гораздо больше, но эта статья должна помочь вам быстрее в ней освоиться.

Источник

Читайте также:  Php mysql variable types
Оцените статью