1.常用场景

(1)将用户数据导出为excel表格(导出数据)

(2)将Excel表中的数据导出到网站数据库中

操作Excel最常用的就是Apache的POI和阿里巴巴的easyExcel。

2.官网地址

3.poi和easyExecel的区别:

以下是我在官网截的图:

2af5aa515aa3c831a73d92fd3f38bc42.png

6b08efddc1d56e4cf11822f41c2769e5.png

https://www.yuque.com/easyexcel/doc/easyexcel

4.Excel中的对象

在java中万物皆对象,Excel也不例外,Excel中的对象有:

工作簿:Workbook(是一个接口)

(使用时需要导入Workbook:import org.apache.poi.ss.usermodel.Workbook;)

Workbook的三个实现类:

(1)HSSFWorkbook:HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls;

(2)XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx;

(3)SXSSFworkbook:是XSSFWorkbook的升级版,当数据量超出65536条后,在使用HSSFWorkbook或XSSFWorkbook,程序会报OutOfMemoryError:Javaheap space;内存溢出错误。这时应该用SXSSFworkbook。

工作表:Sheet (使用时需要导入Sheet:import org.apache.poi.ss.usermodel.Sheet;)

行:Row     (使用时需要导入Row:import org.apache.poi.ss.usermodel.Row;)

列:Cell  (使用时需要导入Cell  :import org.apache.poi.ss.usermodel.Cell;)

5.03版Excel表的生成代码:

packagecom.lqz.controller;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.junit.Test;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;/*** @Author:liqinzhen

* @Date:2020/6/9

* @Description:*/

public classExcelWriteTest {

String path= "E:\\java\\project\\springmvc-ajax-json";

@Testpublic void test() throwsIOException {//1.创建一个工作簿

Workbook workbook = newHSSFWorkbook();//2.创建一个工作表,因为工作表在工作簿中,所以用工作簿创建工作表

Sheet sheet = workbook.createSheet("学生表");//3.创建一个 行

Row row1 = sheet.createRow(0);//4.创建一个单元格

Cell cell11 = row1.createCell(0);//给单元格设置值

cell11.setCellValue("陈咬金");;//创建第二个单元格

Cell cell12 = row1.createCell(1);

cell12.setCellValue("1021");//创建第二行

Row row2 = sheet.createRow(1);//创建第二行的第一个单元格

Cell cell21 = row2.createCell(0);

cell21.setCellValue("芈月");//创建第二行的第二个单元格

Cell cell22 = row2.createCell(1);

cell22.setCellValue("1023");//生成一张表(io流),03版的Excel使用.xls结尾

FileOutputStream fileOutputStream = new FileOutputStream(path+"学生表03.xls");//输出

workbook.write(fileOutputStream);//关闭流

fileOutputStream.close();

System.out.println("学生表03.xls表生成完毕");

}

}

6.07版的Excel生成代码:

//(1)导入少量数据

packagecom.lqz.controller;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.junit.Test;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;/*** @Author:liqinzhen

* @Date:2020/6/9

* @Description:*/

public classExcelWriteTest {

String path= "E:\\java\\project\\springmvc-ajax-json";

@Testpublic void test() throwsIOException {//1.创建一个工作簿

Workbook workbook = newXSSFWorkbook();//2.创建一个工作表,因为工作表在工作簿中,所以用工作簿创建工作表

Sheet sheet = workbook.createSheet("学生表");//3.创建一个 行

Row row1 = sheet.createRow(0);//4.创建一个单元格

Cell cell11 = row1.createCell(0);//给单元格设置值

cell11.setCellValue("陈咬金");;//创建第二个单元格

Cell cell12 = row1.createCell(1);

cell12.setCellValue("1021");//创建第二行

Row row2 = sheet.createRow(1);//创建第二行的第一个单元格

Cell cell21 = row2.createCell(0);

cell21.setCellValue("芈月");//创建第二行的第二个单元格

Cell cell22 = row2.createCell(1);

cell22.setCellValue("1023");//生成一张表(io流),03版的Excel使用.xls结尾

FileOutputStream fileOutputStream = new FileOutputStream(path+"学生表07.xls");//输出

workbook.write(fileOutputStream);//关闭流

fileOutputStream.close();

System.out.println("学生表07.xls表生成完毕");

}

}

//(2)批量数据导入

@Testpublic void test02() throwsIOException {

String path= "E:\\java\\project\\springmvc-ajax-json";//1.创建一个工作簿

HSSFWorkbook workbook = newHSSFWorkbook();//2.创建一个工作表

HSSFSheet sheet =workbook.createSheet();for (int rowNum = 0;rowNum<65536;rowNum++){//3.创建行

Row row =sheet.createRow(rowNum);for (int cellNum = 0;cellNum <5;cellNum++){

Cell cell=row.createCell(cellNum);

cell.setCellValue(cellNum);

}

}

System.out.println("写入完毕");

FileOutputStream fileOutputStream= new FileOutputStream(path + "工作表03.xls");

workbook.write(fileOutputStream);//关闭流

fileOutputStream.close();

}

//(3)读取Excel文件

@Testpublic void readExcelTest() throwsIOException {//获取文件流

FileInputStream fileInputStream = new FileInputStream(path + "学生表03.xls");//创建一个工作簿

HSSFWorkbook workbook = newHSSFWorkbook(fileInputStream);//得到表

Sheet sheet = workbook.getSheetAt(0);//得到行

Row row = sheet.getRow(0);//得到列

Cell cell = row.getCell(0);

System.out.println(cell.getStringCellValue());

fileInputStream.close();

}

//读取不同数据类型

@Testpublic void testCellType() throwsIOException {//获取文件流

FileInputStream fileInputStream = new FileInputStream(path + "学生表03.xls");//创建一个工作簿

HSSFWorkbook workbook = newHSSFWorkbook(fileInputStream);

HSSFSheet sheet= workbook.getSheetAt(0);//获取标题,表的第一行

Row rowTile = sheet.getRow(0);//如果第一行不为空

if (rowTile != null) {//读取第一行列的数量

int numberOfCells =rowTile.getPhysicalNumberOfCells();//循环遍历每一列

for (int i = 0; i < numberOfCells; i++) {//获得列

Cell cell =rowTile.getCell(i);//如果列不为空

if (cell != null) {//获取列的类型

CellType cellType =cell.getCellType();

String stringCellValue=cell.getStringCellValue();

System.out.print(stringCellValue+ "|");

}

}

}

System.out.println();//获取行内容,行的数量

int numberOfRows =sheet.getPhysicalNumberOfRows();//循环获取每一行

for (int i = 1; i < numberOfRows; i++) {//获得行

Row row =sheet.getRow(i);if (row != null) {//获取列

int numberOfCells1 =rowTile.getPhysicalNumberOfCells();for (int j = 0; j < numberOfCells1; j++) {

System.out.print("[" + (j + 1) + (numberOfCells1 + 1) + "]");

Cell cell=rowTile.getCell(j);//匹配列的数据类型

if (cell != null) {

CellType cellType=cell.getCellType();

String cellValue= "";switch(cellType) {caseSTRING:

System.out.println("[STRING]");

cellValue=cell.getStringCellValue();break;caseBOOLEAN:

System.out.println("[BOOLEAN]");

cellValue=String.valueOf(cell.getStringCellValue());break;case BLANK://空

System.out.println("[BOOLEAN]");break;case NUMERIC://空

System.out.println("[NUMERIC]");//数字(普通数字,日期)

if(HSSFDateUtil.isCellDateFormatted(cell)) {

System.out.println("[日期]");

Date date=cell.getDateCellValue();

cellValue= new DateTime(date).toString("yyyy-MM-dd");

}else{

System.out.println("转换为字符串输出");

cell.setCellType(STRING);

cellValue=cell.toString();

}break;case ERROR://空

System.out.println("[数据类型错误]");break;

}

System.out.println(cellValue);

}

}

}

}//关闭流

fileInputStream.close();

}

b7bd87fcf5b052a9f2a20f30733fa161.png

GitHub 加速计划 / ea / easyexcel
31.63 K
7.47 K
下载
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:1 个月前 )
c42183df Bugfix 1 个月前
efa7dff6 * 重新加回 `commons-io` 1 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐