java读写excel_Java实现excel简单读写

本文介绍了如何使用Apache POI库在Java中读写Excel文件。POI提供了HSSF和XSSF组件,分别处理旧版.xls和新版.xlsx格式。通过创建Workbook、Sheet、Row和Cell,可以方便地进行Excel数据的写入。同时,文章给出了读取Excel文件并将其数据映射到bean对象的示例代码。
摘要由CSDN通过智能技术生成

概述

Apache POI是 Java 程序员用来处理 MS Office 文件的常用开源库。由 Apache 软件基金会开发,使用 Java 分布式设计或修改 Microsoft Office 文件

,包含一系列类和方法对用户输入数据或文件进行编码和解码。其中 POI-HSSF 和 POI-XSSF 是用来处理 excel 文件的组件,前者对应 97~2007版本的文件格式(.xls), 后者对应07以后的格式(.xlsx),更多关于 POI 的介绍请访问官方主页。

个人觉得 POI 对 office 的对象进行了很好的抽象设计,因此学习起来比较平滑。在阅读学习的同时打开excel进行同步操作,你会感觉到使用 POI 和使用 office 一样简单。

Quick Start

在进行操作之前,请使用 maven,ivy 等工具导入依赖;或者下载jar包导入到classpath下,本文使用的是最新稳定版3.14.

org.apache.poi

poi

3.14

org.apache.poi

poi-ooxml

3.14

创建工作簿

// 07之前版本

Workbook wb = new HSSFWorkbook();

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

// 07之后版本

Workbook wb = new XSSFWorkbook();

FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");

wb.write(fileOut);

fileOut.close();

创建表单

// wb 可以是上述创建的两个对象之一

Sheet sheet = wb.createSheet();

Sheet sheet = wb.createSheet("库存");

创建单元格

// 1. 首先创建行,声明行的索引,从0开始。

Row row = sheet.createRow(0);

// 2. 创建单元格,并设置值,可以是 Date,String,Calendar等类型

Cell cell = row.createCell(0);

cell.setCellValue("msg");

## 读取Excel文件

下面的示例方法中,读取一个excel表格,将其每一行数据抽象成一个 bean ExcelInfo, 最后返回一个list对象

``` java

package excel;

/**

* excel bean

*

* @author Michal

* @create 2016-04-22 17:03

*/

public class ExcelInfo {

private int index;

private String ip;

private String community;

public int getIndex() {

return index;

}

public void setIndex(int index) {

this.index = index;

}

public String getIp() {

return ip;

}

public void setIp(String ip) {

this.ip = ip;

}

public String getCommunity() {

return community;

}

public void setCommunity(String community) {

this.community = community;

}

@Override

public String toString() {

return "ExcelInfo{" +

"index=" + index +

", ip='" + ip + '\'' +

", community='" + community + '\'' +

'}';

}

}

package excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

/**

* Excel演示demo

*

* @author Michal

* @create 2016-04-22 17:00

*/

public class ExcelDemo {

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

// 需要替换到本机上的文件路径

List list = importExcel("D:/learn/javautil/src/test/java/resources/example.xls");

System.out.println(list);

}

/**

* 导入excel文件,使用绝对路径

*

* @param file

* @param sheetIndex

* @return

* @throws IOException

*/

public static List importExcel(String file, int sheetIndex) throws IOException {

FileInputStream in = null;

List result = null;

try {

in = new FileInputStream(file);

result = new ArrayList();

Workbook wb = new HSSFWorkbook(in);

Sheet sheet = wb.getSheetAt(sheetIndex);

for (Row row : sheet) {

if (row.getRowNum() < 1) {

continue;

}

ExcelInfo eInfo = new ExcelInfo();

eInfo.setIndex(row.getRowNum());

eInfo.setIp(row.getCell(0).getStringCellValue());

eInfo.setCommunity(row.getCell(1).getStringCellValue());

result.add(eInfo);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

in.close();

}

return result;

}

public static List importExcel(String file) throws IOException {

return importExcel(file, 0);

}

}

写入Excel文件

package excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

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.ss.usermodel.WorkbookFactory;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

/**

* Excel写入

*

* @author Michal

* @create 2016-04-22 17:34

*/

public class ExcelWrite {

public static void main(String[] args) {

excelExp("e:/result.xls");

}

/**

* 传入文件的绝对路径

*

* @param filePath

*/

public static void excelExp(String filePath) {

Workbook wb = null;

OutputStream out = null;

try {

wb = new HSSFWorkbook();

Sheet sheet = wb.createSheet("test");

sheet.setColumnWidth(0, 18 * 256);

sheet.setColumnWidth(1, 18 * 256);

Row r = sheet.createRow(0);

r.createCell(0).setCellValue("ip");

r.createCell(1).setCellValue("community");

r.createCell(2).setCellValue("result");

out = new FileOutputStream(filePath);

wb.write(out);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

后记

之前有个jdbc的demo,现在想把excel操作加进去,后面更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值