把数据导入到本地的EXcel中和读取本地的EXCEL到list中

public class TestCaseManager{
      public static final String title_caseID="测试例ID";
      public static final String title_caseName="测试名";
      public static final String title_testResult="测试结果";
      public static final String title_testTime ="测试通过时间";
      public static final String [] titles= new String[]{title_caseID,title_caseName,title_testResult,title_testTime };
    public static void generExcel(List<TestCase> list , String path){
    File file = new File (path);
    if(!file.exists()){
           createExcel(list,path);
              }else{
              List <TestCase> list2 readExcel(path);
              int listNumber list2.size();
             for(int i=0;i<listNumber;i++){
                      TestCase testcase =list2.get(i);
                          list.add(testcase);  
                      }
                       createExcel(list,path);
                   }
    }
      public static void createExcel(List<TestCase> list ,String path){
         HSSFWorkbook workbook = new HSSFWorkbook();
         Format format = new SimpleDateFormat("yyyy-MM-dd");
         String sheetName = format.format(new Date());
         HSSFSheet sheet = workbook.createSheet(sheetName);
         HSSFRow row =sheet.createRow(0);
HSSFCell cell = null;
int titleNumber = titles.length;
for (int i=0;i<titleNumber;i++){
cell = row.createCell(i);
cell.setCellValue(titles[i];
}
int rowNumber = list.size();
for(int i=1;i<=rowNumber;i++){
HSSFRow nextRow = sheet.createRow(i);
for(int j=0;j<titleNumber;j++){
cell=nextRow.createCell(j);
TestCase testCase= list.get(i-1);
if(titles[j].equals(title_caseId)){
cell.setCellValue(testCase.getCaseId());
}else if(titles[j].equals(title_caseNameSpace)){
cell.setCellValue(testCase.getCaseNameSpace());
}else if (titles[j].equals(title_testResult)){
cell.setCellValue(testCase.getTestResult());
}else if(titles[j].equals(title_testTime)){
cell.setCellValue(testCase.getTestTime());
}

}
}
File file = new File(path);
FileOutputStream stream =null;
try{
stream = new FileOutputStream(file);
workbook.write(stream);
stream.close();
}catch(IOException e){
e.printStackTrace();

}finally{
try{
if(stream!=null)
stream.close();
}catch(IOException e ){
e.printStackTrace();
}
}
}
public static List<TestCase> readExcel(String path){
List<TestCase> list = new ArrayLIst<TestCase>();
TestCase testCase= new TestCase();
InputStream stream = null;
try{
stream = new FileInputStream(path);
HSSFWorkbook workbook = new HSSFWorkbook(stream);
Format format new SimpleDateFormat("yyyy-MM-dd");
String sheetName = format.format(new Date());
HSSFSheet sheet =workbook.getSheet(sheetName);
int lastRowNumber sheet.getLastRowNum();
for(int i=1;i<lastRowNumber; i++){
HSSFRow row sheet.getROw(i);
int lastCellNumber = row.getLastCellNum90;
for(int j=0; j<lastCellNumber; j++){
HSSFCell cell = row.getCell(j);
if(cell==null){
throw new RuntimeException(sheet.getSheetName()+"不是别的行:"+row.getRowNum());



}
String value = null;
int type = cell.getCellType();
if(type==Cell.CELL_TYPE_BLANK){
value="";
}else if(type==Cell.CELL_TYPE_BOOLEAN){

value=cell.getBooleanCellValue()+"";
}else if(type==Cell.TYPE_ERROR){
value= cell.getErrorCellValue()+"";
}else if(type==Cell.CELL_TYPE_FORMULA){
throw new RuntimeException("不是别的单元格格式");
}else if(type==Cell.CELL_TYPE_NUMERIC){
value = cell.getNumericCellValue()+"";
}else if(type==Cell.CELL_TYPE_SPRING){
value= cell.getStringCellValue();
}
if(j==0){
testCase.setCaseId(value);
}else if(j==1){
testCase.setCaseNameSpace(value);
}else if(j==2){
testCase.setTestResult(value);
}else if (j==3){
testCase.setTime(value);
}
}
list.add(testCase);
}
}catch(Exception e ){
e.printStackTrace();
} return list;
} }
原文地址:https://www.cnblogs.com/zhangxuesong/p/5232014.html