1. 程式人生 > >Android JUnIt Test 解析Excel檔案 pio or jxl

Android JUnIt Test 解析Excel檔案 pio or jxl

package demo;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


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;


import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;


import android.annotation.SuppressLint;
import android.test.AndroidTestCase;
import android.util.Log;


import com.baidu.xcloud.gallery.CloudGalleryClient;
import com.google.gson.Gson;


/**
 * <p>
 * SDK Test Tool Demo
 * </p>
 * 
 * @author chenjinliang01
 * @date 2013-11-14
 */
@SuppressLint("SdCardPath")
public class Demo extends AndroidTestCase {
private String TAG = "GalleryTest";
//    private Business business = null;
    private CloudGalleryClient client = null;
//    private final String myToken = "3.a7c351734f6af86462d3a033e2afe34f.2592000.1378366405.288382192-174538";
    
    //產生隨機的相簿名
//    private Random random = new Random(System.currentTimeMillis());
    
    
    public void setUp() throws Exception {
        print("new request");
//        business = new Business(getContext(), false);
//        business.useAccessToken(getAccessToken());
        
        String uid = "陳錦亮cjl";
        client = new CloudGalleryClient(getContext(), getAccessToken(), uid);
//        client.debug(true);
        
    }
    
    public void tearDown() {
        
    }    
    
    /**
     * test normal create album
     * the album name consist of letters and numbers
     * 
     * @throws IOException
     */
    public void testSum1(Object a, Object b) throws IOException {
    Log.i(TAG, "---------------------------testSum1 start---------------------------");
   
    Log.i(TAG, "test SDK");

//delete this test album

    Log.i(TAG, "---------------------------testSum1 end-----------------------------");
    }
    
    public void testSum2(Object a, Object b) throws IOException {
    Log.i(TAG, "---------------------------testSum2 start---------------------------");
   
    Log.i(TAG, "test SDk");

//delete this test album

    Log.i(TAG, "---------------------------testSum2 end-----------------------------");
    }
    
    /**
     * test all case
     * 
     * @throws IOException
     */
    public void allTest() throws IOException {
    Log.i(TAG, "---------------------------allTest start---------------------------");
   
//    //read the excel
//    String str = "1";
//    Object ob = str;
//    int b = ;
//    
//    Log.i(TAG, "b + 1 = " + (b+1));
   
    TestApi t = new TestApi();

//read param value
Object a = "1";
Object b = "2";

Log.i(TAG, "Result = " + t.sum1(a, b));

//case number
int caseNume = 4;

//open the excel file
//InputStream is = new FileInputStream("/mnt/sdcard/dcim/Camera/Download/xls_test2.xls");  
//Workbook book = Workbook.getWorkbook(new File("/mnt/sdcard/dcim/Camera/Download/xls_test2.xls")); 

//XmlResourceParser xrp = null;
//
//xrp = getContext().getResources().getXml(R.xml.books);
//try {
//while (xrp.next() != XmlResourceParser.START_TAG) {
//continue;
//}
//} catch (XmlPullParserException e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//}
readExcelJXL();

    Log.i(TAG, "---------------------------allTest end-----------------------------");
    }
    
    public void allTest2() throws IOException {
    Log.i(TAG, "---------------------------allTest start---------------------------");
   
    readExcel2003PIO();
   
    Log.i(TAG, "---------------------------allTest end-----------------------------");
    }
    
    /**
     * use jxl to read excel file 
     * but in this way only can read Excel 2003
     * 
     */
    private void readExcelJXL() {

    try {
Workbook workbook = null;
try {
///sdcard/ab.xls
workbook = Workbook
.getWorkbook(new File("/mnt/sdcard/dcim/Camera/Download/xls_test2.xls"));
} catch (Exception e) {
throw new Exception("file to import not found!");
}

Sheet sheet = workbook.getSheet(0);
Cell cell = null;

int columnCount = sheet.getColumns();
int rowCount = sheet.getRows();

for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
//j-column, i-row
cell = sheet.getCell(j, i);

//output
if (cell.getType() == CellType.NUMBER) {
System.out.print(((NumberCell) cell).getValue() + " ");
} else if (cell.getType() == CellType.DATE) {
System.out.print(((DateCell) cell).getDate() + " ");
} else {
System.out.print(cell.getContents() + " ");
}
System.out.print(" ");
}
System.out.print("\n");
}

workbook.close();
} catch (Exception e) {
Log.i(TAG, "Error:" + e.getMessage());
}
}
    
    /**
     * use pio to read excel 2003 file 
     * 
     * @throws IOException
     */
    private void readExcel2003PIO() throws IOException{  
        InputStream is = new FileInputStream("/mnt/sdcard/dcim/Camera/Download/xls_test2.xls");  
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);   
          
        // 迴圈工作表Sheet  
        for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){  
          HSSFSheet hssfSheet = hssfWorkbook.getSheetAt( numSheet);  
          if(hssfSheet == null){  
            continue;  
          }  
            
          // 迴圈行Row   
          for(int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++){  
            HSSFRow hssfRow = hssfSheet.getRow( rowNum);  
            if(hssfRow == null){  
              continue;  
            }  
              
            // 迴圈列Cell    
            for(int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++){  
              HSSFCell hssfCell = hssfRow.getCell( cellNum);  
              if(hssfCell == null){  
                continue;  
              }  
                
              System.out.print("    " + getValue( hssfCell));  
            }  
            System.out.println();  
          }  
        }  
      }
        
      @SuppressWarnings("static-access")  
      private String getValue(HSSFCell hssfCell){  
        if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN){  
          return String.valueOf( hssfCell.getBooleanCellValue());  
        }else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC){  
          return String.valueOf( hssfCell.getNumericCellValue());  
        }else{  
          return String.valueOf( hssfCell.getStringCellValue());  
        }  
      }
    
    
    // test app key: GjOc7V3y6AUYpyh16Xdjn14i
    // get token url: https://openapi.baidu.com/oauth/2.0/authorize?response_type=token&client_id=GjOc7V3y6AUYpyh16Xdjn14i&redirect_uri=oob&scope=netdisk
    //3.a015e19e8928e2f433f16e52c99ac03c.2592000.1377679328.2553241110-174538
    private String getAccessToken() {
        // added your token here
        String myToken = "3.1c3cbc26bffc96f0f25289508be3752e.2592000.1384484225.3675839611-174538";
        
        return myToken;
    }
    
    public void printAsJson(Object o) {
        print(new Gson().toJson(o));
    }
    
    public void printAsString(Object o) {
        print(o.toString());
    }


    public void print(String str) {
        System.out.println(str);
    }


}