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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. importing data from a xls file to an array

importing data from a xls file to an array

Scheduled Pinned Locked Moved C#
csharpdata-structureshelp
9 Posts 2 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.
  • G Offline
    G Offline
    guchu
    wrote on last edited by
    #1

    can neone help me with importing data from a xls file to an array ... i am not using databases and i want it to be importes into an array in c# class :^)

    L 1 Reply Last reply
    0
    • G guchu

      can neone help me with importing data from a xls file to an array ... i am not using databases and i want it to be importes into an array in c# class :^)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Where did you get stuck? Seems pretty straight forward...

      Check out the CodeProject forum Guidelines[^]

      G 1 Reply Last reply
      0
      • L Lost User

        Where did you get stuck? Seems pretty straight forward...

        Check out the CodeProject forum Guidelines[^]

        G Offline
        G Offline
        guchu
        wrote on last edited by
        #3

        using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; using Excel = Microsoft.Office.Interop.Excel; I am learning to use c# at the moment .. i have this code to read an excel file ... its incomplete now asi am yet to define the other varibles but as of now i want to have a user input in textbox1 and then that is to be the file name of the excel file to open ... can ne one help me namespace WindowsFormsApplication7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { try { // open the users excel workbook. Excel.Application ExcelApp = new Excel.ApplicationClass(); ExcelApp.Visible = true; String WorkbookPath = "c:/Users/Guchu/Desktop/a.xls"; Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(WorkbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,false,0,true,false,false); Excel.Sheets excelSheets = ExcelWorkbook.Worksheets; string CurrentSheet = "CurrentSheet"; excelSheets = (Excel.Sheets) excelSheets.get_Item(CurrentSheet); } catch (Exception theException) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, theException.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, theException.Source); MessageBox.Show(errorMessage, "Error"); } } private void button2_Click(object sender, EventArgs e) { } } }

        L 1 Reply Last reply
        0
        • G guchu

          using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; using Excel = Microsoft.Office.Interop.Excel; I am learning to use c# at the moment .. i have this code to read an excel file ... its incomplete now asi am yet to define the other varibles but as of now i want to have a user input in textbox1 and then that is to be the file name of the excel file to open ... can ne one help me namespace WindowsFormsApplication7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { try { // open the users excel workbook. Excel.Application ExcelApp = new Excel.ApplicationClass(); ExcelApp.Visible = true; String WorkbookPath = "c:/Users/Guchu/Desktop/a.xls"; Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(WorkbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,false,0,true,false,false); Excel.Sheets excelSheets = ExcelWorkbook.Worksheets; string CurrentSheet = "CurrentSheet"; excelSheets = (Excel.Sheets) excelSheets.get_Item(CurrentSheet); } catch (Exception theException) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, theException.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, theException.Source); MessageBox.Show(errorMessage, "Error"); } } private void button2_Click(object sender, EventArgs e) { } } }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Why not just use the process class with the start method to instantiate excel.exe to open with your workbook in question?

          Check out the CodeProject forum Guidelines[^]

          G 1 Reply Last reply
          0
          • L Lost User

            Why not just use the process class with the start method to instantiate excel.exe to open with your workbook in question?

            Check out the CodeProject forum Guidelines[^]

            G Offline
            G Offline
            guchu
            wrote on last edited by
            #5

            as i have told u that i am new to c# hence i might have done it the harder way ... but it will be easy for me to understand if u can give me the code ... what i did was ... for the text box i wrote... this.textbox1 = new system.windows.forms.textbox(); string a = textbox1.text, then in the button 1 part for opening the code i want to insert this string a as the file name in the file path string workbookpath = @ "c:\.....\" + a ; but this doesnt works ... can u tell me the correct way

            L 1 Reply Last reply
            0
            • G guchu

              as i have told u that i am new to c# hence i might have done it the harder way ... but it will be easy for me to understand if u can give me the code ... what i did was ... for the text box i wrote... this.textbox1 = new system.windows.forms.textbox(); string a = textbox1.text, then in the button 1 part for opening the code i want to insert this string a as the file name in the file path string workbookpath = @ "c:\.....\" + a ; but this doesnt works ... can u tell me the correct way

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              guchu wrote:

              this.textbox1 = new system.windows.forms.textbox(); string a = textbox1.text,

              A new textbox would be blank, so string a would be "", debug through your code. I will not give you the code but go to the documentation[^] and check out the process class, specifically the start method, and search the forums a bit, you'll find your answer. You just have to research for it. Also, I would look into the OpenFileDialog to pick your file, that way you have no restriction on the residual location of your excel files.

              Check out the CodeProject forum Guidelines[^]

              G 1 Reply Last reply
              0
              • L Lost User

                guchu wrote:

                this.textbox1 = new system.windows.forms.textbox(); string a = textbox1.text,

                A new textbox would be blank, so string a would be "", debug through your code. I will not give you the code but go to the documentation[^] and check out the process class, specifically the start method, and search the forums a bit, you'll find your answer. You just have to research for it. Also, I would look into the OpenFileDialog to pick your file, that way you have no restriction on the residual location of your excel files.

                Check out the CodeProject forum Guidelines[^]

                G Offline
                G Offline
                guchu
                wrote on last edited by
                #7

                i understand that u want me to learn the easier method but even using this one i am able to open the file if i specify the file name as "C:\....\a.xls" ... all i want to know is if i want to take this last part(a.xls) from the user since the file neways has to be on the desktop then how do i accomodate it in the code ... i have tried doing it using the + operator but it is not done this way as it seems as the compiler doesnt reads the remaining thing written outside "" ... i would be obliged if u can help ... i need it desperately

                L 1 Reply Last reply
                0
                • G guchu

                  i understand that u want me to learn the easier method but even using this one i am able to open the file if i specify the file name as "C:\....\a.xls" ... all i want to know is if i want to take this last part(a.xls) from the user since the file neways has to be on the desktop then how do i accomodate it in the code ... i have tried doing it using the + operator but it is not done this way as it seems as the compiler doesnt reads the remaining thing written outside "" ... i would be obliged if u can help ... i need it desperately

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You reset your textbox by doing Textbox1 = new System.Windows.Forms.Textbox(); if you take that out, you should be fine provided the file exists but your design is horrible, inefficient and there is a faster, prettier way of doing it.

                  Check out the CodeProject forum Guidelines[^]

                  G 1 Reply Last reply
                  0
                  • L Lost User

                    You reset your textbox by doing Textbox1 = new System.Windows.Forms.Textbox(); if you take that out, you should be fine provided the file exists but your design is horrible, inefficient and there is a faster, prettier way of doing it.

                    Check out the CodeProject forum Guidelines[^]

                    G Offline
                    G Offline
                    guchu
                    wrote on last edited by
                    #9

                    I will go through the documentation and redesign the program .... i will get back to u if i have any quaries regarding this ... as of now thank you very much for helping me out ....

                    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