473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

C# code to read excel files

Hi Guys,
My name is Henry, I'm a new member. I need to know how to read cells from an excel spreadsheet using C#. Can anyone please assist.
Aug 6 '08 #1
5 1591
kenobewan
4,871 Expert 4TB
Welcome to the site. What have you tried so far? If you search the site you are likely to find examples.
Aug 6 '08 #2
r035198x
13,262 8TB
Welcome to the site. What have you tried so far? If you search the site you are likely to find examples.
Hint: ADO.NET
Aug 6 '08 #3
Curtis Rutland
3,256 Expert 2GB
If you look in the Howtos section, there are two articles about using Databases in your .NET program. Read them.

You can treat a spreadsheet as a database. Go to connectionstrings.com to find the connection string for connecting to Excel.
Aug 6 '08 #4
Thanks guys, I got it. Here's how it goes in case anyone else needs
Expand|Select|Wrap|Line Numbers
  1. public DataTable Connect()
  2.         {
  3.             string connStr;
  4.             connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Practice.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
  5.             OleDbConnection con = new OleDbConnection(connStr);
  6.             con.Open();
  7.             string Query = "SELECT * FROM [Sheet1$]";
  8.             DataTable Table = new DataTable();
  9.             OleDbDataAdapter adapt = new OleDbDataAdapter(Query, con);
  10.             adapt.Fill(Table);
  11.             con.Close();
  12.             return Table;
  13.         }       
  14.  
  15. private void button1_Click_1(object sender, EventArgs e)
  16.         {
  17.             int num = Connect().Rows.Count;
  18.             string server, account, Password;
  19.  
  20.             listBox1.Items.Clear();
  21.             for (int i = 0; i < num; i++)
  22.             {
  23.                 server = Connect().Rows[i]["Host"].ToString();
  24.                 account = Connect().Rows[i]["Account"].ToString();
  25.                 Password = Connect().Rows[i]["Passwd"].ToString();
  26.                 listBox1.Items.Add(i + "\t" + server + "\t\t " + account + "\t\t" + Password);
  27.             }
  28.         }
Aug 12 '08 #5
Curtis Rutland
3,256 Expert 2GB
Just so you know, that's not the most efficient way to do things.

Every time you call Connect() you are redoing your whole query. What you should do is something like this:
Expand|Select|Wrap|Line Numbers
  1. DataTable dt = Connect();
  2. int num = dt.Rows.Count;
  3. .
  4. .
  5. .
  6.  
That way, you only run the query once.
Aug 12 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Bob Bedford | last post by:
I've a page where users may enter a zip code (text edit) and I want to fill a combobox with existing cities depending on the zip code entered. Let give an example. I've my text edit like:...
10
by: QQ | last post by:
I am trying to read data from Excel files I use the following codes, however, it didn't work Can anyone tell me what the format excel files have, what I should do to read it from C? Thanks a...
6
by: Anand | last post by:
Hello, Can I get some help on how to read the excel files using python? from win32com.client import Dispatch xlApp = Dispatch("Excel.Application") xlWb = xlApp.Workbooks.Open("Read.xls")...
2
by: polabear | last post by:
I'm converting a VB6/SQL Server application to a web application. I need to read data to and from Excel and Word. I'm not allowed to install Excel and Word on the server, only on the client. Is...
4
by: Juliano.net | last post by:
How can I read an Excel 2003 XML Spreadsheet and save it as an Excel 97 file?
3
by: sajithkahawatta | last post by:
is there any one used aspose.excel i used automation to read a exel doc .but we have to install ms excel to do that so i suppose to use aspose.excel can any one give me the 'Aspose.Excel.dll' and...
7
by: Wernerh | last post by:
Hi all, Have created a vb application that interacts with Excel for report purposes. Base application and excel file is on host pc on network. I require the clients to access the excel workbook for...
0
by: dprjessie | last post by:
Hello, I am a Web programmer and I'm working on my first desktop application as a favor for a friend. I'm sure I have a stupid error here, but there is no error being thrown so I can't figure out...
2
by: Riak | last post by:
Hellow Helpers: I am doing data entry job manually and data is huge. Now I was told to write/get some sort of program/script which can do this job quickly, otherwise I will loose job. I am good...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.