POI导入导出小案例
一、HSSF 97-2003 需要jar:poi-3.9.jar 简单示例:生成EXCEL //93---2003 String [] titlie={"id","name","sex"}; //创建Excel工作簿 HSSFWorkbook workBook=new HSSFWorkbook(); //创建一个Sheet页 HSSFSheet sheet=workBook.createSheet(); //创建第一行 HSSFRow row=sheet.createRow(0); //定义单元格 HSSFCell cell=null; //插入第一行数据id,name,sex for (int i = 0; i < titlie.length; i++) { cell=row.createCell(i); cell.setCellValue(titlie[i]); } //追加数据 for(int i=1;i<10;i++){ HSSFRow nextrow=sheet.createRow(i); HSSFCell cell2=nextrow.createCell(0); cell2.setCellValue("a"+i); cell2=nextrow.createCell(1); cell2.setCellValue("user"+i); cell2=nextrow.createCell(2); cell2.setCellValue("男"); } //创建一个文件 File file=new File("C:/Users/Administrator/Desktop/POI_test.xls"); try { file.createNewFile(); FileOutputStream stream=FileUtils.openOutputStream(file); workBook.write(stream); stream.close(); System.out.println(">>>结束"); } catch (IOException e) { e.printStackTrace(); } 解析EXCEL //93---2003 //引入需要解析的Excel文件 File file=new File("C:/Users/Administrator/Desktop/POI_test.xls"); try { //创建Excel,读取文件内容 HSSFWorkbook workbook= new HSSFWorkbook(FileUtils.openInputStream(file)); //HSSFSheet sheet=workbook.getSheet("");//名称或者(获取第一个工作表) HSSFSheet sheet=workbook.getSheetAt(0); int firstRowNum=0; //获取sheet中最后银行行号 int lastRowNum=sheet.getLastRowNum(); for (int i = firstRowNum; i < lastRowNum; i++) { HSSFRow row=sheet.getRow(i); //获取当前行最后单元格列号 int lastCellNum=row.getLastCellNum(); for(int j=0;j<lastCellNum;j++){ HSSFCell cell=row.getCell(j); String value=cell.getStringCellValue(); System.err.print(value+" >>"); } System.err.println(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
-----------------------------------------------------------------------------------------------------------
2007------生成EXCEL 需要JAR: //2007 String [] titlie={"id","name","sex"}; //创建Excel工作簿 XSSFWorkbook workBook=new XSSFWorkbook(); //创建一个Sheet页 Sheet sheet=workBook.createSheet(); //创建第一行 Row row=sheet.createRow(0); //定义单元格 Cell cell=null; //插入第一行数据id,name,sex for (int i = 0; i < titlie.length; i++) { cell=row.createCell(i); cell.setCellValue(titlie[i]); } //追加数据 for(int i=1;i<10;i++){ Row nextrow=sheet.createRow(i); Cell cell2=nextrow.createCell(0); cell2.setCellValue("a"+i); cell2=nextrow.createCell(1); cell2.setCellValue("user"+i); cell2=nextrow.createCell(2); cell2.setCellValue("男"); } //创建一个文件 File file=new File("C:/Users/Administrator/Desktop/POI_test07Last.xlsx"); try { file.createNewFile(); FileOutputStream stream=FileUtils.openOutputStream(file); workBook.write(stream); stream.close(); System.out.println(">>>结束"); } catch (IOException e) { e.printStackTrace(); } 相关jar下载地址 http://download.csdn.net/detail/yjaspire/9534540