Java实现excel的读与写(Apache POI)
本文將討論利用Apache POI提供的類實現(xiàn)Excel文件的讀與寫操作。
整個項目的目錄結(jié)構(gòu)基于前面的一篇文章:《java讀取pdf內(nèi)容》
1。pom.xml
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.15</version></dependency>2。將內(nèi)容寫入 Excel文件
下面的代碼利用apache poi把內(nèi)容寫入到一個excel文件中。待寫入的內(nèi)容放到了一個2維數(shù)組中,內(nèi)容將被寫入到XSSFWorkbook對象中,利用XSSFSheet指定excel中工作的sheet。
package org.thinkingingis.optionexcel;import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ApachePOIExcelWrite {private static final String FILE_NAME = "/Users/gisboy/Desktop/MyFirstExcel.xlsx";public static void main(String[] args) {XSSFWorkbook workbook = new XSSFWorkbook();XSSFSheet sheet = workbook.createSheet("sheet in java");Object[][] datas = {{"name", "address", "email"},{"thinking", "beijing", "thinking@thinking.com"},{"in", "beijing", "in@in.com"},{"gis", "beijing", "gis@gis.com"}};int rowNum = 0;System.out.println("apache poi is creating excel...");for(Object[] obj : datas) {Row row = sheet.createRow(rowNum++);int colNum = 0;for(Object field : obj) {Cell cell = row.createCell(colNum++);if(field instanceof String) {cell.setCellValue(field.toString());} else if (field instanceof Integer) {cell.setCellValue((Integer)field);}}}try {FileOutputStream outputStream = new FileOutputStream(FILE_NAME);workbook.write(outputStream);workbook.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}System.out.println("done!");}}注:FILE_NAME根據(jù)操作系統(tǒng)的不同而指定。
執(zhí)行上面代碼,你會得到下面內(nèi)容的excel:
3。讀取Excel文件
下面的代碼將說明通過apache poi如何讀取Excel文件
package org.thinkingingis.optionexcel;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator;import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ApachePOIExcelRead {private static final String FILE_NAME = "/Users/gisboy/Desktop/MyFirstExcel.xlsx";public static void main(String[] args) {try {FileInputStream excelFile = new FileInputStream(FILE_NAME);Workbook workbook = new XSSFWorkbook(excelFile);Sheet dataSheet = workbook.getSheetAt(0);Iterator<Row> iterator = dataSheet.iterator();while(iterator.hasNext()) {Row currentRow = iterator.next();Iterator<Cell> cellIterator = currentRow.iterator();while(cellIterator.hasNext()) {Cell currentCell = cellIterator.next();if(currentCell.getCellTypeEnum() == CellType.STRING) {System.out.print(currentCell.getStringCellValue() + "--");}else if(currentCell.getCellTypeEnum() == CellType.NUMERIC) {System.out.print(currentCell.getNumericCellValue() + "--");}}System.out.println();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}執(zhí)行上面的代碼,結(jié)果如下:
4 。一些解釋:
1.XSSF 是一些操作Mircosoft Excel 2007及以后的版本 類的前綴;
2.XSSFWorkbook是抽象為Excel工作簿的類;
3.XSSFSheet是Excel中sheet的抽象;
4.ROW是excel中的一行
5.Cell是excel行中單元格的地址。
參考引用:
https://poi.apache.org/apidocs/
至此,一個簡單的spring?boot?+?thymeleaf?+?ajax?程序?就搭建好了。
(如遇到問題,請留言給作者,以便共同探討gis知識。thinkingingis@qq.com)
更多干貨?歡迎關(guān)注微信公眾號:?ThinkingInGIS
當(dāng)然,如果你覺得本文對你有幫助,想支持一下作者的話,也是可以贊賞一下的:)
總結(jié)
以上是生活随笔為你收集整理的Java实现excel的读与写(Apache POI)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot AJAX 示例
- 下一篇: Spring Boot 打成war包部署