Apache POI常用的类:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC97格式档案的功能。
XWPF - 提供读写Microsoft Word DOC2003格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。
1.创建Excel表格
//创建文件
public static void createExcel() throws IOException {
//从桌面获取真实路径
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
String path = fileSystemView.getHomeDirectory().getPath();
//设置文件路径
String filePath = path + "/测试.xls";//这里后缀是.xls 类一定用HSSF(参考常用类对应)
//根据路径创建一个新文件
File file = new File(filePath);
FileOutputStream outputStream = new FileOutputStream(file);
HSSFWorkbook sheets = new HSSFWorkbook();
//创建一个新表
HSSFSheet sheet1 = sheets.createSheet("客户管理");
//创建第一行
HSSFRow row1 = sheet1.createRow(0);
//填字段
row1.createCell(0).setCellValue("id");
row1.createCell(1).setCellValue("name");
row1.createCell(2).setCellValue("sex");
row1.createCell(3).setCellValue("phone");
row1.createCell(4).setCellValue("QQ");
row1.createCell(5).setCellValue("Email");
row1.createCell(6).setCellValue("parentId");
row1.createCell(7).setCellValue("count");
//创建第二行
HSSFRow row2 = sheet1.createRow(1);
//填写数据
row2.createCell(0).setCellValue("1");
row2.createCell(1).setCellValue("尉某人");
row2.createCell(2).setCellValue("男");
row2.createCell(3).setCellValue("189****7950");
row2.createCell(4).setCellValue("11468***98");
row2.createCell(5).setCellValue("YLL130****8233@163.com");
row2.createCell(6).setCellValue(0);
row2.createCell(7).setCellValue(135);
sheets.setActiveSheet(0);
sheets.write(outputStream);
outputStream.close();
System.out.println("=================");
System.out.println("创建成功");
System.out.println("=================");
}
2.解析Excel内容
//解析文件
public static void lookExcel() throws IOException {
//获取文件路径
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
String path = fileSystemView.getHomeDirectory().getPath();
String filePath = path + "/测试.xls";//注意后缀
//将文件转化成流
FileInputStream fileInputStream = new FileInputStream(filePath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(bufferedInputStream);
HSSFWorkbook sheets = new HSSFWorkbook(poifsFileSystem);
//获取哪个sheet内容
HSSFSheet sheel1 = sheets.getSheet("客户管理");
//获取行数
int rowNum = sheel1.getLastRowNum();
System.out.println("====================");
System.out.println(rowNum);
for (int i = 0; i <= rowNum; i++) {
HSSFRow row = sheel1.getRow(i);
if (row == null){
System.out.println("没有了");
break;
}
//获取列数
short cellNum = row.getLastCellNum();
System.out.println(cellNum);
System.out.println("====================");
for (int j = 0; j < cellNum ; j++) {
//这里根据需要修改解析结果类型(我全部解析成string类型了)
row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
String cellValue = row.getCell(j).getStringCellValue();
System.out.println("***********************");
System.out.println(cellValue);
System.out.println("***********************");
}
}
bufferedInputStream.close();
}
3.测试创建Excel表格和解析Excel数据
需要哪个方法放开就可以(“Ctrl + /”)
@Test
public static void main(String[] args) {
// //创建表
// try {
// poiTest.createExcel();
// System.out.println("真的创建成功♪(^∀^●)ノ");
// } catch (IOException e) {
// e.getMessage();
// System.out.println("创建失败(づ╥﹏╥)づ");
// }
//读取表数据
try {
poiTest.lookExcel();
System.out.println("解析成功了♪(^∀^●)ノ");
} catch (IOException e) {
e.getMessage();
System.out.println("解析失败(づ╥﹏╥)づ");
}
}