Export to Excel with number of columns unknown in advance
-
I have a web application to display sales data. My input, transaction_header table, is like this:
...
van_id
sale_date
...
...
Van A
2012-03-01 17:28:38.000
...
...
Van A
2012-03-01 17:33:54.000
...
...
Van B
2012-03-01 12:33:48.000
...
...
Van B
2012-03-01 12:38:14.000
...
I need to export the transaction data to excel and the excel would show as:
Gross Sales
DATE
Van A
Van B
Van C
...
1-Mar
$3,359
...
$49,934
$20,657
$41,481
...
2-Mar
$3,633
$4,263
...
$73,394
$38,134
$41,083
...
3-Mar
$2,118
...
$40,831
$39,838
$32,344
...
4-Mar
SUNDAY
5-Mar
$4,387
$3,842
...
$84,814
$39,242
$41,116
...
...
My problem is the number of van may change over time. For the transaction_header table, this does not post a problem as there is just one more row. Yet for the output, it is a problem. Thw columns of my table may increase over time. Now how do I approach this problem? Appreciate any feedback. Thanks.
-
I have a web application to display sales data. My input, transaction_header table, is like this:
...
van_id
sale_date
...
...
Van A
2012-03-01 17:28:38.000
...
...
Van A
2012-03-01 17:33:54.000
...
...
Van B
2012-03-01 12:33:48.000
...
...
Van B
2012-03-01 12:38:14.000
...
I need to export the transaction data to excel and the excel would show as:
Gross Sales
DATE
Van A
Van B
Van C
...
1-Mar
$3,359
...
$49,934
$20,657
$41,481
...
2-Mar
$3,633
$4,263
...
$73,394
$38,134
$41,083
...
3-Mar
$2,118
...
$40,831
$39,838
$32,344
...
4-Mar
SUNDAY
5-Mar
$4,387
$3,842
...
$84,814
$39,242
$41,116
...
...
My problem is the number of van may change over time. For the transaction_header table, this does not post a problem as there is just one more row. Yet for the output, it is a problem. Thw columns of my table may increase over time. Now how do I approach this problem? Appreciate any feedback. Thanks.
-
I have a web application to display sales data. My input, transaction_header table, is like this:
...
van_id
sale_date
...
...
Van A
2012-03-01 17:28:38.000
...
...
Van A
2012-03-01 17:33:54.000
...
...
Van B
2012-03-01 12:33:48.000
...
...
Van B
2012-03-01 12:38:14.000
...
I need to export the transaction data to excel and the excel would show as:
Gross Sales
DATE
Van A
Van B
Van C
...
1-Mar
$3,359
...
$49,934
$20,657
$41,481
...
2-Mar
$3,633
$4,263
...
$73,394
$38,134
$41,083
...
3-Mar
$2,118
...
$40,831
$39,838
$32,344
...
4-Mar
SUNDAY
5-Mar
$4,387
$3,842
...
$84,814
$39,242
$41,116
...
...
My problem is the number of van may change over time. For the transaction_header table, this does not post a problem as there is just one more row. Yet for the output, it is a problem. Thw columns of my table may increase over time. Now how do I approach this problem? Appreciate any feedback. Thanks.
-
Yes, there are tons of example out there. :-D My question, I think, is more specific. The number of columns may increase (or even decrease, although I doubt it will) over time. Now how can I handle this?
-
Yes, there are tons of example out there. :-D My question, I think, is more specific. The number of columns may increase (or even decrease, although I doubt it will) over time. Now how can I handle this?
Quote:
The number of columns may increase (or even decrease, although I doubt it will)
That is just fine. Most examples I have seen use a DataGrid and set AutoGenerateColumns = true. This means that it does not matter how many columns you have. The code is have used before is here.[^] All you have to do is set the datasource of the datagrid.
There are only 10 types of people in the world, those who understand binary and those who don't.
-
I have a web application to display sales data. My input, transaction_header table, is like this:
...
van_id
sale_date
...
...
Van A
2012-03-01 17:28:38.000
...
...
Van A
2012-03-01 17:33:54.000
...
...
Van B
2012-03-01 12:33:48.000
...
...
Van B
2012-03-01 12:38:14.000
...
I need to export the transaction data to excel and the excel would show as:
Gross Sales
DATE
Van A
Van B
Van C
...
1-Mar
$3,359
...
$49,934
$20,657
$41,481
...
2-Mar
$3,633
$4,263
...
$73,394
$38,134
$41,083
...
3-Mar
$2,118
...
$40,831
$39,838
$32,344
...
4-Mar
SUNDAY
5-Mar
$4,387
$3,842
...
$84,814
$39,242
$41,116
...
...
My problem is the number of van may change over time. For the transaction_header table, this does not post a problem as there is just one more row. Yet for the output, it is a problem. Thw columns of my table may increase over time. Now how do I approach this problem? Appreciate any feedback. Thanks.
Try This...it will work as per your requirement: protected void cmdExport_Click(object sender, EventArgs e) { clsDataAcess oDataAccess = new clsDataAccess(); oDT = new DataTable(); oConst = new clsConstMaster(); string filename = string.Empty; try { oDT = oDataAccess.GetCustomerWholeInformation(Parameter To function Here); if (oDT.Rows.Count > 0) { filename = "CustomerInformationFromMaster.xls"; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); DataGrid dg = new DataGrid(); dg.DataSource = oDT; dg.DataBind(); dg.HeaderStyle.BackColor = System.Drawing.Color.LightGray; dg.HeaderStyle.Font.Bold = true; foreach (DataGridItem i in dg.Items) { foreach (TableCell tc in i.Cells) { tc.Attributes.Add("class", "text"); //tc.BackColor = System.Drawing.Color.Blue; } } dg.RenderControl(hw); string style = @" .text { mso-number-format:\@; } "; Response.Write(style); Response.ContentType = "application/vnd.ms-excel"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ""); this.EnableViewState = false; Response.Write(tw.ToString()); Response.End(); } } catch (Exception ex) { MessageBox(ex.Message.ToString()); } } public override void VerifyRenderingInServerForm(Control control) { }
- Happy Coding - Vishal Vashishta