一:读取数据
1:首先需要准备jar包或者引入maven的依赖
<!-- poi报表-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
2:准备需要读取文件的数据表格
**
3:话不多说,上代码
**
@Test
public void testRead()throws Exception{
// 第一步 使用流 读取文件
//读取文件的路径
FileInputStream fileInputStream =
new FileInputStream(new File("D:\\zhuomian\\111.xlsx"));
// 2. 使用 xssf 去创建 我们 workbook
XSSFWorkbook excel=new XSSFWorkbook(fileInputStream);
// 3. 获取 sheet 根据索引
XSSFSheet sheet=excel.getSheetAt(0);
//4. 遍历row
for (Row row : sheet) {
System.out.println(row);
for (Cell cell : row) {
System.out.println(cell.getStringCellValue());
}
}
excel.close();
}
4:读取表格后控制台的显示
这只是其中一条数据
二:录入数据
1:和上面一样,同样引入依赖或者jar包
2:代码实现过程
//使用pio写入到磁盘
@Test
public void testWriteExecl()throws IOException{
//1:创建execl
XSSFWorkbook execl=new XSSFWorkbook();
//2:创建sheet
XSSFSheet sheet=execl.createSheet("新的sheet");
//3:row cell
XSSFRow title = sheet.createRow(0);
//cell
title.createCell(0).setCellValue("姓名");
title.createCell(1).setCellValue("地址");
title.createCell(2).setCellValue("年龄");
XSSFRow dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("张三");
dataRow.createCell(1).setCellValue("河南");
dataRow.createCell(2).setCellValue("14");
FileOutputStream fileOutputStream=new FileOutputStream(new File("D:\\zhuomian\\113.xlsx"));
execl.write(fileOutputStream);
fileOutputStream.flush();
//关闭数据流
execl.close();
}
3:看结果
如有需要,拿走不谢