C # Export data to CSV

Source: Internet
Author: User

Sometimes when you save Excel data to a CSV file, a CSV format error occurs, and the following example implements the data stored in the DataTable directly to a CSV file.

system.web.httpruntime.cache["v_ds" "";

This v_ds is a cache, because sometimes because the page refresh will empty the memory of the data, here with the cache, of course, there are many other ways.

DataTable dt = (DataTable) system.web.httpruntime.cache["V_ds"];            Response.clearcontent (); Response.AddHeader ("content-disposition","attachment; Filename="+ DateTime.Now.ToString ("YYYY-MM-DD") +". csv"); Response.ContentEncoding= System.Text.Encoding.GetEncoding ("gb2312"); Response.ContentType="Application/excel"; StringBuilder SB=NewStringBuilder (); Sb. Append ("ticket, pallet number, box number, SN,IMEID1,IMEID2, customer po\r\n");  for(inti =0; i < dt. Rows.Count; i++) {sb. Append ("\""+ dt. rows[i]["Ticket number"] +"\""+","); Sb. Append ("\""+ dt. rows[i]["Stack Plate number"] +"\""+","); Sb. Append ("\""+ dt. rows[i]["Medium Container No."] +"\""+","); Sb. Append ("\""+ dt. rows[i]["SN"] +"\""+","); Sb. Append ("\""+ dt. rows[i]["IMEID1"] +"\""+","); Sb. Append ("\""+ dt. rows[i]["IMEID2"] +"\""+","); Sb. Append ("\""+ dt. rows[i]["Customer PO"] +"\""+"\ r \ n"); } Response.Write (sb.)            ToString ()); Response.End ();

C # Export data to CSV

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.