Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Export to Excel with number of columns unknown in advance

Export to Excel with number of columns unknown in advance

Scheduled Pinned Locked Moved ASP.NET
questionsalesbeta-testinghelpcode-review
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    biop codeproject
    wrote on last edited by
    #1

    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.

    H Z V 3 Replies Last reply
    0
    • B biop codeproject

      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.

      H Offline
      H Offline
      Hermaine
      wrote on last edited by
      #2

      hmmm..... you can do it.... good luck! ;)

      1 Reply Last reply
      0
      • B biop codeproject

        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.

        Z Offline
        Z Offline
        ZurdoDev
        wrote on last edited by
        #3

        There are a ton of examples online of how to export data to Excel. Start here.[^]

        There are only 10 types of people in the world, those who understand binary and those who don't.

        B 1 Reply Last reply
        0
        • Z ZurdoDev

          There are a ton of examples online of how to export data to Excel. Start here.[^]

          There are only 10 types of people in the world, those who understand binary and those who don't.

          B Offline
          B Offline
          biop codeproject
          wrote on last edited by
          #4

          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?

          Z 1 Reply Last reply
          0
          • B biop codeproject

            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?

            Z Offline
            Z Offline
            ZurdoDev
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • B biop codeproject

              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.

              V Offline
              V Offline
              vvashishta
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups