package com.smp.server.Ctrl;
import java.io.File;
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import com.smp.server.model.TxlBean;
import jxl.Sheet;
import jxl.Workbook;import jxl.read.biff.BiffException;public class GetExcelInfo { public static void main(String[] args) { GetExcelInfo obj = new GetExcelInfo(); obj.readExcel("E:/txl.xls"); } // 去读Excel的方法readExcel,该方法的入口参数为一个File对象 public List<TxlBean> readExcel(String filepath) { File file = new File(filepath); List<TxlBean> txlList = new ArrayList<TxlBean>(); try { // 创建输入流,读取Excel InputStream is = new FileInputStream(file.getAbsolutePath()); // jxl提供的Workbook类 Workbook wb = Workbook.getWorkbook(is); // Excel的页签数量 int sheet_size = wb.getNumberOfSheets(); for(String str:wb.getSheetNames()){ System.out.println("===========str=========="+str); } for (int index = 0; index < sheet_size; index++) { //获得页签名称 String sheetnames[] = wb.getSheetNames(); // 每个页签创建一个Sheet对象 Sheet sheet = wb.getSheet(index); // sheet.getRows()返回该页的总行数 //第一行列名称,不读取 String deptname=""; for (int i = 1; i < sheet.getRows(); i++) { TxlBean txlbean = new TxlBean(); //机构 if(null!=sheet.getCell(0, i).getContents()&&!sheet.getCell(0, i).getContents().equals("")){ deptname=sheet.getCell(0, i).getContents(); }else{ //不变 } //联系人 if((null!=sheet.getCell(1, i).getContents()&&!sheet.getCell(1, i).getContents().equals(""))&&(null!=sheet.getCell(3, i).getContents()&&!sheet.getCell(3, i).getContents().equals(""))) { txlbean.setBz(sheetnames[index]);//所属分类 txlbean.setJg(deptname);//机构名 txlbean.setLxr(sheet.getCell(1, i).getContents());//联系人 txlbean.setSjh(sheet.getCell(3, i).getContents());//手机号 txlList.add(txlbean); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return txlList; } }