472,794 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 22350
Maybe System.Data.OleDb or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.

connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel
5.0;HDR=yes\";Data 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=Microsoft.Jet.OLEDB.4.0; Data
Source=C:\\test.xls; Extended Properties=Excel 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter("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.OleDb or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.

connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel
5.0;HDR=yes\";Data 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=Microsoft.Jet.OLEDB.4.0; Data
Source=C:\\test.xls; Extended Properties=Excel 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter("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.OleDb or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.

connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel
5.0;HDR=yes\";Data 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=Microsoft.Jet.OLEDB.4.0; Data
Source=C:\\test.xls; Extended Properties=Excel 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter("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.OleDb or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.
>
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel
5.0;HDR=yes\";Data 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=Microsoft.Jet.OLEDB.4.0; Data
Source=C:\\test.xls; Extended Properties=Excel 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter("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.OleDb or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.

connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel
5.0;HDR=yes\";Data 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 = ExcelExtendedConn(excelFile);
using (OleDbConnection objConn = new
OleDbConnection(connString))
{
objConn.Open();
dt =
objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables , 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.IsMatch(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(String excelFile, String sheetName)
{
string connString = ExcelExtendedConn(excelFile);
using (OleDbConnection conn = new
OleDbConnection(connString))
{
OleDbCommand odc = new
OleDbCommand(string.Format("Select * FROM [{0}$]", sheetName), conn);
conn.Open();
OleDbDataReader reader = odc.ExecuteReader();
DataTable sheetSchema = reader.GetSchemaTable();
return sheetSchema;
}
}
</code>

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

<code>
public DataTable originalData(String _filename,String _sheet)
{
DataTable dt = new DataTable();
OdbcConnection conn = new OdbcConnection();
OdbcCommand cmd = new OdbcCommand();
conn.ConnectionString = string.Format(@"Driver={{Microsoft
Excel Driver (*.xls)}};DBQ={0};ReadOnly=0;", _filename);
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [" + _sheet + "$]";
OdbcDataAdapter oda = new OdbcDataAdapter();
oda.SelectCommand = 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=Microsoft.Jet.OLEDB.4.0; Data
Source=C:\\test.xls; Extended Properties=Excel 8.0;";
//You must use the $ after the object you reference in the
spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter("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.OleDb or ODBC will help you. I used OleDb to connect
EXCEL Worksheet.
>
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel
5.0;HDR=yes\";Data 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(myConnection,
SqlBulkCopyOptions.Default);
bc.DestinationTableName = "yourTableName";
bc.SqlRowsCopied += new
SqlRowsCopiedEventHandler(bc_SqlRowsCopied);
bc.NotifyAfter = 1;
bc.BatchSize = 250;
bc.WriteToServer(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
ExcelExtendedConn 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 ExcelExtendedConn(string excelFile)
{
return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
excelFile + "\";Extended Properties=\"Excel 8.0;HDR=Yes;\"";
}
</code>

as for the Regex namespace: System.Text.RegularExpressions

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
ExcelExtendedConn 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
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...
0
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...
14
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...
1
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...
0
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...
1
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...
7
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
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...
2
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...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?

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.