处理CSV文件 [英] Processing CSV File
问题描述
朋友,
在我正在开发的应用程序中,我正在处理CSV文件.
实际上,过程是,用户将下载网站的Excel模板并将其输入到Excel File,然后将excel文件上传到系统以将记录处理到sql.
从我的应用程序逻辑.我正在使用文件流将上载的文件存储在SQL中,然后在开始处理时下载文件流,将其转换为CSV(管道分隔),然后处理该文件,到目前为止,我在处理过程中没有遇到任何问题,但是只有一列在excel中是( Description ),用户将复制粘贴该列,现在该列具有换行符和段落换行符,因此当我处理该文件并使用Stream Reader Readline方法逐行读取文件时,当遇到中断时将其视为结束行
请任何人提出以下解决方案是正确理解的代码
string fileinfo = ExcelConversion(FileName, FileID.Value);//Converion of Excel File to CSV byte[] output = UTF8Encoding.UTF8.GetBytes(fileinfo);//since the values will have chinese or japanese characters MemoryStream stream = new MemoryStream(output); using (StreamReader reader = new StreamReader(stream)) { char[] splitchar; splitchar = new char[] { ''|'' }; SrcUpperBoundColumnIndex =Src_Excel_Issedetails._021_TargetCompletionDate; DataTable IssueUploaded = CreateDataTable(reader.ReadLine()); string strIssueRecord; while ((strIssueRecord = reader.ReadLine()) != null) { string[] Columns = strIssueRecord.Split(splitchar); if (Columns.GetUpperBound(0) == (SrcUpperBoundColumnIndex.GetHashCode() + 1)) { AddRow(Columns); } else { throw new Exception("File upload failed. Invalid column count found."); } }
在处理-Stream Reader时,您可以使用Trim Function删除此类换行符和参数
>
Hi Friends ,
In my application which i am developing i am processing a CSV File.
Actually the Process is , the user will download a Excel Template for Website and make entries to Excel File and then will upload the excel file for the system to Process the record to sql.
From my application logic . i am storing the uploaded file in SQL using file stream , then when processing start , i download the file stream convert it to CSV (Pipe Seprated) and then process the file , so far i have not encountered any issue with processing ,but one column in the excel is (Description)and users will copy paste the column , now the column has line breaks and paragraph breaks , so when i processing the file and reading the file Line by Line using Stream Reader Readline method , when it encounter the break and consider it as the end line
Any one pls suggest any solution for it below is the code for proper understanding
string fileinfo = ExcelConversion(FileName, FileID.Value);//Converion of Excel File to CSV byte[] output = UTF8Encoding.UTF8.GetBytes(fileinfo);//since the values will have chinese or japanese characters MemoryStream stream = new MemoryStream(output); using (StreamReader reader = new StreamReader(stream)) { char[] splitchar; splitchar = new char[] { ''|'' }; SrcUpperBoundColumnIndex =Src_Excel_Issedetails._021_TargetCompletionDate; DataTable IssueUploaded = CreateDataTable(reader.ReadLine()); string strIssueRecord; while ((strIssueRecord = reader.ReadLine()) != null) { string[] Columns = strIssueRecord.Split(splitchar); if (Columns.GetUpperBound(0) == (SrcUpperBoundColumnIndex.GetHashCode() + 1)) { AddRow(Columns); } else { throw new Exception("File upload failed. Invalid column count found."); } }
While processing - Stream Reader you can use Trim Function to remove such Line Breaks & Para''s
这篇关于处理CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!