标签: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