首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

用poi 读取写下xls 文件

2012-11-08 
用poi 读取写入xls 文件读取文件InputStream input//try {//input new FileInputStream(D:\\nkc\\poi.

用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();
}
}

热点排行