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. General Programming
  3. C#
  4. Generating Excel reports by using .Net

Generating Excel reports by using .Net

Scheduled Pinned Locked Moved C#
questioncsharpsysadmin
11 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.
  • C Offline
    C Offline
    Charith Jayasundara
    wrote on last edited by
    #1

    I am currently working with a .Net web application. I have to create several complicated excel reports using that web application. I have two doubts First Question: Can I use all the excel functions with .Net ? Do you have any good samples ? Second Question: This is not a windows application, this is a web server. Can I generate excel report by using .Net web application? Thanks!:)

    jayasshc

    P U E 3 Replies Last reply
    0
    • C Charith Jayasundara

      I am currently working with a .Net web application. I have to create several complicated excel reports using that web application. I have two doubts First Question: Can I use all the excel functions with .Net ? Do you have any good samples ? Second Question: This is not a windows application, this is a web server. Can I generate excel report by using .Net web application? Thanks!:)

      jayasshc

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      csecharithjayasshc wrote:

      First Question: Can I use all the excel functions with .Net ? Do you have any good samples ?

      Do you need to include functions in your Excel spreadsheet? Or do you just need to output a set of literal values to it? If you just need to output data to your spreadsheet you should look at using the ODBC/Jet driver. Google will provide you with plenty of samples. If you need to create functions for particular cells, then you will need to use the Excel automation library. Again, Google should be your starting point.

      csecharithjayasshc wrote:

      Second Question: This is not a windows application, this is a web server. Can I generate excel report by using .Net web application?

      Yes you can, but of the two options I mentioned above I would only recommend using the ODBC/Jet solution from a web application. Excel has a single-threaded architecture, so the automation library is not suited for use within a server application. Regards Paul

      C 1 Reply Last reply
      0
      • P pmarfleet

        csecharithjayasshc wrote:

        First Question: Can I use all the excel functions with .Net ? Do you have any good samples ?

        Do you need to include functions in your Excel spreadsheet? Or do you just need to output a set of literal values to it? If you just need to output data to your spreadsheet you should look at using the ODBC/Jet driver. Google will provide you with plenty of samples. If you need to create functions for particular cells, then you will need to use the Excel automation library. Again, Google should be your starting point.

        csecharithjayasshc wrote:

        Second Question: This is not a windows application, this is a web server. Can I generate excel report by using .Net web application?

        Yes you can, but of the two options I mentioned above I would only recommend using the ODBC/Jet solution from a web application. Excel has a single-threaded architecture, so the automation library is not suited for use within a server application. Regards Paul

        C Offline
        C Offline
        Charith Jayasundara
        wrote on last edited by
        #3

        Dear Paul, Thank you very much for the reply. I just noticed another more important fact. I'll receive all the required data through a stored procedure. So I don't want to use excel functions, I can calculate everything in C# and post the calculated value on the cell. So "Excel automation library" would be suitable for that, isn't it? Thanks Again!

        P 1 Reply Last reply
        0
        • C Charith Jayasundara

          Dear Paul, Thank you very much for the reply. I just noticed another more important fact. I'll receive all the required data through a stored procedure. So I don't want to use excel functions, I can calculate everything in C# and post the calculated value on the cell. So "Excel automation library" would be suitable for that, isn't it? Thanks Again!

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #4

          Yes, but you're still better off using the ODBC/Jet driver rather than Excel automation for the reasons I mentioned in my previous post. This is what I recently used when creating a data export facility for an ASP.NET web app. Paul

          C 2 Replies Last reply
          0
          • P pmarfleet

            Yes, but you're still better off using the ODBC/Jet driver rather than Excel automation for the reasons I mentioned in my previous post. This is what I recently used when creating a data export facility for an ASP.NET web app. Paul

            C Offline
            C Offline
            Charith Jayasundara
            wrote on last edited by
            #5

            Hi, ok, is ODBC/Jet driver functionality depends on the excel version (2003 or 2007)? Thanks!

            P 1 Reply Last reply
            0
            • C Charith Jayasundara

              I am currently working with a .Net web application. I have to create several complicated excel reports using that web application. I have two doubts First Question: Can I use all the excel functions with .Net ? Do you have any good samples ? Second Question: This is not a windows application, this is a web server. Can I generate excel report by using .Net web application? Thanks!:)

              jayasshc

              U Offline
              U Offline
              Urs Enzler
              wrote on last edited by
              #6

              This article shows you another approach: http://www.bbv.ch/pdf_files/system_event/2005_XMLto%20Excel.pdf XSLT is used to create Excel files. The advantage is that you do not need Excel itself to create the files. The disadvantage is that you have a Excel-xml file in the end. If you need to have an xls then you still have to use Excel to transform the xml to a xls file. But this is mus easier then program all data into Excel with interop.

              -^-^-^-^-^- no risk no funk ................... please vote ------>

              C 1 Reply Last reply
              0
              • C Charith Jayasundara

                I am currently working with a .Net web application. I have to create several complicated excel reports using that web application. I have two doubts First Question: Can I use all the excel functions with .Net ? Do you have any good samples ? Second Question: This is not a windows application, this is a web server. Can I generate excel report by using .Net web application? Thanks!:)

                jayasshc

                E Offline
                E Offline
                eoe
                wrote on last edited by
                #7

                Hi, if you are looking for a solution for a large project, you should evaluate the libraries offered by aspose http://www.aspose.com[^]. I started a project using COM/INTEROP to create an interface to the office world. Then it was necessary to support the generation of word documents on a server without having any MS office application installed. We decided to use aspose. Finally we were able to improve our applications noticeably. Erik

                1 Reply Last reply
                0
                • U Urs Enzler

                  This article shows you another approach: http://www.bbv.ch/pdf_files/system_event/2005_XMLto%20Excel.pdf XSLT is used to create Excel files. The advantage is that you do not need Excel itself to create the files. The disadvantage is that you have a Excel-xml file in the end. If you need to have an xls then you still have to use Excel to transform the xml to a xls file. But this is mus easier then program all data into Excel with interop.

                  -^-^-^-^-^- no risk no funk ................... please vote ------>

                  C Offline
                  C Offline
                  Charith Jayasundara
                  wrote on last edited by
                  #8

                  Thanks!

                  jayasshc

                  1 Reply Last reply
                  0
                  • C Charith Jayasundara

                    Hi, ok, is ODBC/Jet driver functionality depends on the excel version (2003 or 2007)? Thanks!

                    P Offline
                    P Offline
                    pmarfleet
                    wrote on last edited by
                    #9

                    No. You don't need Excel installed on the server, only the driver. Paul

                    1 Reply Last reply
                    0
                    • P pmarfleet

                      Yes, but you're still better off using the ODBC/Jet driver rather than Excel automation for the reasons I mentioned in my previous post. This is what I recently used when creating a data export facility for an ASP.NET web app. Paul

                      C Offline
                      C Offline
                      Charith Jayasundara
                      wrote on last edited by
                      #10

                      Hi Paul, Could you please provide me a good C# example for odbc/jet driver thing? I tried on the internet but still I couldn't find a proper one. I could find several nice article for "Excel Automation Library". Thanks for your kind help! Good Luck!!!

                      jayasshc

                      P 1 Reply Last reply
                      0
                      • C Charith Jayasundara

                        Hi Paul, Could you please provide me a good C# example for odbc/jet driver thing? I tried on the internet but still I couldn't find a proper one. I could find several nice article for "Excel Automation Library". Thanks for your kind help! Good Luck!!!

                        jayasshc

                        P Offline
                        P Offline
                        pmarfleet
                        wrote on last edited by
                        #11

                        Try something like this:

                        //create random file so as not to conflict
                        string dataFile = "";

                        //make sure the temp file is not read only (copied from template) as this stops the OLE connection from updating
                        File.SetAttributes(dataFile, FileAttributes.Normal);

                        //inject data
                        System.Data.OleDb.OleDbConnection tempExcelConnection = new System.Data.OleDb.OleDbConnection();
                        tempExcelConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;";
                        tempExcelConnection.ConnectionString += "Data Source=" + dataFile + ";";
                        tempExcelConnection.ConnectionString += @"Extended Properties=""Excel 8.0;HDR=Yes;"";";
                        System.Data.OleDb.OleDbCommand tempExcelCommand = new System.Data.OleDb.OleDbCommand();
                        tempExcelCommand.Connection = tempExcelConnection;

                        System.Data.SqlClient.SqlDataReader dr = null;

                        tempExcelCommand.Connection.Open();

                        // TODO: Get data for SqlDataReader

                        if (dr.HasRows)
                        {
                        while(dr.Read())
                        {
                        //NB. Ths query expects a named range called "mynamedrange" containing 5 columns
                        //the data will be inserted after the named range so should only include the column headings
                        tempExcelCommand.CommandText = String.Format("INSERT INTO [mynamedrange] VALUES ({0}, {1}, {2}, {3}, {4})", dr[0], dr[1], dr[2], dr[3], dr[4]);
                        tempExcelCommand.ExecuteNonQuery();
                        }
                        }

                        dr.Close();
                        tempExcelCommand.Connection.Close();

                        Regards Paul

                        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