我有一小段代码,我可以在其中读取CSV文件,然后我的代码会将CSV文件中的帐户转换为IBAN帐户。
不知怎的,这给了我一个错误:
using (var client = new WebService.BANBICSoapClient("IBANBICSoap"))
{
StreamReader sr = new StreamReader(@"C:\CSV\test.csv");
StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv");
while (sr.Peek() >= 0)
{
string line = sr.ReadLine();
try
{
string[] rowsArray = line.Split(';');
line = string.Empty;
string account = rowsArray[0];
string rel = rowsArray[1];
string res = acc;
string rest = acct;
if (resultIBAN != string.Empty && resultBIC != string.Empty)
{
line = account + ";" + res +";" + rest + ";" + rel;
}
else
{
line = account + ";" + "0" + ";" + "0" + ";" + rel;
}
}
catch (Exception msg)
{
Console.WriteLine(msg);
}
sw.WriteLine(line) ;
}
sr.Close();
sw.Close();
}
}
答案 0 :(得分:3)
为您提供流媒体和编剧
StreamReader sr = new StreamReader(@"C:\CSV\test.csv");
StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv");
您可以将其放在方法中:
private void ConvertToIBANAccount(string fileName,string outFileName)
{
using (var client = new WebService.BANBICSoapClient("IBANBICSoap"))
{
StreamReader sr = new StreamReader(fileName);
StreamWriter sw = new StreamWriter(outFileName);
Console.WriteLine("Bezig met converteren....");
while (sr.Peek() >= 0)
{
string line = sr.ReadLine();
try
{
string[] rowsArray = line.Split(';');
line = string.Empty;
string account = rowsArray[0];
string relationID = rowsArray[1];
string resultIBAN = client.BBANtoIBAN(account);
string resultBIC = client.BBANtoBIC(account);
if (resultIBAN != string.Empty && resultBIC != string.Empty)
{
line = account + ";" + resultIBAN +";" + resultBIC + ";" + relationID;
}
else
{
line = account + ";" + "0" + ";" + "0" + ";" + relationID;
}
}
catch (Exception msg)
{
Console.WriteLine(msg);
}
sw.WriteLine(line) ;
}
sr.Close();
sw.Close();
}
Console.WriteLine("Conversie succesvol");
Console.ReadLine();
}
}
然后找到扩展名.csv
的每个文件string[] files = Directory.GetFiles(yourDirectoryName, "*.csv");
files.ToList().ForEach(f => ConvertToIBANAccount(f,"temp" + f));
答案 1 :(得分:2)
你可以像这样阅读所有csv文件
var filePaths = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")
foreach(string s in filePaths)
{
using (StreamReader sr = new StreamReader(s))
{
//perform task related file
}
}
答案 2 :(得分:1)
做这样的事......
List<string> filesList = Directory.GetAllFiles("*.csv", FolderLocationToSearch).ToList();
foreach(var fileName in fileList)
{
ConvertToIBANfromCSV(fileName);
}
public static void ConvertToIBANfromCSV(string fileName)
using (var client = new WebService.BANBICSoapClient("IBANBICSoap"))
{
StreamReader sr = new StreamReader(fileName);
StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv");
Console.WriteLine("Bezig met converteren....");
while (sr.Peek() >= 0)
{
string line = sr.ReadLine();
try
{
string[] rowsArray = line.Split(';');
line = string.Empty;
string account = rowsArray[0];
string relationID = rowsArray[1];
string resultIBAN = client.BBANtoIBAN(account);
string resultBIC = client.BBANtoBIC(account);
if (resultIBAN != string.Empty && resultBIC != string.Empty)
{
line = account + ";" + resultIBAN +";" + resultBIC + ";" + relationID;
}
else
{
line = account + ";" + "0" + ";" + "0" + ";" + relationID;
}
}
catch (Exception msg)
{
Console.WriteLine(msg);
}
sw.WriteLine(line) ;
}
sr.Close();
sw.Close();
}
Console.WriteLine("Conversie succesvol");
Console.ReadLine();
}
如果我手动编写代码有语法错误,请原谅我。