我有一个用于向服务器提交 CSV 文件的网页。我必须验证文件,例如正确的列数、正确的数据类型、跨字段验证、数据范围验证等。最后要么显示成功消息,要么返回包含错误消息和行号的 CSV。
目前循环遍历每一行和每一列,以找出 CSV 文件中的所有错误。但是对于更大的文件,它会变得非常慢,有时会导致服务器超时。有人可以建议一个更好的方法来做到这一点。
谢谢
最佳答案
要验证 CSV 文件,您肯定需要检查每一列。在您的场景中,如果可能,唯一的最佳方法是在附加到 CSV 文件时验证条目本身。
编辑
由于@accolaum 指出了一个错误,我已经编辑了我的代码
It will only work provided each row is delimited with a `\n`
IF you only want to Validate number of Columns.. then its easier.. Just take the mod of all the entries with the num of columns
bool file_isvalid;
string data = streamreader.ReadLine();
while(data != null)
{
if(data.Split(',').Length % Num_Of_Columns == 0)
{
file_isvalid = true;
//Perform opertaion
}
else
{
file_isvalid = false;
//Perform Operation
}
data = streamreader.ReadLine();
}
希望对你有帮助
关于c# - 验证 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4755533/