![]() |
|
#1
|
||||
|
||||
Reading a Microsoft Excel file in C#I am trying to open an Excel file for reading. However, I'm not sure what referecence to use in my c# project, and what the using statement should be. I have added the "office" reference to my project, but I can't use the Excel object to create new types e.g. the statement "Excel.Application app = new Excel.Appliocation()" is not working. Any help or ideas? Thanks Aresti |
||||
|
#2
|
||||
|
||||
Re: Reading an Excel file in C#Ok, I think I've made some progress in this. There's a microsoft Excel 9.0 Object library which I can add as a reference in my project, and the compiler seems to pick up the Excel object. I will come back and maybe post mty code if I get this working.
Aresti |
|
#3
|
||||
|
||||
Re: Reading an Excel file in C#Hi
This is for C# - Reading and Writing to Excel Ok, I think I have worked out or borrowed a way of connecting to an Excel file, here with path: "C:\Test Projects\Excel example\Excel - reading an excel file\ReadThis.xls", like you connect to a database. You need the "System.Data" reference and use the statement "using using System.Data.OleDb;" at the top of your code. I have tried to comment the code as you go along so hopefully this makes sense. I got this to work yesterday. The file ReadThis.xls has a column called A with two values that get printed to a MessageBox. It also has a column called B that has all its 1's changed into 2's. This looks long, but it also has two parts! A read part and an update part. C-SHARP / C# Code:
If you have any questions please ask. Aresti Last edited by admin : 28-Feb-2008 at 07:08.
Reason: Please insert your example C# codes between [CSHARP] and [/CSHARP] tags
|
|
#4
|
|||
|
|||
Re: Reading an Excel file in C#Hi!
Do you know any other way to read a xls file? Like a method that read cells? Tks! |
|
#5
|
||||
|
||||
Re: Reading an Excel file in C#Hi Ken
Unfortunately I don't know of another way of reading in the spreadsheets at the moment. It took me a while to work out the above solution. In my example you have to loop through the rows of the spreadsheet, and you have to have it setup like a table (e.g. column A headed by "A" and column B headed by "B", or headed by whatever your column headings are). I think that in VB there is a way of accessing the cells individually like you are asking for. Cheers Arestispartan |
|
#6
|
|||
|
|||
Re: Reading a Microsoft Excel file in C#HI,
Thank you very much for the code help I followed your code, however I am getting a strange scenario error. My Excel sheet has titles in Numeric instead of Alphabetic. When I use the numeric value in the select query, its not working, However if the column title is Alphabetic, it is working This is WORKING ExcelQuery = "Select A from [MySheet$]"; // from Sheet1"; BUT This is not working (Excelsheet column titles are in numeric value) ExcelQuery = "Select 1 from [MySheet$]"; // from Sheet1"; Please help me on this |
|
#7
|
|||
|
|||
Re: Reading a Microsoft Excel file in C#name the column 'one' instead of '1'. Maybe numerics aren't a valid format for column headers for 'tables'.
Justin Fox |
|
#8
|
|||
|
|||
Re: Reading a Microsoft Excel file in C#I implemented a writing to an excel file a long time ago, it was done by using the _sheets object and _WorkBook object.
I ran into problems with saving the darn thing tho. But writing to the excel file was pretty easy. Just google writing to excel file using _Sheets and _Workbook etc... hope you can find something Justin Fox |
|
#9
|
||||
|
||||
Re: Reading a Microsoft Excel file in C#Hi Arul
I will look into it when I get some time... Have you tried putting quotes around the number 1 like ExcelQuery = "Select '1' from [MySheet$]"; or ExcelQuery = "Select "1" from [MySheet$]"; ? It may be that you can't use numeric constants as column headings. Can you not add in an extra (top) row into your sheet as headings? Arestispartan |
|
#10
|
||||
|
||||
Re: Reading a Microsoft Excel file in C#Hi Justin
What reference do you need for your project to use the _sheets and _workbook object? Thanks Arestispartan |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Reading MS Excel file through C++ code | swati_npl | C++ Forum | 5 | 03-Mar-2008 05:12 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| Reading and Writing to a text file | raptorhawk | C++ Forum | 16 | 14-Apr-2005 13:09 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The