0

I was trying to export my C# list to Csv file. All is set well. But the thing is field seperator is not working properly. its showing like, my string with " at the end (eg: 0000324df"). Here is my Controller code.

IEnumerable stockexpo = stockexp; // Assign value
        MemoryStream output = new MemoryStream();
        StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
        writer.Write("ItemNo,");
        writer.Write("Repeat Count");
        writer.WriteLine();
        foreach (StockResult order in stockexpo)
        {
            writer.Write(String.Format("{0:d}", order.ItemNumber));
            writer.Write("\"");
            writer.Write(",");
            writer.Write("\"");
            writer.Write(order.Count);
            writer.Write("\"");
            writer.Write(",");
            writer.WriteLine();
        }
        writer.Flush();
        output.Position = 0;

        return File(output, "text/comma-separated-values", "stockexp.csv");

I need to know how i can seperate the field values appropriately. Anyone can help me for this.

4
  • Does some of your data contain speachmarks? Looking at your code it is doing exactly what you have asked it to do... Commented Aug 16, 2012 at 8:54
  • No speachmarks in my data. but its (") showing with every value. Commented Aug 16, 2012 at 9:00
  • writer.Write("\""); <- What do you expect this line to do? Commented Aug 16, 2012 at 9:01
  • Its like this:0000000027" 0000000028" 0000000029" 0000000030" 0000000031" 0000000032" 0000000033" 0000000034" 0000000035" 0000000036" Commented Aug 16, 2012 at 9:01

1 Answer 1

3
  writer.Write("\"");

This line of code will be outputting a " every time. Why have it at all?

Also, I wouldn't have a comma before the WriteLine, since there is no need to delimit the end of the file.

IEnumerable stockexpo = stockexp; // Assign value
    MemoryStream output = new MemoryStream();
    StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
    writer.Write("ItemNo,");
    writer.Write("Repeat Count");
    writer.WriteLine();
    foreach (StockResult order in stockexpo)
    {
        writer.Write(order.ItemNumber);
        writer.Write(",");
        writer.Write(order.Count);
        writer.WriteLine();
    }
    writer.Flush();
    output.Position = 0;

    return File(output, "text/comma-separated-values", "stockexp.csv");
Sign up to request clarification or add additional context in comments.

9 Comments

This time i am getting the last two chars in the string. like, the actual value => 0000000043. But i am getting only 43.
What is the type of order.ItemNumber?
Then why use String format like you were. See my edited answer.
In that case, your order.ItemNumber is not a string type.
Its a String and Unique Type.
|

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.