• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
pealy
博客园    首页    新随笔    联系   管理    订阅  订阅

Operation for Excel with NPOI and Excel.Load

Main function to operate Excel.

1. Define a mapper class for Excel. eg:

class ExcelColMapper

{

[cloumn (index=10)]

public string basePathCol{get;set;}

[cloumn (index=11)]

public string extPathCol{get;set;}

[cloumn (index=13)]

public string verbCol{get;set;}

}

 

2. define a ExcelReader used to retrieve data from Excel

FileStream fStream;

string fileName;

Public ExcelReader(string filename)

{

  filename=fileName;

//XSSFWorkbook abook;

using(FileStream file=new FileStream(filename,FileMode.Open,FileAccess.Read))

{

abook=new XSSFWorkbook(file);

try{

for(int row=1;row<=sheet.LastRowNum;row++)

{

List<EndPointExcelColMapper> results = new List<EndPointExcelColMapper>();

results.Add(new EndPointExcelColMapper()

{

  jsonRequest=sheet.GetRow(row).GetCell(0).StringCellValue,

 cdfResponse=sheet.GetRow(row).GetCell(2).StringCellValue,

jsonResponse=sheet.GetRow(row).GetCell(3).StringCellValue

}

Catch(Exception ex)

{

throw ex;

}

}

}

}

}

public List<EndPtCDFRespMapping> retrieveuriTOCDFobjMapping()

{

List<ExcelColMapper> result=.Load<ExcelColMapper>(filename,1,0).ToList();

List<EndPtCDFRespMapping> fullMapping = new List<EndPtCDFRespMapping>();

 results.RemoveAll(s=>s.typeCol==null);

result.RemoveAll(s=>s.typpeCol.ToString()=="Request");

for(int n=0;n<results.count;n++)

{

string UriFromSwagger=UriTransform(result[n].basePathCol+result[n].extPathCol).Item1;

fullMapping.Add(new EndPtCDFResMapping(uriFromSwagger,result[n].verbCol,result[n].cdfNameSpaceCol+result[n].cdfObjCpl));

}

return fullMapping;

}

 private Tuple<string,int> uriTransform(string fulluritemp)

{

string fulluri=fulluritemp.Substring(0,fulluritemp.Length-1);

string pattern=@"\d{2,}|(\{[^0-9].*?})";

Regex regExp=new Regex(pattern);

string tempuri="";

int i=0;

string result=fulluri;

do

{

tempuri=result;

string replacement ="{"+i+"}";

result=regExp.Replace(result,replacement,1);

i++;

} while(!result.Equals(tempuri));

return new Tuple<string,int>(result,i-1)

}

 

posted @ 2018-07-24 17:55  jessicaxia  阅读(206)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3