Operation for Excel with NPOI and Excel.Load

本文介绍了一种使用C#操作Excel的方法,包括定义映射类、读取Excel数据及转换URI等关键步骤。通过示例代码,展示了如何从Excel中提取特定列的数据并进行处理。

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)

}

 

转载于:https://www.cnblogs.com/jessicaxia/p/9361022.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值