Export CSV File From Gridview in ASP.NET using C#

View: 259    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: C / C++ / MFC   Fields: Other

0 point/0 review File has been tested

These are different multiple values with comma separation (e.g php, javascript, asp, facebook etc) in a file

Introduction

What is a CSV file ?

It stands for comma separated values

These are different multiple values with comma separation (e.g php, javascript, asp, facebook etc) in a file

This file’s extension is .csv and it contains comma separated values or data inside it

What is a Grid view in asp.net ?

It is a table which contains data or information in a tabular form

It contains static as well as dynamic data (from database)

It has paging, sorting and more basic functions etc

In asp.net, it is a server side GridView control which is a part of .net framework

In the following, we discuss how to export csv file from Gridview in ASP.NET using C#.NET

Code Snippet.

Response.ClearContent();
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "NewsLetterSubscribers.csv")); // AddHeader method pass 3 parameters, first string, second attachement string, and 3rd one the .csv file to be exported. AddHeader() method sends an attachment to the client or browser directly here that attachment is .CSV file
 
                Response.ContentType = "application/text"; // this line tells the browser that the contents of this file is a simple text
 
                // gvdetails is name of Girdview
                gvdetails.AllowPaging = false; // paging is false
 
                gvdetails.DataBind();
 
                var strbldr = new StringBuilder(); // crate an object of stringBuilder() class
 
                for (var i = 0; i < gvdetails.Columns.Count; i++) // loop through each data items in grid view control
                {
                    //separting header columns text with comma operator
                    strbldr.Append(gvdetails.Columns[i].HeaderText + ',');
                }
 
                //appending new line for gridview header row
                strbldr.Append("\n");
 
                for (var j = 0; j < gvdetails.Rows.Count; j++)
                {
                    for (var k = 0; k < gvdetails.Columns.Count; k++)
                    {
                        //separating gridview columns with comma
                        strbldr.Append(gvdetails.Rows[j].Cells[k].Text + ',');
                    }
                    //appending new line for gridview rows
                    strbldr.Append("\n");
                }
 
                Response.Write(strbldr.ToString());
 
                Response.End();

Export CSV File From Gridview in ASP.NET using C#

Export CSV File From Gridview in ASP.NET using C# Posted on 22-08-2016  These are different multiple values with comma separation (e.g php, javascript, asp, facebook etc) in a file 5/10 259

Comment:

To comment you must be logged in members.

Files with category

  • How to Swap Two Numbers using Call by Reference in C

    View: 510    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    View: 512    Download: 0   Comment: 0   Author: none  

    How to Print Fibonacci Series using Recursion in C, C++

    Category: C / C++ / MFC
    Fields: Other

    2.25/2 review
    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    View: 568    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    1.3333333333333/3 review
    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    View: 405    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    View: 550    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

    Category: C / C++ / MFC
    Fields: Other

    0/4 review
    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    View: 371    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    View: 322    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

  • Build C# Multilevel Inheritance

    View: 351    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

    Category: C / C++ / MFC
    Fields: Other

    2.5/2 review
    C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd