一、java poi依赖引入
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
二、使用步骤
1.进行写操作
static String PATH="d:\\image\\";
public static void testWriteTest() throws IOException {
//1.创建一个工作簿 最多65546 写入缓存,最后一次写入磁盘,速度快 03版本
// Workbook workbook=new HSSFWorkbook();
//07以后写入速度慢
// Workbook workbook=new XSSFWorkbook();
//07版以后的 优化后的 先写入临时文件(最后需要清理临时文件) 大文件读写速度快
Workbook workbook=new SXSSFWorkbook();
//2.创建一个工作表
Sheet sheet=workbook.createSheet("狂神观众统计表");
//3.创建一个行
Row row1=sheet.createRow(0);
//4.创建一个单元格
Cell cell11=row1.createCell(0);
cell11.setCellValue("今日新增观众");
//(1,2)
Cell cell12=row1.createCell(1);
cell12.setCellValue("666");
//第二行(2,1)
Row row2=sheet.createRow(1);
Cell cell21=row2.createCell(0);
cell21.setCellValue("统计时间");
//(2,1)
Cell cell22=row2.createCell(1);
String time=new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell22.setCellValue(time);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream=new FileOutputStream(PATH+"统计表.xlsx");
workbook.write(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileOutputStream!=null){
fileOutputStream.close();
}
}
}
2.进行读操作(简单)
//获取文件流
FileInputStream inputStream=new FileInputStream("d:\\image\\统计表.xlsx");
//1.创建一个工作簿,使得excel可以操作
Workbook workbook1=new XSSFWorkbook(inputStream);
//得到表
Sheet sheet=workbook1.getSheetAt(0);
//得到行
Row row=sheet.getRow(0);
//得到(0,0)表格
Cell cell=row.getCell(0);
//读取值得时候要注意类型
System.out.println(cell.getStringCellValue());
inputStream.close();
3.批量读取
匹配读取到表格内容的东西,然后转化为String返回
private String matchTheDataType(Cell cell) {
String cellValue="";
//匹配数据类型
if(cell !=null){
CellType cellType= cell.getCellType();
switch (cellType){
case STRING:
cellValue= cell.getStringCellValue();
break;
case _NONE:
break;
case BLANK://如果为空
break;
case NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)){
//如果是日期
Date date= cell.getDateCellValue();
cellValue=new DateTime(date).toString();
}else {
//普通数字
cellValue= cell.toString();
}
case FORMULA:
break;
case BOOLEAN:
cellValue=String.valueOf(cell.getBooleanCellValue());
break;
case ERROR:
System.out.println("数据类型错误");
break;
}
}
return cellValue;
}
批量处理
public void testCellType() throws Exception{
//获取文件流
FileInputStream fileInputStream=new FileInputStream("");
//创建一个工作簿
Workbook workbook=new HSSFWorkbook(fileInputStream);
Sheet sheet=workbook.getSheetAt(0);
//处理表头
Row rowTitle=sheet.getRow(0);
if(rowTitle!=null){
//掌握
int cellCount=rowTitle.getPhysicalNumberOfCells();
for (int i = 0; i < cellCount; i++) {
Cell cell=rowTitle.getCell(i);
if(cell!=null){
CellType cellType=cell.getCellType();
}
}
}
//获取表中得位置
int rowCount=sheet.getPhysicalNumberOfRows();
for(int rowNum=1;rowNum<rowCount;rowNum++){
Row rowData=sheet.getRow(rowNum);
if(rowData!=null){
//读取列
int cellCount=rowTitle.getPhysicalNumberOfCells();
for (int cellNum = 0; cellNum < cellCount; cellNum++) {
Cell cell=rowData.getCell(cellNum);
matchTheDataType(cell);
}
}
}
fileInputStream.close();
}