相信,各位小伙伴们,对电子表格一定相当的熟悉,学生相关信息,成绩单,业务单,工资单,都会使用Excel来进行存储。
但,将大量的数据导入Excel和从Excel中将大量的数据导出,却耗费我们大量的时间。
我们是否可以利用程序来进行操作的?
显然是可以的。
在现在来说,对于Excel的操作的工具包,主要为
apache POI
alibaba easyExcel
今天咱们来一起学习一些POI的操作。
1.饭前甜点
我们先来看一下POI的基础知识。
官方地址:https://poi.apache.org/ 是appche下面的产品。
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
我们再来看一下Excel中基本信息。
当我们使用鼠标右击,然后新建Excel时,我们发现Excel含有两种格式。
XLS
XLSX
区别如下:
1、存储大小:xls的存储行数最大为65536行,存储有限,xlsx的存储行数最大为1048576行
2、版本不同。xls是excel2003及以前版本生成的文件格式,而xlsx是excel2007及以后版本生成的文件格式。
3、兼容性不同。xlsx格式是向下兼容的,可兼容xls格式。
2.POI的登场
当我们了解一些基础知识后,我们开始代码的书写。
我们知道Excel中是由三个部分组成的
一个excel文件,我们称之为一个工作簿
一个工作簿默认含有三个工作表
单元格为基础的单位 利用行和列进行确定
我们在上面的基础知识中,知道Excel含有两种格式,所以我们的jar包也是两种。
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi xls的jar包 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml xlsx的jar包-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
我们进行写的基本步骤
先创立工作簿
在创立工作表
建立单元格,先生成行
再生成列
在单元格中存值
利用流写入文件
03版本的execel的基础写操作
public class TestExcel03Write {
private static String PATH = "D:\\Data\\idea数据\\small\\excel-poi";
public static void main(String[] args) throws IOException {
//先创立一个工作簿
Workbook workbook = new HSSFWorkbook();
//在创立一个工作表
Sheet sheet = workbook.createSheet("我的03EXCEL");
//创立单元格 先创立一个行,再创立列 代码中从零开始 excel中的下标为(1,1)
Row row1 = sheet.createRow(0);
Cell cell11 = row1.createCell(0);
cell11.setCellValue("姓名");
//再创立(1,2)
Cell cell12 = row1.createCell(1);
cell12.setCellValue("年龄");
//再创立(2,1)和(2,2);
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
Cell cell22 = row2.createCell(1);
cell21.setCellValue("辉辉");
cell22.setCellValue("21");
//需要写入文件中去,引入流的知识
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "03.XLS");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
}
07版本的execel的基础写操作
public class TestExcel07Write {
private static String PATH = "D:\\Data\\idea数据\\small\\excel-poi";
public static void main(String[] args) throws IOException {
//先创立一个工作簿
Workbook workbook = new XSSFWorkbook();
//在创立一个工作表
Sheet sheet = workbook.createSheet("我的07EXCEL");
//创立单元格 先创立一个行,再创立列 代码中从零开始 excel中的下标为(1,1)
Row row1 = sheet.createRow(0);
Cell cell11 = row1.createCell(0);
cell11.setCellValue("姓名");
//再创立(1,2)
Cell cell12 = row1.createCell(1);
cell12.setCellValue("年龄");
//再创立(2,1)和(2,2);
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
Cell cell22 = row2.createCell(1);
cell21.setCellValue("辉辉");
cell22.setCellValue("21");
//需要写入文件中去,引入流的知识
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "07.XLSX");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
}
03版本和07版本的代码区别
文件的后缀不同
03 使用的工作簿的实现类为HSSFWorkbook 07使用的工作簿的实现类为XSSFWorkbook
3.POI的逆使用
我们从上面,已经知道了写,那么我们现在开始搞定读。
我们进行写的基本步骤
利用流将Excel中文件读入数据
得到工作表
得到单元格,先得到行
再得到列
得到单元格
最后得到值
03版本的读
private static String PATH = "D:\\Data\\idea数据\\small\\";
@Test
public void test() throws Exception{
//因为是读,所以将把文件加入流中
FileInputStream fileInputStream = new FileInputStream(PATH + "excel-poi03.xls");
//创立一个工作簿
Workbook workbook = new HSSFWorkbook(fileInputStream);
//得到工作表
Sheet sheet = workbook.getSheetAt(0);
//利用sheet来得到相应单元格中的值
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println(cell.getStringCellValue());
//记住使用流就要关掉
fileInputStream.close();
}
07版本的读
private static String PATH = "D:\\Data\\idea数据\\small\\";
@Test
public void test() throws Exception{
//因为是读,所以将把文件加入流中
FileInputStream fileInputStream = new FileInputStream(PATH + "excel-poi07.xlsx");
//创立一个工作簿
Workbook workbook = new XSSFWorkbook(fileInputStream);
//得到工作表
Sheet sheet = workbook.getSheetAt(0);
//利用sheet来得到相应单元格中的值
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println(cell.getStringCellValue());
//记住使用流就要关掉
fileInputStream.close();
}
吾们已修的其功,却身心乏累,吾担忧其身,进阶功法,且听小弟下回分解。
更多精彩:微信公众号 JAVA在左 美文在右