用poi 读取写入xls 文件

读取文件
InputStream input;
// try {
// input = new FileInputStream("D:\\nkc\\poi.xls");
// POIFSFileSystem fs = new POIFSFileSystem(input);
// HSSFWorkbook wb = new HSSFWorkbook(fs);
//     HSSFSheet sheet = wb.getSheetAt(0);
//     Iterator rows = sheet.rowIterator();
//       while (rows.hasNext())
//       {
//       HSSFRow row = (HSSFRow) rows.next();
//       Iterator cells = row.cellIterator();
//       while (cells.hasNext())
//           {
//             HSSFCell cell = (HSSFCell) cells.next();
//             switch (cell.getCellType())
//             {
//             case HSSFCell.CELL_TYPE_STRING:
//             System.out.println("AA"+cell.getStringCellValue().toUpperCase());
//
//             break;
//
//             case HSSFCell.CELL_TYPE_NUMERIC:
//             break;
//
//             }
//           }
//
//       }
//    
//    
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }







写入文件

FileOutputStream fos;
try {
fos = new FileOutputStream("D:\\nkc\\poi.xls");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
wb.setSheetName(2, "XX");
HSSFRow row = null;
HSSFCell cell = null;
HSSFRichTextString hts= null;
row = s.createRow(0);
cell = row.createCell((int)0,0);
hts=new HSSFRichTextString("ID");
        cell.setCellValue(hts);
  
                           for (int i=1;i<lstBeans.size();i++) {
row = s.createRow(i);
cell = row.createCell((int)0,0);
hts=new HSSFRichTextString(lstBeans.get(i).getId()+"");
    cell.setCellValue(hts);
   }
wb.write(fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

你可能感兴趣的:(poi)