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. Visual Basic
  4. Read data from MS Excel

Read data from MS Excel

Scheduled Pinned Locked Moved Visual Basic
csharpdatabaseannouncement
16 Posts 3 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.
  • N Offline
    N Offline
    nishkarsh_k
    wrote on last edited by
    #1

    Hello every one. My objective is to read data from .xls and process in VB.net application. I do have a desktop application which even reads data from a remote DB. I have the following clarification and issues - The application will be installed on different systems and we cannot make it sure that all the systems should have MS Office installed and that too a particular version - Can’t we somehow access data from and to excel files without using Ms Excel components, so that the application should also work on a system where MS Excel is not installed also :doh: Regards Nishkarsh

    D W 2 Replies Last reply
    0
    • N nishkarsh_k

      Hello every one. My objective is to read data from .xls and process in VB.net application. I do have a desktop application which even reads data from a remote DB. I have the following clarification and issues - The application will be installed on different systems and we cannot make it sure that all the systems should have MS Office installed and that too a particular version - Can’t we somehow access data from and to excel files without using Ms Excel components, so that the application should also work on a system where MS Excel is not installed also :doh: Regards Nishkarsh

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You can read/write Excel sheets using OleDb, an example of which you can find here[^]. I'm not quite sure, but I don't think you need Excel installed at all in order to use this method. ANy other method requires Excel to be installed.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008
      But no longer in 2009...

      N 1 Reply Last reply
      0
      • N nishkarsh_k

        Hello every one. My objective is to read data from .xls and process in VB.net application. I do have a desktop application which even reads data from a remote DB. I have the following clarification and issues - The application will be installed on different systems and we cannot make it sure that all the systems should have MS Office installed and that too a particular version - Can’t we somehow access data from and to excel files without using Ms Excel components, so that the application should also work on a system where MS Excel is not installed also :doh: Regards Nishkarsh

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        Hi Nishkarsh I have used a free library from CarlosAg for a while and it is really brilliant, as it seems to cover all bases. Here is the link CarlosAg[^]. He also has other links to other free Excel libraries out there. Definitely worth a visit. Happy Coding

        N 3 Replies Last reply
        0
        • W Wayne Gaylard

          Hi Nishkarsh I have used a free library from CarlosAg for a while and it is really brilliant, as it seems to cover all bases. Here is the link CarlosAg[^]. He also has other links to other free Excel libraries out there. Definitely worth a visit. Happy Coding

          N Offline
          N Offline
          nishkarsh_k
          wrote on last edited by
          #4

          Thanks a lot for the response. I will try it today

          1 Reply Last reply
          0
          • W Wayne Gaylard

            Hi Nishkarsh I have used a free library from CarlosAg for a while and it is really brilliant, as it seems to cover all bases. Here is the link CarlosAg[^]. He also has other links to other free Excel libraries out there. Definitely worth a visit. Happy Coding

            N Offline
            N Offline
            nishkarsh_k
            wrote on last edited by
            #5

            Hi Zimvbcoder I am trying to read the help manual which I downloaded along with the DLL. Can you please send me any example code (Vb.net if possible). Regards Nishkarsh

            modified on Monday, May 3, 2010 6:57 AM

            W 1 Reply Last reply
            0
            • N nishkarsh_k

              Hi Zimvbcoder I am trying to read the help manual which I downloaded along with the DLL. Can you please send me any example code (Vb.net if possible). Regards Nishkarsh

              modified on Monday, May 3, 2010 6:57 AM

              W Offline
              W Offline
              Wayne Gaylard
              wrote on last edited by
              #6

              Hi Nishkarsh, Carlos has a code generator on his site which will generate the code for any excel spreadsheet. This can be found here CarlosAg Code Generator[^]. If you design a sample spreadsheet, then you can use the code generator to generate the code for any .Net language. Most often you can use the code straight off, just ensuring the correct values go in the right places, otherwise a bit of refactoring is all that is needed. Happy Coding

              N 2 Replies Last reply
              0
              • W Wayne Gaylard

                Hi Nishkarsh, Carlos has a code generator on his site which will generate the code for any excel spreadsheet. This can be found here CarlosAg Code Generator[^]. If you design a sample spreadsheet, then you can use the code generator to generate the code for any .Net language. Most often you can use the code straight off, just ensuring the correct values go in the right places, otherwise a bit of refactoring is all that is needed. Happy Coding

                N Offline
                N Offline
                nishkarsh_k
                wrote on last edited by
                #7

                Hi Zimvbcoder, It is great. Works really good. One of the easiest thing to output a Excel sheet. The only other thing which i want to know is how to read from a Excel sheet. Regards Nishkarsh

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  You can read/write Excel sheets using OleDb, an example of which you can find here[^]. I'm not quite sure, but I don't think you need Excel installed at all in order to use this method. ANy other method requires Excel to be installed.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008
                  But no longer in 2009...

                  N Offline
                  N Offline
                  nishkarsh_k
                  wrote on last edited by
                  #8

                  Hi Dave, Thanks for the input. I will be trying it now to read from Excel sheet and write to it. Can you please provide me with some samples in VB.net. Regards Nishkarsh

                  modified on Monday, May 3, 2010 8:40 AM

                  D 1 Reply Last reply
                  0
                  • N nishkarsh_k

                    Hi Dave, Thanks for the input. I will be trying it now to read from Excel sheet and write to it. Can you please provide me with some samples in VB.net. Regards Nishkarsh

                    modified on Monday, May 3, 2010 8:40 AM

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    I already gave you one, you can find more with a little Google for "vb.net oledb excel[^]".

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008
                    But no longer in 2009...

                    1 Reply Last reply
                    0
                    • W Wayne Gaylard

                      Hi Nishkarsh, Carlos has a code generator on his site which will generate the code for any excel spreadsheet. This can be found here CarlosAg Code Generator[^]. If you design a sample spreadsheet, then you can use the code generator to generate the code for any .Net language. Most often you can use the code straight off, just ensuring the correct values go in the right places, otherwise a bit of refactoring is all that is needed. Happy Coding

                      N Offline
                      N Offline
                      nishkarsh_k
                      wrote on last edited by
                      #10

                      Hi Zimvbcoder, The lib is really great for making good looking XLS file output. I am having some issues and clarifications, can you please help me out. 1. Dose the lib provides any method to read from the excel sheet? 2. I read data from DB and used the codes given to create xls, is looks fine but when i went to save as option i realized it that the format is XML. Am i doing some thing wrong or it gives output in XML format only. Coz when i try to read the data from the generated file (in VB.net using OLEDB) i get file format error. And i Have to manually go and re-save the file in XLS format. Any idea? Regards Nishkarsh:confused: :doh:

                      W 1 Reply Last reply
                      0
                      • N nishkarsh_k

                        Hi Zimvbcoder, The lib is really great for making good looking XLS file output. I am having some issues and clarifications, can you please help me out. 1. Dose the lib provides any method to read from the excel sheet? 2. I read data from DB and used the codes given to create xls, is looks fine but when i went to save as option i realized it that the format is XML. Am i doing some thing wrong or it gives output in XML format only. Coz when i try to read the data from the generated file (in VB.net using OLEDB) i get file format error. And i Have to manually go and re-save the file in XLS format. Any idea? Regards Nishkarsh:confused: :doh:

                        W Offline
                        W Offline
                        Wayne Gaylard
                        wrote on last edited by
                        #11

                        Hi Nishkarsh I am afraid the library is only for generating reports, and cannot read Excel spreadsheets. The only way I know of reading Excel spreadsheets without Excel installed on the machine is by using ADO.Net and connecting to the spreadsheet like a database. Unfortunately, I have never used this method and can't give any advice. As the file generated by CarlosAg library is in Spreadsheet XML format, what I do is if I want to read or add to the file, I treat it as a purely XML file and then do my business with XML. As far as saving the file generated, the format of the generated file is Spreadsheet XML, but you can save this as an .xls file and Excel opens this file without an issue, just warning you that the file format is not correct the first time you open it. If you ignore that and open it, then you can save as any format you like from Excel itself. What I normally do is use a FileSaveDialog to get the user to select the name of the file and I use a filter to make sure it only selects .xls files like this :

                            Dim sfdReport As New SaveFileDialog
                            sfdReport.Filter = "Excel Files (\*.xls)|\*.xls"
                            sfdReport.ShowDialog()
                            book.save(sfdReport.FileName)
                        

                        That way the file is only saved as .xls. Happy Coding

                        N 1 Reply Last reply
                        0
                        • W Wayne Gaylard

                          Hi Nishkarsh I am afraid the library is only for generating reports, and cannot read Excel spreadsheets. The only way I know of reading Excel spreadsheets without Excel installed on the machine is by using ADO.Net and connecting to the spreadsheet like a database. Unfortunately, I have never used this method and can't give any advice. As the file generated by CarlosAg library is in Spreadsheet XML format, what I do is if I want to read or add to the file, I treat it as a purely XML file and then do my business with XML. As far as saving the file generated, the format of the generated file is Spreadsheet XML, but you can save this as an .xls file and Excel opens this file without an issue, just warning you that the file format is not correct the first time you open it. If you ignore that and open it, then you can save as any format you like from Excel itself. What I normally do is use a FileSaveDialog to get the user to select the name of the file and I use a filter to make sure it only selects .xls files like this :

                              Dim sfdReport As New SaveFileDialog
                              sfdReport.Filter = "Excel Files (\*.xls)|\*.xls"
                              sfdReport.ShowDialog()
                              book.save(sfdReport.FileName)
                          

                          That way the file is only saved as .xls. Happy Coding

                          N Offline
                          N Offline
                          nishkarsh_k
                          wrote on last edited by
                          #12

                          Hi zimvbcoder, Thanks for the input, i am doing the same as of now. I will tel you what exactly i wanted to achieve. I have a application which has a long list of items and values (some thing like price list) which the client wants to update on his own from time to time. He is asking that give a provision so that the list is available in Excel he can update it and then the list can be updated in the system. SO i am creating a application in which i am using CarlosAg to read from DB and create a excel file. Which client will update and then i will read it from the excel using OLEDB and update the DB (I know this is not the best way of updating a DB but client is finally the god) :-O Now CarlosAg gives output in XML format and even if i name it as .XLS then also OLEDB dose not read it. So i have to tel the client that please go to file menu and "Save As" .xls before he dose the step 2. This makes the solution not fully automated and chances are they mis out that step and we end up it issues... X| Please do let me know if there is some thing else which we can do about it... Regards Nishkarsh

                          W 1 Reply Last reply
                          0
                          • N nishkarsh_k

                            Hi zimvbcoder, Thanks for the input, i am doing the same as of now. I will tel you what exactly i wanted to achieve. I have a application which has a long list of items and values (some thing like price list) which the client wants to update on his own from time to time. He is asking that give a provision so that the list is available in Excel he can update it and then the list can be updated in the system. SO i am creating a application in which i am using CarlosAg to read from DB and create a excel file. Which client will update and then i will read it from the excel using OLEDB and update the DB (I know this is not the best way of updating a DB but client is finally the god) :-O Now CarlosAg gives output in XML format and even if i name it as .XLS then also OLEDB dose not read it. So i have to tel the client that please go to file menu and "Save As" .xls before he dose the step 2. This makes the solution not fully automated and chances are they mis out that step and we end up it issues... X| Please do let me know if there is some thing else which we can do about it... Regards Nishkarsh

                            W Offline
                            W Offline
                            Wayne Gaylard
                            wrote on last edited by
                            #13

                            Hi Nishkarsh. Personally what I would do is to include a place in the app where the client can update the products, rather than saving to Excel and then reading from the spreadsheet. This way you cut out the Excel part altogether. If the customer really insists on a spreadsheet, then I would tell him that Excel needs to be installed on the machine, and use the Microsoft Interop Assemblies to read and write to Excel format. I agree with you that relying on the client to do anything in the right order is just going to cause headaches, and I do not know of a library that can read Excel files without a reference to the Excel dll. Sorry I can't be any more help.

                            N 1 Reply Last reply
                            0
                            • W Wayne Gaylard

                              Hi Nishkarsh. Personally what I would do is to include a place in the app where the client can update the products, rather than saving to Excel and then reading from the spreadsheet. This way you cut out the Excel part altogether. If the customer really insists on a spreadsheet, then I would tell him that Excel needs to be installed on the machine, and use the Microsoft Interop Assemblies to read and write to Excel format. I agree with you that relying on the client to do anything in the right order is just going to cause headaches, and I do not know of a library that can read Excel files without a reference to the Excel dll. Sorry I can't be any more help.

                              N Offline
                              N Offline
                              nishkarsh_k
                              wrote on last edited by
                              #14

                              Hi zimvbcoder, I need a bit of help in reading .xml file. I was able to convince my client to have xml instead of xls as both can oven in excel interface. This will avoid 1 manual steep to of saving .xml as .xls. No i tried reading data from .xml files as i am new to it i am having some issues. I am using XmlTextReader class for the same and looking for XmlNodeType.Text, it seams to be working but it dives me some data before and after the data life author and versions also. Which makes my work harder as i only want the files which displayed in the excel interface when i use it to open the .xml file.. Any idea? :~

                              1 Reply Last reply
                              0
                              • W Wayne Gaylard

                                Hi Nishkarsh I have used a free library from CarlosAg for a while and it is really brilliant, as it seems to cover all bases. Here is the link CarlosAg[^]. He also has other links to other free Excel libraries out there. Definitely worth a visit. Happy Coding

                                N Offline
                                N Offline
                                nishkarsh_k
                                wrote on last edited by
                                #15

                                Hi zimvbcoder, Thanks for the earlier help I had started using "CarlosAg Excel Xml Writer Library" it was a great experience. Now i am stuck at a point for last 2 days i am not able to move ahead at all. - I have used a excel which had 2-3 merged cells as the report heading then the col headers followed by data lines. I am not able to add more then 6 lines (I am using a loop) a sample code is as followed

                                    For j = 0 To 5
                                        Row = sheet.Table.Rows.Add
                                        'Row.AutoFitHeight = False
                                        For i = 0 To DataGridView1.Columns.Count - 1
                                           Row.Cells.Add("test", DataType.\[String\], "s100")
                                        Next
                                    Next
                                

                                The above code works fine But the moment I increase "j" loop to 6 or more

                                For j = 0 To 6

                                the file is generated but when i open it using Excel 2007 it gives me error with worksheet settings and dose not opens it. I had tried all what i could have taught. Can you please help me with it. Note : I am using "NON-STRONGLY NAMED" version as i did not knew the difference and it was recommended if user is not sure. Will that make any difference? Regards Nishkarsh

                                N 1 Reply Last reply
                                0
                                • N nishkarsh_k

                                  Hi zimvbcoder, Thanks for the earlier help I had started using "CarlosAg Excel Xml Writer Library" it was a great experience. Now i am stuck at a point for last 2 days i am not able to move ahead at all. - I have used a excel which had 2-3 merged cells as the report heading then the col headers followed by data lines. I am not able to add more then 6 lines (I am using a loop) a sample code is as followed

                                      For j = 0 To 5
                                          Row = sheet.Table.Rows.Add
                                          'Row.AutoFitHeight = False
                                          For i = 0 To DataGridView1.Columns.Count - 1
                                             Row.Cells.Add("test", DataType.\[String\], "s100")
                                          Next
                                      Next
                                  

                                  The above code works fine But the moment I increase "j" loop to 6 or more

                                  For j = 0 To 6

                                  the file is generated but when i open it using Excel 2007 it gives me error with worksheet settings and dose not opens it. I had tried all what i could have taught. Can you please help me with it. Note : I am using "NON-STRONGLY NAMED" version as i did not knew the difference and it was recommended if user is not sure. Will that make any difference? Regards Nishkarsh

                                  N Offline
                                  N Offline
                                  nishkarsh_k
                                  wrote on last edited by
                                  #16

                                  Hello every one... I got to know what wrong i was doing(that time only) now just giving info as may be useful for others... There are variables for number of rows in the starting of the function. One needs to set them ;P

                                  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