狂神说:https://space.bilibili.com/95256449/channel/detail?cid=146244
原生
依赖
<!-- xls(03) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- xlsx(07) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!-- 日期格式化 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
write
03版本xls
//存储路径
static final String PATH="C:\\Users\\LEGION\\Desktop\\";
@Test
public void test03Write03() throws Exception {
//1:创建一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//2:创建一个工作表
HSSFSheet sheet = workbook.createSheet("阿杰数据");
//第一行
//3:创建一个行,从第一行开始
Row row1 = sheet.createRow(0);
//4: 创建一个单元格,从第一行第一个单元格开始
Cell cell11 = row1.createCell(0);
//插入第一行第一列数据
cell11.setCellValue("title");
//第一行第二个单元格
Cell cell12 = row1.createCell(1);
//插入第一行第二列数据
cell12.setCellValue("统计时间");
//第二行
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("java");
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell22.setCellValue(time);
//创建流写入数据
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "阿杰数据03.xls");
//将工作簿通过流写出到指定的文件夹
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
}
07版本xlsx
@Test
public void test03Write07() throws Exception {
//1:创建一个工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
//2:创建一个工作表
Sheet sheet = workbook.createSheet("阿杰数据");
//第一行
//3:创建一个行,从第一行开始
Row row1 = sheet.createRow(0);
//4: 创建一个单元格,从第一行第一个单元格开始
Cell cell11 = row1.createCell(0);
//插入第一行第一列数据
cell11.setCellValue("title");
//第一行第二个单元格
Cell cell12 = row1.createCell(1);
//插入第一行第二列数据
cell12.setCellValue("统计时间");
//第二行
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("java");
Cell cell22 = row2.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell22.setCellValue(time);
//创建流写入数据
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "阿杰数据07.xlsx");
//将工作簿通过流写出到指定的文件夹
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
}
03大批量写入数据
@Test
public void test03WriteDate03() throws Exception {
long start = System.currentTimeMillis();
//1:创建一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//2:创建一个工作表
Sheet sheet = workbook.createSheet("阿杰数据大数据测试");
//大批量写入数据
for (int i = 0; i <65536 ; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellValue("java"+i);
}
}
//创建流写入数据
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "阿杰数据03.xls");
//将工作簿通过流写出到指定的文件夹
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
long end = System.currentTimeMillis();
//输出运行时间
System.out.println((double) (end-start)/1000);
}
07大批量写入数据,时间久
@Test
public void test03WriteDate07() throws Exception {
long start = System.currentTimeMillis();
//1:创建一个工作簿
Workbook workbook = new XSSFWorkbook();
//2:创建一个工作表
Sheet sheet = workbook.createSheet("阿杰数据大数据测试");
//大批量写入数据
for (int i = 0; i <100000 ; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(i);
}
}
System.out.println("over");
//创建流写入数据
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "阿杰数据07.xlsx");
//将工作簿通过流写出到指定的文件夹
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
long end = System.currentTimeMillis();
//输出运行时间
System.out.println((double) (end-start)/1000);
}
07大批量写入数据(高效)
@Test
public void test03WriteDates07() throws Exception {
long start = System.currentTimeMillis();
//1:创建一个工作簿
Workbook workbook = new SXSSFWorkbook();
//2:创建一个工作表
Sheet sheet = workbook.createSheet("阿杰数据大数据测试");
//大批量写入数据
for (int i = 0; i <200000 ; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(i);
}
}
System.out.println("over");
//创建流写入数据
FileOutputStream fileOutputStream = new FileOutputStream(PATH + "阿杰数据07s.xlsx");
//将工作簿通过流写出到指定的文件夹
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
//清除临时文件
((SXSSFWorkbook)workbook).dispose();
long end = System.currentTimeMillis();
//输出运行时间
System.out.println((double) (end-start)/1000);
}
Reader
03版本读文件
@Test
public void test03Reader03() throws Exception {
//通过io流读取文件
FileInputStream fileInputStream = new FileInputStream(PATH + "阿杰数据03.xls");
//创建工作簿
Workbook workbook = new HSSFWorkbook(fileInputStream);
//得到表
Sheet sheetAt = workbook.getSheetAt(0);
//得到行
Row row = sheetAt.getRow(0);
//得到列
Cell cell = row.getCell(0);
//输出内容
/**
* getStringCellValue() 获取字符串类型的
* getNumericCellValue() 获取数字类型的
*
*/
System.out.println(cell.getStringCellValue());
fileInputStream.close();
}
07版本读文件
@Test
public void testReader07() throws Exception {
//通过io流读取文件
FileInputStream fileInputStream = new FileInputStream(PATH + "阿杰数据07.xlsx");
//创建工作簿
Workbook workbook = new XSSFWorkbook(fileInputStream);
//得到表
Sheet sheetAt = workbook.getSheetAt(0);
//得到行
Row row = sheetAt.getRow(0);
//得到列
Cell cell = row.getCell(1);
//输出内容
/**
* getStringCellValue() 获取字符串类型的
* getNumericCellValue() 获取数字类型的
*
*/
System.out.println(cell.getStringCellValue());
fileInputStream.close();
}
读取不知道类型的文件
@Test
public void ReaderType() throws Exception {
//通过io流读取文件
FileInputStream fileInputStream = new FileInputStream(PATH + "会员消费商品明细表.xls");
//创建工作簿
Workbook workbook = new HSSFWorkbook(fileInputStream);
//得到表
Sheet sheetAt = workbook.getSheetAt(0);
//得到行
Row rowTitle = sheetAt.getRow(0);
// 判断这一行是否为空
if (rowTitle!=null){
//获取第一行的数量
int rowTitleNumber = rowTitle.getPhysicalNumberOfCells();
//遍历第一行所有的列
for (int cellNum = 0; cellNum < rowTitleNumber; cellNum++) {
//获取第cellNum个单元格
Cell cell = rowTitle.getCell(cellNum);
// 判断这个单元格数据是否为空
if(cell!=null){
//获取这个单元格的类型
int cellType = cell.getCellType();
//获取这个单元格的内容
String stringCellValue = cell.getStringCellValue();
//输出
System.out.print(stringCellValue+"|");
}
}
System.out.println();
}
//获取第二行的以后的数据
int rowCount = sheetAt.getPhysicalNumberOfRows();
for (int rowNUm = 1; rowNUm < rowCount; rowNUm++) {
// 获取这一行
Row rowData = sheetAt.getRow(rowNUm);
if (rowData !=null){
//读取这一列
int rowTitleNumber = rowTitle.getPhysicalNumberOfCells();
for (int cellNum = 0; cellNum < rowTitleNumber; cellNum++) {
System.out.print("["+(rowNUm+1)+"-"+(cellNum+1)+"]");
//获取这个单元格
Cell cell = rowData.getCell(cellNum);
if (cell!=null){
//获得这个单元格内容的类型
int cellType = cell.getCellType();
String value="";
//对单元格内容的类型进行判断
switch (cellType){
case HSSFCell.CELL_TYPE_STRING: //该单元格内容为字符类型
System.out.print("【字符串】");
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN: //该单元格内容为布尔类型
System.out.print("【布尔】");
value =String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK: //该单元格内容为空类型
System.out.print("【BLANK】");
break;
case HSSFCell.CELL_TYPE_NUMERIC: //该单元格内容为数字类型
System.out.print("【数字】");
if (HSSFDateUtil.isCellDateFormatted(cell)){ //判断是否为日期
System.out.print("【日期】");
Date date = cell.getDateCellValue();
value = new DateTime(date).toString("yyyy-MM-dd"); //格式化写入
}else{
System.out.print("【转换为字符串输出】");
cell.setCellType(HSSFCell.CELL_TYPE_STRING); //转换为字符类型的
value = cell.toString();
}
break;
case HSSFCell.CELL_TYPE_ERROR: //错误类型
System.out.print("【错误】");
break;
}
System.out.println(value); //输出
}
}
}
}
fileInputStream.close();
}