目录
POI对Excel文件的读写
1.写数据到Excel文件(小数据+大数据)
按行来写,先确定行,然后对每一列进行赋值
- //生成的文档的地址为桌面
- String path = "C:\\Users\\Administrator\\Desktop\\";
-
- /**
- * 1.小数据
- * @throws IOException
- */
- @Test
- public void writePoI07 () throws IOException {
- /**
- * row --行从0开始0代表第一行,cell --列 0开始,0代表第一列
- */
- //1.创建工作簿
- Workbook wk = new XSSFWorkbook();
- //2.创建工作表
- Sheet sh = wk.createSheet("周先生统计表");
- //3.创建行(1,1)
- Row row = sh.createRow(0);
- //4.创建单元格
- Cell cell = row.createCell(0);
- //5.写数据
- cell.setCellValue("姓名");
- Cell cell1 = row.createCell(1);
- cell1.setCellValue("周彬彬");
-
- Row row2=sh.createRow(1);
- Cell cells=row2.createCell(0);
- cells.setCellValue("注册日期");
- Cell cell2 = row2.createCell(1);
- String date = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
- cell2.setCellValue(date);
-
- //创建文件流,生存一张表
- FileOutputStream fos = new FileOutputStream(path + "666.xlsx");
- //把文件写入工作簿中
- wk.write(fos);
- //关闭流
- fos.close();
- System.out.println("输出完毕!!!!!!");
- }
-
- /**
- * 大数据输出
- * @throws Exception
- */
- @Test
- public void writeXls07d () throws Exception {
- //0.获得写操作开始时间
- long start=System.currentTimeMillis();
-
- //1.创建文档对象
- Workbook wk =new XSSFWorkbook();
-
- //2.通过文档对象创建工作簿
- Sheet sheet = wk.createSheet("07大数据写");
-
- //3.通过工作谱创建行和单元
- for (int rowNum = 0; rowNum <100000 ; rowNum++) {
- Row row = sheet.createRow(rowNum);
- for (int cellNum = 0; cellNum <10 ; cellNum++) {
- Cell cell = row.createCell(cellNum);
- cell.setCellValue(cellNum);
- }
- }
-
-
- //通过流输出文档对象
- FileOutputStream fos = new FileOutputStream(path+"testS.xls");
-
- //向文档对象传入流对象进行写操作
- wk.write(fos);
- //获得写操作结束的时间
- long end = System.currentTimeMillis();
- System.out.println("批量写操作成功!!!");
- System.out.println("批量写操作总共用时:"+(double)(end-start)/1000+"秒");
- //关闭流
- fos.close();
-
- }
2.读取Excel文件
- package com.wyh.logstash;
-
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFRow;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import org.joda.time.DateTime;
- import org.junit.Test;
-
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Date;
-
- public class POIReadTest {
- String path = "C:\\Users\\Administrator\\Desktop\\666.xlsx";
-
- /**
- * 07版本读取excel文件
- */
- @Test
- public void readPOI07() throws IOException {
- //1.获取文件流
- FileInputStream fis = new FileInputStream(path);
- //2.通过文档对象——>创建工作谱
- XSSFWorkbook workbook = new XSSFWorkbook(fis);
- XSSFSheet sheet = workbook.getSheetAt(0);
- //3.获取第一行(标题)内容
- Row rowTitle = sheet.getRow(0);
-
- if (rowTitle != null) {
- //3.1拿到列数
- int cellCount = rowTitle.getPhysicalNumberOfCells();
- //3.2遍历
- for (int cellNum = 0; cellNum < cellCount; cellNum++) {
- //3.3得到单元格的值
- Cell cell = rowTitle.getCell(cellNum);
- if (cell != null) {
- int cellType = cell.getCellType();
- String cellValue = cell.getStringCellValue();
- System.out.println(cellValue + " | ");
- }
- }
- System.out.println();
- }
-
- //4.表中的内容
- int rows = sheet.getPhysicalNumberOfRows();
-
- //4.1遍历每一行
- for (int rowNum = 1; rowNum < rows; rowNum++) {
- //4.2得到当前行
- Row rowData = sheet.getRow(rowNum);
- if (rowData != null) {
- //4.3读取每行的每一列,得到单元格
- int cellCount = rowTitle.getPhysicalNumberOfCells();
- for (int cellNum = 0; cellNum < cellCount; cellNum++) {
- System.out.println("[" + (rowNum + 1) + "-" + (cellNum + 1) + "]");
- Cell cell = rowData.getCell(cellNum);
- //5.根据单元格cell匹配数据
- if (cell != null) {
- int cellType = cell.getCellType();
- String cellValue = "";
- switch (cellType) {
- case HSSFCell.CELL_TYPE_STRING://字符串
- System.out.println("[String]");
- cellValue = cell.getStringCellValue();
- break;
- case HSSFCell.CELL_TYPE_BOOLEAN: // 布尔
- System.out.print("【BOOLEAN】");
- cellValue = String.valueOf(cell.getBooleanCellValue());
- break;
- case HSSFCell.CELL_TYPE_BLANK: // 空
- System.out.print("【BLANK】");
- break;
- case HSSFCell.CELL_TYPE_NUMERIC: // 数字(日期、普通数字)
- System.out.print("【NUMERIC】");
- if (HSSFDateUtil.isCellDateFormatted(cell)) { // 日期
- System.out.print("【日期】");
- Date date = cell.getDateCellValue();
- cellValue = new DateTime(date).toString("yyyy-MM-dd");
- } else {
- // 不是日期格式,防止数字过长!
- System.out.print("【转换为字符串输出】");
- cell.setCellType(HSSFCell.CELL_TYPE_STRING);
- cellValue = cell.toString();
- }
- break;
- case HSSFCell.CELL_TYPE_ERROR:
- System.out.print("【数据类型错误】");
- break;
- }
- System.out.println(cellValue);
- }
- }
- }
- }
- }
-
- }
EasyExcel对文件的读取
读取数据主要靠的是监听器
(50条消息) EasyExcel综合课程实战_Fairy要carry的博客-CSDN博客