将数据转换为CSV

时间:2013-12-03 14:22:43

标签: c# asp.net

我有一个C#/ ASPX表单,在SQL Sever中有一个包含8列(StoreID,Date,EmpID,EmployeeName,Starttime,Endtime,Starttime,Endtime)的表

我只想转换Storeid,Date,EmpID,第一个Starttime和Last Endtime)

但是当我点击按钮时它会转换所有这些按钮,如何在转换期间限制列

这是导出按钮代码:

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();
    }
}

2 个答案:

答案 0 :(得分:1)

如果您知道要导出的列索引,请执行以下操作:

StoreID | 日期 | EmpID | EmployeeName | 开始时间 |结束时间|开始时间|的结束时间

您必须从index = 0,1,2,4,7

的列中获取值

然后你的代码就像这样简单

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();
    }
}

答案 1 :(得分:0)

我还没有编译这段代码,但它应该有用。

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();
}