Question

I have a C#/ASPX Form that have a table with 8 columns (StoreID, Date, EmpID, EmployeeName, Starttime, Endtime, Starttime, Endtime) in SQL Sever

I want only to convert the Storeid, Date, EmpID, the first Starttime and last Endtime)

but when i click the button it convert all of them, how can i limit the column during conversion

this is the export button code:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnExportCSV_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";

        GridView1.AllowPaging = false;
        GridView1.DataBind();

        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < GridView1.Columns.Count; k++)
        {
            //add separator
            sb.Append(GridView1.Columns[k].HeaderText + ',');
        }
        //append new line
        sb.Append("\r\n");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            for (int k = 0; k < GridView1.Columns.Count; k++)
            {
                //add separator
                sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
            }
            //append new line
            sb.Append("\r\n");
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();
    }
}
Was it helpful?

Solution

If you know the columns' index which you want to export such as the following:

StoreID | Date | EmpID | EmployeeName | Starttime | Endtime | Starttime | Endtime

You have to get values from columns at index = 0, 1, 2, 4, 7

Then your code could be simpler like this

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnExportCSV_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";

        GridView1.AllowPaging = false;
        GridView1.DataBind();

        StringBuilder sb = new StringBuilder();
        //add separator for header
        sb.Append(GridView1.Columns[0].HeaderText).Append(",")
            .Append(GridView1.Columns[1].HeaderText).Append(",")
            .Append(GridView1.Columns[2].HeaderText).Append(",")
            .Append(GridView1.Columns[4].HeaderText).Append(",")
            .Append(GridView1.Columns[7].HeaderText).Append("\r\n");

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            DateTime date = DateTime.Parse(GridView1.Rows[i].Cells[1].Text);

            if (date.DayOfWeek != DayOfWeek.Saturday 
                 && date.DayOfWeek != DayOfWeek.Sunday) 
            {
               //add separator for each row
               sb.Append(GridView1.Rows[i].Cells[0].Text).Append(",")
               .Append(GridView1.Rows[i].Cells[1].Text).Append(",")
               .Append(GridView1.Rows[i].Cells[2].Text).Append(",")
               .Append(GridView1.Rows[i].Cells[4].Text).Append(",")
               .Append(GridView1.Rows[i].Cells[7].Text).Append("\r\n");
            }
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();
    }
}

OTHER TIPS

I haven't compiled this code, but it should work.

protected void btnExportCSV_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
    Response.Charset = "";
    Response.ContentType = "application/text";

    GridView1.AllowPaging = false;
    GridView1.DataBind();

    StringBuilder sb = new StringBuilder();
    bool hitFirstStartTime = false;
    for (int k = 0; k < GridView1.Columns.Count; k++)
    {
        var letsDoThis = false;

        if (GridView1.Columns[k].HeaderText == "Storeid" ||
            GridView1.Columns[k].HeaderText == "Date" ||
            GridView1.Columns[k].HeaderText == "EmpID")
        {
            letsDoThis = true;
        }
        else if (GridView1.Columns[k].HeaderText == "Starttime" && !hitFirstStartTime)
        {
            letsDoThis = true;
            hitFirstStartTime = true;
        }
        else if (GridView1.Columns[k].HeaderText == "Endtime" &&  k == GridView1.Columns.Count)
        {
            letsDoThis = true;
        }

        if (letsDoThis)
            sb.Append(GridView1.Columns[k].HeaderText + ',');
    }
    //append new line
    sb.Append("\r\n");
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        bool hitFirstStartTime = false;
        for (int k = 0; k < GridView1.Columns.Count; k++)
        {
            var letsDoThis = false;

            if (GridView1.Columns[k].HeaderText == "Storeid" ||
                GridView1.Columns[k].HeaderText == "Date" ||
                GridView1.Columns[k].HeaderText == "EmpID")
                letsDoThis = true;
            else if (GridView1.Columns[k].HeaderText == "Starttime" && 
                     !hitFirstStartTime)
                letsDoThis = true;
            else if (GridView1.Columns[k].HeaderText == "Endtime" && 
                     k == GridView1.Columns.Count)
                letsDoThis = true;
            {
                letsDoThis = true;
                hitFirstStartTime = true;
            }

            if (letsDoThis)
                sb.Append(GridView1.Columns[k].HeaderText + ',');
        }
        //append new line
        sb.Append("\r\n");
    }
    Response.Output.Write(sb.ToString());
    Response.Flush();
    Response.End();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top