读取文件
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();
}
}
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();
}
}
本文介绍了如何使用Java POI库来读取和写入Excel文件。通过示例代码展示了创建工作簿、工作表及单元格的过程,并演示了读取单元格数据的方法。
1364

被折叠的 条评论
为什么被折叠?



