POI 读取EXCEL实现程序
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-28 11:24:20 浏览: 评论:0
- /*
- * 使用POI读取EXCEL文件
- */
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.ArrayList;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- /**
- *
- * @author Hanbin
- */
- public class ReadExcel {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)throws Exception {
- read("d:\demo.xls");
- }
- public static ArrayList read(String fileName){
- ArrayList list = new ArrayList();
- String sql = "";
- try{
- File f = new File(fileName);
- FileInputStream fis = new FileInputStream(f);
- HSSFWorkbook wbs = new HSSFWorkbook(fis);
- HSSFSheet childSheet = wbs.getSheetAt(0);
- System.out.println("行数:" + childSheet.getLastRowNum());
- for(int i = 4;i<childSheet.getLastRowNum();i++){
- HSSFRow row = childSheet.getRow(i);
- System.out.println("列数:" + row.getPhysicalNumberOfCells());
- if(null != row){
- for(int k=1;k<row.getPhysicalNumberOfCells();k++){
- HSSFCell cell;
- cell = row.getCell((short)k);
- // System.out.print(getStringCellValue(cell) + "t");
- list.add(getStringCellValue(cell) + "t");
- }
- }
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- return list;
- }
- /**
- * 获取单元格数据内容为字符串类型的数据
- *
- * @param cell Excel单元格
- * @return String 单元格数据内容
- */
- private static String getStringCellValue(HSSFCell cell) {
- String strCell = "";
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_STRING:
- strCell = cell.getStringCellValue();
- break;
- case HSSFCell.CELL_TYPE_NUMERIC:
- strCell = String.valueOf(cell.getNumericCellValue());
- break;
- case HSSFCell.CELL_TYPE_BOOLEAN:
- strCell = String.valueOf(cell.getBooleanCellValue());
- break;
- case HSSFCell.CELL_TYPE_BLANK:
- strCell = "";
- break;
- default:
- strCell = "";
- break;
- }
- if (strCell.equals("") || strCell == null) {
- return "";//开源代码phpfensi.com
- }
- if (cell == null) {
- return "";
- }
- return strCell;
- }
- }
Tags: POI读取EXCEL PHP读取EXCEL
- 上一篇:php文件缓存类文件
- 下一篇:把php生成静态(html)页面程序代码
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)