How to create a CSV file in asp.net?

How to create .CSV file from read database in asp.net? Can someone suggest me how to create a .CSV file in asp.net?

Thanks at Advance.

+4
source share
6 answers

Note this: Export DataTable to CSV file format

public void CreateCSVFile(DataTable dt, string strFilePath) { #region Export Grid to CSV // Create the CSV file to which grid data will be exported. StreamWriter sw = new StreamWriter(strFilePath, false); // First we will write the headers. //DataTable dt = m_dsProducts.Tables[0]; int iColCount = dt.Columns.Count; for (int i = 0; i < iColCount; i++) { sw.Write(dt.Columns[i]); if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); // Now write all the rows. foreach (DataRow dr in dt.Rows) { for (int i = 0; i < iColCount; i++) { if (!Convert.IsDBNull(dr[i])) { sw.Write(dr[i].ToString()); } if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); } sw.Close(); #endregion } 

If you are trying to force the download to always indicate Save As, you must set the content type to application / octet-stream.

A source:

  string attachment = "attachment; filename=MyCsvLol.csv"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "text/csv"; HttpContext.Current.Response.AddHeader("Pragma", "public"); var sb = new StringBuilder(); foreach(var line in DataToExportToCSV) sb.AppendLine(TransformDataLineIntoCsv(line)); HttpContext.Current.Response.Write(sb.ToString()); HttpContext.Current.Response.End(); 

One of the easiest uses FileHelpers Library

 FileHelpers.CsvEngine.DataTableToCsv(dataTable, filename); 

WriteFile (inherited from FileHelperEngine) Overloaded. Create an array of records in the specified file.

WriteStream (inherited from FileHelperEngine) Overloaded. Write an array of records to the specified stream.

WriteString (inherited from FileHelperEngine) Overloaded. Write an array of records to a string and return it.

Reference:
Write to a CSV file and export it?
Export data to a comma-delimited file (CSV) for an application such as Excel
export datatable to CSV using Save File dialog box - C #

+5
source

You can see how to create a CSV file in the article Reading and Writing CSV Files in C # .

You can see how to dynamically generate the contents of a file, but behave like a downloadable file in the article Create dynamically downloadable content .

0
source

Why can't you just:

1) Request a database (for example, using an SQL reader),

2) set the HTTP response header (response.ContentType) to the MIME type "application / csv"

3) write out (response.Write) each line of your result set in comma-delimited format (CSV)?

Here is an example of this approach:

0
source

Try the following:

 string filePath = @"C:\test.csv"; string delimiter = ","; string[][] output = new string[][]{ new string[]{"Col 1 Row 1", "Col 2 Row 1", "Col 3 Row 1"}, new string[]{"Col1 Row 2", "Col2 Row 2", "Col3 Row 2"} }; int length = output.GetLength(0); StringBuilder sb = new StringBuilder(); for (int index = 0; index < length; index++) sb.AppendLine(string.Join(delimiter, output[index])); File.WriteAllText(filePath, sb.ToString()); 

for more help .

0
source

In any case, you should find any reliable CSV library for this, if you allow special characters in your file, such as double quote, comma, space, etc.

0
source

Think you're looking for a CSV file format. This is a really plain text file with a very simple structure . Here you can link to the Wiki .

You can do it yourself by getting data from your database and writing data to a text file according to your needs.

Here is an example of the contents of a CSV file:

 Year,Make,Model 1997,Ford,E350 2000,Mercury,Cougar 

The file has 3 columns: Year , Make , Model and 2 rows of data: 1997,Ford,E350 , 2000,Mercury,Cougar .

0
source

Source: https://habr.com/ru/post/1446376/


All Articles