---------读取EXCEL类
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcel
{
public static List<String[]> getSheetMap(File file) throws BiffException, IOException, ServletException
{
Workbook book = Workbook.getWorkbook(file);
Sheet[] sheetList = book.getSheets();// 获得工作表对象数组
String[] mes = null;
Sheet sheets = sheetList[0];
int cols = sheets.getColumns();// col列数
int rows = sheets.getRows();// 行数
int nullcols = 0;
List<String[]> cellList = new ArrayList<String[]>();// NEW一个单元格集合
for (int i = 0; i < rows; i++)
{
mes = new String[cols];// 每一行NEW一个新的列数组,cols是列的长度
nullcols = 0;
for (int j = 0; j < cols; j++)
{
String cur = sheets.getCell(j, i).getContents();// 从EXCEL取得每个单元格中的数据
if (null == cur || "" == cur)
{
nullcols++;
}
mes[j] = cur;// 将单元格的数据存入列数组中
}
if (cols != nullcols)
cellList.add(mes);// 将每列的数组存入每个页的集合中
}
book.close();
return cellList;
}
}
追问在public static void main里面怎么把excel数列中的前三列取出来并赋值给a1a2,a3?
追答public static void main(String args[]){
List infoList = new ArrayList();
File file = new File("F:\\as.xls");
try {
infoList=ReadExcel.getSheetMap(file);
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String [] mes = null;
for(int i = 0 ; i< infoList.size() ; i++){
mes = infoList.get(i);
System.out.println(mes[0]);
}
}
本回答被提问者采纳