2

I have an excel .csv file that has some data in the below formats in cells:

NAME Address Contact
Shoeb   Lko 675567
Rajesh  Banaras 7687678

.csv file is not a text file having only .csv extension. It is a csv file that is made using Microsoft Excel file...For testing you can also make .csv file... For this (1) create an excel file (2) open this excel file (3) go to file menu and click on "save as" (4) select CSV(Comma Seperated) option in Save as type: ---- Now this is .csv file from which I will read content and write content also in a .csv file

I am trying to use C# to write another Excel .csv file in the same format.

Code that I am using is written below:

//Below line is reading file from system drive
    StreamReader rd = new StreamReader("D:\FilesUploadedToTablet\drivers.csv", true);
//Below line is writing data to file existing in our site folder
    StreamWriter wr = new StreamWriter(Server.MapPath(".") + "\filename\CSVFile.csv");
    wr.Write(rd.ReadToEnd());
    rd.Close();
    wr.Close();

Here StreamReader is reading drivers.csv file but StreamWriter is not writing that content to the CSVFile.csv file. If I use any text file in place of .csv file then the content writes successfully. What am I doing wrong?

15
  • I think the issue isn't related to CSV specifically. Something like writing permissions or file locks may be the subject. What is the output of Server.MapPath(".")? Commented Dec 25, 2012 at 13:09
  • "but something wrong" is not at all helpful. What specifically went wrong? What was the error message that you got? Commented Dec 25, 2012 at 13:10
  • Actually StreamWriter writes content provided by StreamReader in .txt file(destination file in which i will write) very well but I want to use .csv file as destination file (destination file in which i will write) then It doesn't works and also it doesn't produce any error. Given Code is ok if i am using .txt file as a destination file in which i have to write in place of .csv file. File path are also correct. Commented Dec 25, 2012 at 14:10
  • .csv file is not a text file having only .csv extension. It is a csv file that is made using Microsoft Excel file...For testing you can also make .csv file... For this (1) create an excel file (2) open this excel file (3) go to file menu and click on "save as" (4) select CSV(Comma Seperated) option in Save as type: ---- Now this is .csv file from which I will read content and write content also in a .csv file Commented Dec 25, 2012 at 14:14
  • 1
    FYI: CSV is a text file format. If it is not a text file, then it is not a CSV file. It is completely irrelevant what it was created with. Commented Dec 25, 2012 at 14:28

2 Answers 2

2

You could try using a BinaryReader() instead of the StreamReader(). Also try opening the StreamReader as follows:

   StreamReader("D:\FilesUploadedToTablet\drivers.csv", false);

It looks like you are only copying the file, not filtering or performing any processing on it. Why not just use File.Copy:

  File.Copy("D:\FilesUploadedToTablet\drivers.csv", Server.MapPath(".") + "\filename\CSVFile.csv");
Sign up to request clarification or add additional context in comments.

9 Comments

.csv file is not a text file having only .csv extension and its content separated by comma. It is a csv file that is made using Microsoft Excel file.that cells contains data. For testing you can also make .csv file... For this (1) create an excel file (2) open this excel file (3) go to file menu and click on "save as" (4) select CSV(Comma Seperated) option in Save as type: ---- Now this is .csv file from which I will read content and write content also in a .csv file.
When StreamReader reads data from .csv file then it automatically appends comma (,) after each cell content of a row and appends \r\n after each row. above given data in question is returned by StreamReader as "NAME,Address,Contact\r\nShoeb,Lko,675567r\nRajesh,Banaras,7687678" StreamWriter is unable tu understand that comma tells next cell and \r\n tells new row in .csv file. So it doesn't writes in .csv file it only writes in .txt file
Can you cut-copy-paste the first few lines from "D:\FilesUploadedToTablet\drivers.csv" here?
28 Robert London s-02 80 James Banaras s-01 17 Milton America s-01 62 Raju Japan s-01 72 Kapoor Bihar G-2006-01
There are no comma's in this file; StreamReader cannot add comma's by itself.
|
0

Thanx all of you for suggesting me...
I got it's solution – Solution that I got is written below :

string csvContent = string.Empty; 
if (File.Exists("SourceFilePath")) 
{ 
    StreamReader rd = new StreamReader("SourceFilePath", true); 
    csvContent = rd.ReadToEnd(); 
    rd.Close(); 
    var DestPath = HttpContext.Current.Server.MapPath("~/CSV Files/")+"MyFile.csv";     
    StreamWriter wr = new StreamWriter(DestPath, true); 
    StringBuilder sb = new StringBuilder(csvContent); 
    wr.WriteLine(sb.ToString()); 
    sb.Clear(); 
    wr.Close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.