0
protected void Button2_Click1(object sender, EventArgs e)
        {
        StreamWriter myOutputStream = new StreamWriter("Myfile.csv");

        foreach (var item in urlLst.Items)
        {
            myOutputStream.WriteLine(item.ToString());
        }

        myOutputStream.Close();
    }

I am using this code but not getting output.

2
  • Have you tried specifying an absolute path? Also please provide more info on "not getting output" - any exception thrown? Commented Dec 13, 2017 at 8:47
  • Check this answer: stackoverflow.com/a/799454/1970317 Commented Dec 13, 2017 at 8:47

1 Answer 1

1
Hi You can try the below code sample.

            int items = 10; //ListBoxItems.Items.Count;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < items; i++)
            {

                sb.AppendLine(i.ToString()); //Loop through and get list box item values
            }

            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=ListBox_Contents.csv");
            Response.Charset = "";
            Response.ContentType = "application/vnd.csv";
            StringWriter stringWrite = new StringWriter(sb);
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            Response.Write(stringWrite.ToString());
            Response.End(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please tell me in which code you are getting empty CSV generated?
Also, from your code you have to provide a directory path here. StreamWriter myOutputStream = new StreamWriter("Directory Path");
Thanks i have tried your code, it is proper. Now i am not getting empty csv.

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.