您的位置:首页 > 编程语言 > Java开发

Java实现excel简单的读写操作

2017-03-16 19:16 525 查看
原文

概述

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.
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>

<!-- ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>


创建工作簿
// 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<ExcelInfo> 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<ExcelInfo> importExcel(String file, int sheetIndex) throws IOException {
FileInputStream in = null;
List<ExcelInfo> result = null;
try {
in = new FileInputStream(file);
result = new ArrayList<ExcelInfo>();
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<ExcelInfo> 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操作加进去,后面更新。

作者: Michaelix 
链接:http://www.imooc.com/article/6885
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: