473,749 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading Excel sheet into DB Table

I need some help and direction on what classes and an example or two
(article) on how to read an Excel Worksheet and insert one column into a
database table column. I am using .NET 2.0 only. What namespaces and
classes should I use and how?
--
dba123
Jul 31 '06 #1
9 22502
Maybe System.Data.Ole Db or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.

connStr = "Provider=Micro soft.Jet.OLEDB. 4.0;Extended Properties=\"Ex cel
5.0;HDR=yes\";D ata Source=test.xls ;";

You can also use MSSQL Tools about Import/Export data Wizard to import
your worksheet data into DB.

Sincerely,
simida

dba123 写道:
I need some help and direction on what classes and an example or two
(article) on how to read an Excel Worksheet and insert one column into a
database table column. I am using .NET 2.0 only. What namespaces and
classes should I use and how?
--
dba123
Jul 31 '06 #2
Thanks. What about reading the worksheet, how to pick out a certain column,
and pass that as a stream or something to a s tored procedure which inserts
the data from the worksheet?

Here's my code so far:

// Insert Root Categories
string strConn;
strConn = "Provider=Micro soft.Jet.OLEDB. 4.0; Data
Source=C:\\test .xls; Extended Properties=Exce l 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapte r myCommand = new OleDbDataAdapte r("SELECT * FROM
[Root Levels$]", strConn);

DataSet myDataSet = new DataSet();
myCommand.Fill( myDataSet, "ExcelInfo" );

//Now, using stored proc, insert the data (Specific column in the dataset)
into database table.

Also, is there a better way or new way in .NET 2.0 to read an excel worksheet?

OR can I somehow create a stream then stream each row from the excel sheet
directly to my SQL Table?
--
dba123
"simida" wrote:
Maybe System.Data.Ole Db or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.

connStr = "Provider=Micro soft.Jet.OLEDB. 4.0;Extended Properties=\"Ex cel
5.0;HDR=yes\";D ata Source=test.xls ;";

You can also use MSSQL Tools about Import/Export data Wizard to import
your worksheet data into DB.

Sincerely,
simida

dba123 写道:
I need some help and direction on what classes and an example or two
(article) on how to read an Excel Worksheet and insert one column into a
database table column. I am using .NET 2.0 only. What namespaces and
classes should I use and how?
--
dba123

Jul 31 '06 #3
Another way, you can use EXCEL2003 open XLS file, and save as .XML
file. You will process XML file more easy in .NET 2.0.

You can also anaylse EXCEL file format to read data firstly. More
information about EXCEL file format, plz refer to this.

http://chicago.sourceforge.net/devel/docs/excel/

Sincerely,
simida

dba123 写道:
Thanks. What about reading the worksheet, how to pick out a certain column,
and pass that as a stream or something to a s tored procedure which inserts
the data from the worksheet?

Here's my code so far:

// Insert Root Categories
string strConn;
strConn = "Provider=Micro soft.Jet.OLEDB. 4.0; Data
Source=C:\\test .xls; Extended Properties=Exce l 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapte r myCommand = new OleDbDataAdapte r("SELECT *FROM
[Root Levels$]", strConn);

DataSet myDataSet = new DataSet();
myCommand.Fill( myDataSet, "ExcelInfo" );

//Now, using stored proc, insert the data (Specific column in the dataset)
into database table.

Also, is there a better way or new way in .NET 2.0 to read an excel worksheet?

OR can I somehow create a stream then stream each row from the excel sheet
directly to my SQL Table?
--
dba123
"simida" wrote:
Maybe System.Data.Ole Db or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.

connStr = "Provider=Micro soft.Jet.OLEDB. 4.0;Extended Properties=\"Ex cel
5.0;HDR=yes\";D ata Source=test.xls ;";

You can also use MSSQL Tools about Import/Export data Wizard to import
your worksheet data into DB.

Sincerely,
simida

dba123 写道:
I need some help and direction on what classes and an example or two
(article) on how to read an Excel Worksheet and insert one column into a
database table column. I am using .NET 2.0 only. What namespaces and
classes should I use and how?


--
dba123
Jul 31 '06 #4
I looked at the link, seems more complex than needed to simply read an excel
file and figure out which sheets and columns to work with. what is BIFF?
There has to be an easy way to pinpoint specific worksheets in a workbook
and certain columns through .NET 2.0 in conjunction with reading and writing
to a DB table.

Yea, I could use XML and the .NET XML reader class but right now, time is
of the essence.

--
dba123
"simida" wrote:
Another way, you can use EXCEL2003 open XLS file, and save as .XML
file. You will process XML file more easy in .NET 2.0.

You can also anaylse EXCEL file format to read data firstly. More
information about EXCEL file format, plz refer to this.

http://chicago.sourceforge.net/devel/docs/excel/

Sincerely,
simida

dba123 写道:
Thanks. What about reading the worksheet, how to pick out a certain column,
and pass that as a stream or something to a s tored procedure which inserts
the data from the worksheet?

Here's my code so far:

// Insert Root Categories
string strConn;
strConn = "Provider=Micro soft.Jet.OLEDB. 4.0; Data
Source=C:\\test .xls; Extended Properties=Exce l 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapte r myCommand = new OleDbDataAdapte r("SELECT * FROM
[Root Levels$]", strConn);

DataSet myDataSet = new DataSet();
myCommand.Fill( myDataSet, "ExcelInfo" );

//Now, using stored proc, insert the data (Specific column in the dataset)
into database table.

Also, is there a better way or new way in .NET 2.0 to read an excel worksheet?

OR can I somehow create a stream then stream each row from the excel sheet
directly to my SQL Table?
--
dba123
"simida" wrote:
Maybe System.Data.Ole Db or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.
>
connStr = "Provider=Micro soft.Jet.OLEDB. 4.0;Extended Properties=\"Ex cel
5.0;HDR=yes\";D ata Source=test.xls ;";
>
You can also use MSSQL Tools about Import/Export data Wizard to import
your worksheet data into DB.
>
Sincerely,
simida
>
dba123 写道:
>
I need some help and direction on what classes and an example or two
(article) on how to read an Excel Worksheet and insert one column into a
database table column. I am using .NET 2.0 only. What namespaces and
classes should I use and how?


--
dba123
>
>

Jul 31 '06 #5
If you want to read certain columns, you can use SELECT statements in
SQL language.
Such as,
"SELECT [Column1], [Column2] FROM [Root Levels$]"

OR, you operate it on the table which is contained in the DataSet
object.
Table.Select() method.

As far as I know, it is a easy way to read EXCEL file. :(

Hope a more elegant way to achieve it.

Sincerely,
simida
dba123 写道:
I looked at the link, seems more complex than needed to simply read an excel
file and figure out which sheets and columns to work with. what is BIFF?
There has to be an easy way to pinpoint specific worksheets in a workbook
and certain columns through .NET 2.0 in conjunction with reading and writing
to a DB table.

Yea, I could use XML and the .NET XML reader class but right now, time is
of the essence.

--
dba123
"simida" wrote:
Another way, you can use EXCEL2003 open XLS file, and save as .XML
file. You will process XML file more easy in .NET 2.0.

You can also anaylse EXCEL file format to read data firstly. More
information about EXCEL file format, plz refer to this.

http://chicago.sourceforge.net/devel/docs/excel/

Sincerely,
simida

dba123 写道:
Thanks. What about reading the worksheet, how to pick out a certain column,
and pass that as a stream or something to a s tored procedure which inserts
the data from the worksheet?
>
Here's my code so far:
>
// Insert Root Categories
string strConn;
strConn = "Provider=Micro soft.Jet.OLEDB. 4.0; Data
Source=C:\\test .xls; Extended Properties=Exce l 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapte r myCommand = new OleDbDataAdapte r("SELECT * FROM
[Root Levels$]", strConn);
>
DataSet myDataSet = new DataSet();
myCommand.Fill( myDataSet, "ExcelInfo" );
>
//Now, using stored proc, insert the data (Specific column in the dataset)
into database table.
>
Also, is there a better way or new way in .NET 2.0 to read an excel worksheet?
>
OR can I somehow create a stream then stream each row from the excelsheet
directly to my SQL Table?
>
>
--
dba123
>
>
"simida" wrote:
>
Maybe System.Data.Ole Db or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.

connStr = "Provider=Micro soft.Jet.OLEDB. 4.0;Extended Properties=\"Ex cel
5.0;HDR=yes\";D ata Source=test.xls ;";

You can also use MSSQL Tools about Import/Export data Wizard to import
your worksheet data into DB.

Sincerely,
simida

dba123 写道:

I need some help and direction on what classes and an example or two
(article) on how to read an Excel Worksheet and insert one columninto a
database table column. I am using .NET 2.0 only. What namespaces and
classes should I use and how?
>

--
dba123
Jul 31 '06 #6
To get excel sheet NAMES, use the following - just pass in the filepath
of the excel spreadsheet:

<code>
public string[] ExcelSheetNames (String excelFile)
{
DataTable dt;
string connString = ExcelExtendedCo nn(excelFile);
using (OleDbConnectio n objConn = new
OleDbConnection (connString))
{
objConn.Open();
dt =
objConn.GetOleD bSchemaTable(Ol eDbSchemaGuid.T ables, null);
if(dt == null)
{
return null;
}
string[] res = new string[dt.Rows.Count];
for (int i = 0; i < res.Length; i++)
{
string name = dt.Rows[i]["TABLE_NAME "].ToString();
if(name[0] == '\'')
{
//numeric sheetnames get single quotes around
them in the schema.
//remove them here
if(Regex.IsMatc h(name, @"^'\d\w+\$'$") )
{
name = name.Substring( 1, name.Length - 2);
}
}
res[i] = name;
}
return res;
}
}
</code>

To get the column names of the worksheet in the excel spreadsheet:

<code>
public DataTable columnNames(Str ing excelFile, String sheetName)
{
string connString = ExcelExtendedCo nn(excelFile);
using (OleDbConnectio n conn = new
OleDbConnection (connString))
{
OleDbCommand odc = new
OleDbCommand(st ring.Format("Se lect * FROM [{0}$]", sheetName), conn);
conn.Open();
OleDbDataReader reader = odc.ExecuteRead er();
DataTable sheetSchema = reader.GetSchem aTable();
return sheetSchema;
}
}
</code>

Using the methods above, you can then pull the data of the excel
spreadsheet:

<code>
public DataTable originalData(St ring _filename,Strin g _sheet)
{
DataTable dt = new DataTable();
OdbcConnection conn = new OdbcConnection( );
OdbcCommand cmd = new OdbcCommand();
conn.Connection String = string.Format(@ "Driver={{Micro soft
Excel Driver (*.xls)}};DBQ={ 0};ReadOnly=0;" , _filename);
cmd.Connection = conn;
cmd.CommandType = CommandType.Tex t;
cmd.CommandText = "SELECT * FROM [" + _sheet + "$]";
OdbcDataAdapter oda = new OdbcDataAdapter ();
oda.SelectComma nd = cmd;
oda.Fill(dt);
return dt;
}

</code>

I know this works because it's pulled directly from an application that
I've just written.


simida wrote:
If you want to read certain columns, you can use SELECT statements in
SQL language.
Such as,
"SELECT [Column1], [Column2] FROM [Root Levels$]"

OR, you operate it on the table which is contained in the DataSet
object.
Table.Select() method.

As far as I know, it is a easy way to read EXCEL file. :(

Hope a more elegant way to achieve it.

Sincerely,
simida
dba123 写道:
I looked at the link, seems more complex than needed to simply read an excel
file and figure out which sheets and columns to work with. what is BIFF?
There has to be an easy way to pinpoint specific worksheets in a workbook
and certain columns through .NET 2.0 in conjunction with reading and writing
to a DB table.

Yea, I could use XML and the .NET XML reader class but right now, timeis
of the essence.

--
dba123
"simida" wrote:
Another way, you can use EXCEL2003 open XLS file, and save as .XML
file. You will process XML file more easy in .NET 2.0.
>
You can also anaylse EXCEL file format to read data firstly. More
information about EXCEL file format, plz refer to this.
>
http://chicago.sourceforge.net/devel/docs/excel/
>
Sincerely,
simida
>
dba123 写道:
>
Thanks. What about reading the worksheet, how to pick out a certain column,
and pass that as a stream or something to a s tored procedure whichinserts
the data from the worksheet?

Here's my code so far:

// Insert Root Categories
string strConn;
strConn = "Provider=Micro soft.Jet.OLEDB. 4.0; Data
Source=C:\\test .xls; Extended Properties=Exce l 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapte r myCommand = new OleDbDataAdapte r("SELECT * FROM
[Root Levels$]", strConn);

DataSet myDataSet = new DataSet();
myCommand.Fill( myDataSet, "ExcelInfo" );

//Now, using stored proc, insert the data (Specific column in the dataset)
into database table.

Also, is there a better way or new way in .NET 2.0 to read an excelworksheet?

OR can I somehow create a stream then stream each row from the excel sheet
directly to my SQL Table?


--
dba123


"simida" wrote:

Maybe System.Data.Ole Db or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.
>
connStr = "Provider=Micro soft.Jet.OLEDB. 4.0;Extended Properties=\"Ex cel
5.0;HDR=yes\";D ata Source=test.xls ;";
>
You can also use MSSQL Tools about Import/Export data Wizard to import
your worksheet data into DB.
>
Sincerely,
simida
>
dba123 写道:
>
I need some help and direction on what classes and an example or two
(article) on how to read an Excel Worksheet and insert one column into a
database table column. I am using .NET 2.0 only. What namespaces and
classes should I use and how?


--
dba123

>

>
Jul 31 '06 #7
Forgot to say - once you've got the datatable with all the data, you're
better off using the SqlBulkCopy command if you're using SQL server,
ie;

private void BulkCopyIntoSQL (DataTable dt)
{
SqlBulkCopy bc = new SqlBulkCopy(myC onnection,
SqlBulkCopyOpti ons.Default);
bc.DestinationT ableName = "yourTableName" ;
bc.SqlRowsCopie d += new
SqlRowsCopiedEv entHandler(bc_S qlRowsCopied);
bc.NotifyAfter = 1;
bc.BatchSize = 250;
bc.WriteToServe r(dt);
bc.Close();
}

Jul 31 '06 #8
Hi Code Monkey ,

I need to read the sheetnames of Excel files in my program using
ASP.Net 1.1.
I tried ur way of doing it, but .Net is unable to recognise functions
ExcelExtendedCo nn and Regex.
Could you please specify what namespaces/functions have been missing
here?

Thanks

Aug 29 '06 #9
Sorry - my code was written in ASP.Net v2, but it should still work.

Guess I forgot to include other code!

OK here goes:

just pass the FULL file path of the excel spreadsheet into:

<code>
private static string ExcelExtendedCo nn(string excelFile)
{
return "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=\"" +
excelFile + "\";Extende d Properties=\"Ex cel 8.0;HDR=Yes;\"" ;
}
</code>

as for the Regex namespace: System.Text.Reg ularExpressions

Raj11 wrote:
Hi Code Monkey ,

I need to read the sheetnames of Excel files in my program using
ASP.Net 1.1.
I tried ur way of doing it, but .Net is unable to recognise functions
ExcelExtendedCo nn and Regex.
Could you please specify what namespaces/functions have been missing
here?

Thanks
Aug 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
18848
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at the data in the excel file that if the first character in the excel file cell is numeric it will read and write only numeric values only. If I sort the coloumn in the excel file and the first character in the cell read is alphanumeric then only...
0
2868
by: dgoel | last post by:
Hi, I Have a text file & I want to open it in excel sheet ( withou importing). I have written code for it, but it is not opening exce sheet. It opens the text file, but does not create a excel sheet populate it. I do not know what the problem is, can someone please hel out with this one, Here is my code: StreamReader dataFileReader = null; FileInfo file = null; Excel.Application ExcelObj = new Excel.Application();
14
5801
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do tell those to me too coz I have no idea how to go about doing it.
1
6549
by: dave | last post by:
I am having a problem i bleieve a number of other individuals have had before. I have tried the solutions found on the net however I still have the problem. I am hosting a web site on an external server (i do not have access to registry). I am using the following connection string to read values from rows from an excel sheet into a dataset/datatable. One of the columns contains alpha-numeric values ie. 400c
0
1815
by: Eric | last post by:
Hi all, First i appoligise for my bad english. For a friend i need to create a script that can read XLS documents. Well that's not the problem anymore... with the code (down here) i've already writen i can read tru the document. All rows and colums... but there one fault.. ROW A has normal characters (like A-Z) but row C contains thai carakters (using font : Angsana New in EXCEL or WORD). When i run the following script i see the words on...
1
1999
by: pushya | last post by:
Hi everyone I am just joined this and new in PHP.I have a PHP script which reads data form Excel sheet but there are certain changes needed like it does not reads formats like negative values from sheet. Can anyone please suggest me the best solution for reading Excel sheet in Unix. Also I want to convert it in pipe delimited format.
7
12071
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
1
1966
by: PeacefulSoul | last post by:
Hello, I have a lot of excel sheets which columns are slightly different, i want to import all of these sheets (one at a time) into ONE SQL TABLE. I'll give an example : Say ive written the required program and called it Excel2sql converter. So Excel2sql takes an excel sheet and create a database table with the data of that excel sheet, For example say i have the following excel sheet: Excel_Sheet_1 -----------------------------...
2
2032
by: Pradeep23 | last post by:
Hi all, I want to export 1 big excel workbook containing 200 Sheets in it, and is classified according to Class. Each sheet name is unique according to the class. I am building a package to export data from excel sheet to SQL table. I want to export whole Excel workbook in one table of SQL. I need help here. I want to add one column Class in the SQL table. I want to add Sheet name into that Class column, so for every sheet if there are 10...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.