學習的項目是用maven 來管理的。所以需要用到包會在pom.xml裏配置
第一步
1.pom.xml 裏配置
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
2 .導入以下包
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
3. 新建一個test.java文件,在其中建立以下對象:
book = Workbook.getWorkbook(new File("C:/Users/tt/Downloads/風險指數1.xls"));
// 獲得第一個工作表對象
Sheet sheet = book.getSheet(0);
int rows=sheet.getRows();
int columns=sheet.getColumns();
4. 用for循環將excel裏的數據循環取出
5.Example;
public static void importExcel() {
Workbook book = null;
try {
book = Workbook.getWorkbook(new File("C:/Users/tt/Downloads/風險指數1.xls"));
// 獲得第一個工作表對象
Sheet sheet = book.getSheet(0);
int rows=sheet.getRows();
int columns=sheet.getColumns();
// 遍歷每行每列的單元格
for(int i=1;i<32;i++){
String testItem="";
String testName="";
for(int j=1;j<65;j++){
Cell cell = sheet.getCell(j, i);
if(!"".equals(cell.getContents())){
if(j==1){
testItem=testItem+"("+cell.getContents()+",JSONArray.fromObject(\"[{";
// System.out.print("("+cell.getContents()+",JSONArray.fromObject(\"[{");
}
for(int p=0;p<=j;p++){
if(j==(5+3*p)){
testItem=testItem+"\"testName\":\""+cell.getContents()+"\",";
}
if(j==(5+3*p+2)){
testItem=testItem+"\"testValue\":"+cell.getContents()+"},";
}
}
}
if(j==2){
testName=cell.getContents();
}
}System.out.println(testName+testItem+"]\"))"+"//"+testName);
System.out.println("");
}
} catch (Exception e) {
System.out.println(e);
}finally{
if(book!=null){
book.close();
}
}
}