码迷,mamicode.com
首页 > 其他好文 > 详细

Operation for Excel with NPOI and Excel.Load

时间:2018-07-24 19:15:34      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:bsp   mapper   files   excel   item   sheet   string   work   class   

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)

}

 

Operation for Excel with NPOI and Excel.Load

标签:bsp   mapper   files   excel   item   sheet   string   work   class   

原文地址:https://www.cnblogs.com/jessicaxia/p/9361022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!