Exemplo n.º 1
0
    protected void ExportToExcel(object sender, EventArgs e)
    {
        try
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Testcases.csv");
            Response.Charset     = "";
            Response.ContentType = "text/csv";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                Samples.AspNet.ObjectDataSource.TestcaseProfileData tcpd = new Samples.AspNet.ObjectDataSource.TestcaseProfileData();

                var       lines      = new List <string>();
                int       fetchCount = int.Parse(totalLbl.Text);
                DataTable dt         = (fetchCount > 0) ? tcpd.QueryTestcases(txtFilter.Text, null, 0, fetchCount) : null;
                if (null != dt)
                {
                    string[] columnNames = dt.Columns.Cast <DataColumn>().Select(column => column.ColumnName).ToArray();
                    var      header      = string.Join(",", columnNames);
                    lines.Add(header);

                    var valueLines = tcpd.QueryTestcases(
                        txtFilter.Text, null, 0, fetchCount).AsEnumerable().Select(row => string.Join(",", row.ItemArray));
                    lines.AddRange(valueLines);

                    foreach (string s in lines)
                    {
                        sw.WriteLine(s);
                    }
                }
                else
                {
                    sw.WriteLine("No results!");
                }

                //style to format numbers to string
                //string style = @"<style> .textmode { } </style>";
                //Response.Write(style);
                //Response.Output.Write(sw.ToString());

                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sw.ToString());
                Response.OutputStream.Write(bytes, 0, bytes.Length);

                Response.Flush();
                Response.End();
            }
        }
        catch (Exception ex)
        {
            querySQL.Text = totalLbl.Text + " = " + TestcaseProfileData.querySQL;
            throw new Exception(querySQL.Text, ex);
        }
    }
Exemplo n.º 2
0
    protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        Samples.AspNet.ObjectDataSource.TestcaseProfileData tcpd = new Samples.AspNet.ObjectDataSource.TestcaseProfileData();
        string path  = @"D:\Turtle-export.csv";
        var    lines = new List <string>();

        string[] columnNames = tcpd.QueryTestcases(txtFilter.Text, null, 0, 1000).Columns.Cast <DataColumn>().Select(column => column.ColumnName).ToArray();

        var header = string.Join(",", columnNames);

        lines.Add(header);

        var valueLines = tcpd.QueryTestcases(txtFilter.Text, null, 0, 1000).AsEnumerable().Select(row => string.Join(",", row.ItemArray));

        lines.AddRange(valueLines);

        File.WriteAllLines(path, lines);
    }