In this article you will learn about the writing and reading of data from Excel files in Java (to be considered as
XLS, and XLSX format). We will use Apache POI library, and will focus on working with the types String and Date, the last work is fairly tricky. Let me remind you that the work with the numbers we have already considered in another article.
Library
poi-XX.jar you can use all the old (xls, doc, ppt) files, Microsoft Office, for new (xlsx,docx, pptx), you will need poi-ooxml-XX.jar. It is important to understand that what is, as used classes are also different - for older extensions isHSSFWorkbook, and for new - XSSFWorkbook.Preparation: loading libraries and dependencies
Of course, there are many public libraries that allow you to work with Excel files in Java, for example, JXL, but we will be having the most extensive use of the API and the most popular - Apache POI. To use it, you need to download
jar files and add them through Eclipse manually, or you can leave it to Maven.
In the second case, you simply need to add the following two dependencies:
The most convenient in Maven - it will load not only the specified
poi.jar andpoi-ooxml.jar, but all the jar files that are used internally, ie xmlbeans-2.6.0.jar,stax-api-1.0.1.jar , poi-ooxml-schemas-3.12.jar and commons-codec-1.9.jar.
If you add a library manually - do not forget about the above-mentioned files. Download all possible fromhere. Remember - if you download only the
poi-XX.jar, then your code compiles without errors, but then falls to java.lang.NoClassDefFoundError: org / apache / xmlbeans / XmlObject,because inside will be called xmlbeans.jar.Recording
In this example, we write in the
xls file the following data: the first cell - a string with the name, and the second - the date of birth. Here are step by step instructions:- Create object HSSFWorkBook;
- Create a list using the facility, created in the previous step,
createSheet(); - Create a line on a sheet using
createRow(); - Create a cell in the row -
createCell(); - Asking cell value through
setCellValue (); - Write the
workbookin theFilethrough FileOutputStream; - Close the
workbook, causingaddress closee-().
To write numbers or strings that is enough, but to record the date, we need to do something else:
- Create DateFormat;
- Create CellStyle;
- Record
DateFormatin CellStyle; - Record
CellStyleinto the cell; - Now in this cell can be written object
Datethrough all the same setCellValue; - To date fit into a cell, we need to add a column to automatically change the size of the property:
sheet.autoSizeColumn(1).
All together it will look like this:
Reading
Now we think of the newly created file that we recorded there.
- First, create
HSSFWorkBook, passed to the constructorFileInputStream; - We get the list, passing
getSheet ()its name or number; - Get the string using
getRow(); - Get the cell using
getCell(); - We learn the type of cell, using her
getCellType(); - Depending on the type of cell, read its values using
getStringCellValue(),getNumericCellValue ()orgetDateCellValue(); - Close the
workbookusing theaddress closee-().
Recall that Excel stores dates as numbers, ie cell type will still CELL_TYPE_NUMERIC.
In the form of this code is as follows:
Finally
As mentioned above upomnalos reading from
xlsx file does not differ - but instead needHSSFWorkBook, HSSFSheet, HSSFRow (and others) from poi-XX.jar use XSSFWorkBook,XSSFSheet, XSSFRow of poi-ooxml-XX.jar. That's all you need to know to read and write files in Excel. Of course, with the help of Apache POI library, you can do much more, but this article is to help you quickly get used to it.
Комментариев нет:
Отправить комментарий