Excel导出(poi)
生成excel:
1
2
//创建workbook对象Workbook wb =newHSSFWorkbook();FileOutputStream fileOut =null;try{ //创建sheet Sheet sheet = wb.createSheet("sheet0"); Row row0 = sheet.createRow(0);//创建一行 Cell cell0 = row0.createCell(0); cell0.setCellValue("姓名"); Cell cell1 = row0.createCell(1); cell1.setCellValue("年龄"); Cell cell2 = row0.createCell(2); cell2.setCellValue("手机"); Row row1 = sheet.createRow(1); Cell cell10 = row1.createCell(0); cell10.setCellValue("张三"); Cell cell11 = row1.createCell(1); cell11.setCellValue((short)18); Cell cell12 = row1.createCell(2); cell12.setCellValue("188888888888"); fileOut =newFileOutputStream("D:\\tmp\\workbook.xls"); wb.write(fileOut); fileOut.close(); 解析excel
Workbook wb =newHSSFWorkbook(newFileInputStream("D:\\tmp\\workbook.xls")); Sheet sheet = wb.getSheetAt(0); intcounts = sheet.getLastRowNum(); for(inti=1;i<=counts;i++){ Row row = sheet.getRow(i); System.out.println("姓名:"+row.getCell(0).getStringCellValue()+"年龄:"+row.getCell(1).getNumericCellValue()+"手机:"+row.getCell(2).getStringCellValue()); }}catch(Exception e){ // ignore}
本文由作者按照 CC BY 4.0 进行授权