是这样的,我只想读取Excel的第二列和第四列,我不知道怎么样获取,希望知道的告诉我下,谢了,我的代码如下但是我这种方法解析有问题:package com.aiflow.zxb.parse;import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.aiflow.zxb.bean.Bean;import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;public class ParseExcel {
public static void main(String[] args) {
List<Bean> data = new ArrayList<Bean>();
List list = new ArrayList();
try {
// 文件路径
InputStream is = new FileInputStream("E:/需求/智能网设备码核查/标准数据/SCP设备数据汇总表.xls");
Workbook wb = Workbook.getWorkbook(is);
Sheet sheet = wb.getSheet(0); // 第几张表格从零开始
String sheetName = sheet.getName();
System.out.println("sheet表名(工作薄名): " + sheetName);
if (sheet != null) {
// 获取表格总列数
int rsColumns = sheet.getColumns();
System.out.println("表格总列数rsColumns = " + rsColumns); // 获取表格总行数
int rsRows = sheet.getRows();
System.out.println("表格总行数 rsRows = " + rsRows);
for (int rowNum = 1; rowNum < rsRows; rowNum++) {
Cell[] cells = sheet.getRow(rowNum);
// for (int i = 0; i < rsColumns; i++) {
Bean bean = new Bean();
System.out.println(getExcelColumnLabel(4)+"============"+cells[2].getContents());
bean.setQuhao(Integer.parseInt(cells[2].getContents()));
// System.out.println(cells[2].getContents()+"====="+cells[4].getContents());
bean.setMSC_ID(Integer.parseInt(cells[4].getContents()));
list.add(bean);
data.add(bean);
}//列数
// }
wb.close();
}
} catch (Exception e) {
// TODO: handle exception
}
for (int i = 0; i < data.size(); i++) {
// System.out.println(data.get(i));
}
}
/**
* 获取excel对应列的字母
* @author zxb
* @param num
* @return
*/
public static String getExcelColumnLabel(int num){
String temp="";
double i=Math.floor(Math.log(25.0*(num)/26.0+1)/Math.log(26))+1;
if(i>1){
double sub=num-26*(Math.pow(26, i-1)-1)/25;
for(double j=i;j>0;j--){
temp= temp+(char)(sub/Math.pow(26, j-1)+65);
sub=sub%Math.pow(26, j-1);
}
}else{
temp= temp+(char)(num+65);
}
return temp;
}}
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.aiflow.zxb.bean.Bean;import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;public class ParseExcel {
public static void main(String[] args) {
List<Bean> data = new ArrayList<Bean>();
List list = new ArrayList();
try {
// 文件路径
InputStream is = new FileInputStream("E:/需求/智能网设备码核查/标准数据/SCP设备数据汇总表.xls");
Workbook wb = Workbook.getWorkbook(is);
Sheet sheet = wb.getSheet(0); // 第几张表格从零开始
String sheetName = sheet.getName();
System.out.println("sheet表名(工作薄名): " + sheetName);
if (sheet != null) {
// 获取表格总列数
int rsColumns = sheet.getColumns();
System.out.println("表格总列数rsColumns = " + rsColumns); // 获取表格总行数
int rsRows = sheet.getRows();
System.out.println("表格总行数 rsRows = " + rsRows);
for (int rowNum = 1; rowNum < rsRows; rowNum++) {
Cell[] cells = sheet.getRow(rowNum);
// for (int i = 0; i < rsColumns; i++) {
Bean bean = new Bean();
System.out.println(getExcelColumnLabel(4)+"============"+cells[2].getContents());
bean.setQuhao(Integer.parseInt(cells[2].getContents()));
// System.out.println(cells[2].getContents()+"====="+cells[4].getContents());
bean.setMSC_ID(Integer.parseInt(cells[4].getContents()));
list.add(bean);
data.add(bean);
}//列数
// }
wb.close();
}
} catch (Exception e) {
// TODO: handle exception
}
for (int i = 0; i < data.size(); i++) {
// System.out.println(data.get(i));
}
}
/**
* 获取excel对应列的字母
* @author zxb
* @param num
* @return
*/
public static String getExcelColumnLabel(int num){
String temp="";
double i=Math.floor(Math.log(25.0*(num)/26.0+1)/Math.log(26))+1;
if(i>1){
double sub=num-26*(Math.pow(26, i-1)-1)/25;
for(double j=i;j>0;j--){
temp= temp+(char)(sub/Math.pow(26, j-1)+65);
sub=sub%Math.pow(26, j-1);
}
}else{
temp= temp+(char)(num+65);
}
return temp;
}}
for(int i=1;i<sheet.getRows();i++){ // 循环excel所有数据行,从1开始,0行一般是表头
// sheet.getCell(column, row); sheet.getCell(列索引,行索引)
//列也是从0开始
sheet.getCell(1, i).getContents();
sheet.getCell(3, i).getContents(); // 此处再做你自己想做的逻辑}
http://jexcelapi.sourceforge.net/resources/javadocs/current/docs/