最近项目中常用到Excel的读写,因此想写个总结
Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。 .NET的开发人员则可以利用NPOI (POI for .NET) 来存取 Microsoft Office文档的功能。
包名称说明
HSSF提供读写Microsoft Excel XLS格式档案的功能。
XSSF提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF提供读写Microsoft Word DOC格式档案的功能。
HSLF提供读写Microsoft PowerPoint格式档案的功能。
HDGF提供读Microsoft Visio格式档案的功能。
HPBF提供读Microsoft Publisher格式档案的功能。
HSMF提供读Microsoft Outlook格式档案的功能。
类名 说明
HSSFWorkbook Excel的文档对象
HSSFSheet
Excel的表单
HSSFRow
Excel的行
HSSFCell
Excel的格子单元
HSSFFont Excel字体
HSSFDataFormat 格子单元的日期格式
HSSFHeader Excel文档Sheet的页眉
HSSFFooter Excel文档Sheet的页脚
HSSFCellStyle 格子单元样式
HSSFDateUtil 日期
HSSFPrintSetup 打印
SpringBoot实现xls和xlsx类型的Excel读写的栗子
1、引入相关依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
2、读写的实现
//读取Excel
public static List getExcelData(String path){
List<Data> excelDataList=new ArrayList<>();
try {
//获取文件输入流
InputStream inputStream=new FileInputStream(path);
//获取工作表对象
Workbook workbook=null;
//获取Excel工作薄对象
Sheet sheet=null;
if (path.endsWith("xls")) {
workbook = new HSSFWorkbook(inputStream);
sheet =workbook.getSheetAt(0);
}else if (path.endsWith("xlsx")){
workbook = new XSSFWorkbook(inputStream);
sheet =workbook.getSheetAt(0);
}
//循环读取表格数据
for(Row row:sheet){
if (row.getRowNum()==0)
continue;
//读取当前行数据
Cell numRow= (Cell) row.getCell(0);
//设置单元格类型方便读取
numRow.setCellType(CellType.STRING);
Cell scoreRow= (Cell) row.getCell(1);
//设置单元格类型方便读取
scoreRow.setCellType(CellType.STRING);
String num=row.getCell(0).getStringCellValue();
String score=row.getCell(1).getStringCellValue();
Cell ageRow= (Cell) row.getCell(2);
//设置单元格类型方便读取
ageRow.setCellType(CellType.STRING);
String age=row.getCell(2).getStringCellValue();
Data data=new Data();
data.setNum(num);
data.setScore(score);
data.setAge(Integer.valueOf(age));
excelDataList.add(data);
}
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
return excelDataList;
}
//导出Excel
public static void outputExcell(List<Data> list,String filename){
//在内存中创建Excel文件
Workbook workbook=null;
if (filename.endsWith("xls")){
workbook=new HSSFWorkbook();
}else if (filename.endsWith("xlsx")){
workbook=new XSSFWorkbook();
}
Sheet sheet=workbook.createSheet();
//标题行
Row titleRow=sheet.createRow(0);
titleRow.createCell(0).setCellValue("编号");
titleRow.createCell(1).setCellValue("排行");
titleRow.createCell(2).setCellValue("年龄");
//创建数据行并写入值
for (Data data:list) {
int lastRowNum=sheet.getLastRowNum();
Row dataRow=sheet.createRow(lastRowNum+1);
dataRow.createCell(0).setCellValue(data.getNum());
dataRow.createCell(1).setCellValue(data.getScore());
dataRow.createCell(3).setCellValue(data.getAge());
}
//创建输出流对象
FileOutputStream outputStream=null;
try {
outputStream=new FileOutputStream(filename);
workbook.write(outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}