|
Comments and Discussions
|
 |
 |
Hello Sir,
Can you help me with solution to import data from excel document containing more then one worksheet to sql server.
|
|
|
|
|
|
 |
Very good article Vivekananda Swamy.M
|
|
|
|
 |
It worked perfectly fine.
|
|
|
|
|
 |
Very good article n very helpful.Thanks
|
|
|
|
 |
I've created a (free) app called ClipTable, which pretty much converts anything into a SQL Table, using no other than - the Clipboard.
It is guaranteed to save a lot of time and headache when it comes to Excel, or any other import into a SQL Table.
Here's a link for more info -
http://www.doccolabs.com/products_cliptable.html[^]
|
|
|
|
 |
Thank you for this example, but I want if the value is null returns empty (null) without zero(0), how is it?
|
|
|
|
 |
Hi, i got error at this line!
OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", oconn);
I have Sheet1 but it keeps saying it does not exist!
How do i go around fixing it??
|
|
|
|
 |
hi,this is harshad panchal.i'm beginner to programming.I want to know how to display excel data in data grid view...And how to import that all excel sheets data into Access database...Please send me mail or reply to this..please urgent.....i will wait for ur reply... Smile |
|
|
|
|
 |
HI, If someone is having this error"An attempt to attach an auto-named database for file "
You have to add this line of Code
Integrated Security=True;User Instance=True;
in all 3 Appearances of these lines of Code(Sql Connection)
SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes");
Modified Code:
SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Integrated Security=True;User Instance=True;AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes");
For details http://forums.asp.net/t/879454.aspx/1[^]
Here goes Complete Code for The File"Excel2Sql.aspx"
Code Starts here
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void insertdata_Click(object sender, EventArgs e)
{
OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("example.xls") + ";Extended Properties=Excel 8.0");
try
{
OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", oconn);
oconn.Open();
OleDbDataReader odr = ocmd.ExecuteReader();
string fname = "";
string lname = "";
string mobnum = "";
string city = "";
string state = "";
string zip = "";
while (odr.Read())
{
fname = valid(odr, 0);
lname = valid(odr, 1);
mobnum = valid(odr, 2);
city = valid(odr, 3);
state = valid(odr, 4);
zip = valid(odr, 5);
insertdataintosql(fname, lname, mobnum, city, state, zip);
}
oconn.Close();
}
catch (DataException ee)
{
lblmsg.Text = ee.Message;
lblmsg.ForeColor = System.Drawing.Color.Red;
}
finally
{
lblmsg.Text = "Data Inserted Sucessfully";
lblmsg.ForeColor = System.Drawing.Color.Green;
}
}
protected string valid(OleDbDataReader myreader, int stval)//if any columns are found null then they are replaced by zero
{
object val = myreader[stval];
if (val != DBNull.Value)
return val.ToString();
else
return Convert.ToString(0);
}
protected void viewdata_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Integrated Security=True;User Instance=True;AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes");
try
{
SqlDataAdapter sda = new SqlDataAdapter("select * from emp", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (DataException de)
{
lblmsg.Text = de.Message;
lblmsg.ForeColor = System.Drawing.Color.Red;
}
finally
{
lblmsg.Text = "Data Shown Sucessfully";
lblmsg.ForeColor = System.Drawing.Color.Green;
}
}
public void insertdataintosql(string fname, string lname, string mobnum, string city, string state, string zip)
{
SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Integrated Security=True;User Instance=True;AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into emp(fname,lname,mobnum,city,state,zip) values(@fname,@lname,@mobnum,@city,@state,@zip)";
cmd.Parameters.Add("@fname", SqlDbType.NVarChar).Value = fname;
cmd.Parameters.Add("@lname", SqlDbType.NVarChar).Value = lname;
cmd.Parameters.Add("@mobnum", SqlDbType.NVarChar).Value = mobnum;
cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = city;
cmd.Parameters.Add("@state", SqlDbType.NVarChar).Value = state;
cmd.Parameters.Add("@zip", SqlDbType.Int).Value = Convert.ToInt32(zip);
cmd.CommandType = CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
protected void deletedata_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Integrated Security=True;User Instance=True;AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes");
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "delete from emp";
cmd.CommandType = CommandType.Text;
conn.Open();
cmd.ExecuteScalar();
conn.Close();
}
catch (DataException de1)
{
lblmsg.Text = de1.Message;
lblmsg.ForeColor = System.Drawing.Color.Red;
}
finally
{
lblmsg.Text = "Data Deleted Sucessfully";
lblmsg.ForeColor = System.Drawing.Color.Red;
}
}
}
|
|
|
|
 |
Excellent , Simple And Elegant
I was Developing a Classified Submission Software and An hour ago, I stuck at the Point of Entering the 1000 Sites in CSV format in the Application.I was Searching for half an hour now for a Simple Solution for to Import This Excel Sheet's data to SQL Server, But I was just lost. This Project helped me in such a simple Thanks a lot
|
|
|
|
|
 |
Simple and useful and right to the point. Good Job! (I change the location of the the table to my local server to make the connection free from attachments)
|
|
|
|
 |
Very Clear and Nice Article
|
|
|
|
 |
Hello,
I need to retain the null values when I import from excel to SQL Server. The valid function currently replaces any nulls with zeros. I was not able to modify the valid() method to retain null values.
How best to retain null values?
Greatly appreciated !!
modified on Sunday, October 24, 2010 11:27 AM
|
|
|
|
 |
hi,this is pardha.i'm beginner to programming.I want to know how to display excel data in data grid view...And how to import that all excel sheets data into Access database...Please send me mail or reply to this..please urgent.....i will wait for ur reply...
|
|
|
|
 |
Hi Vivekananda,
Firstly, thanks for your sharing, it was so useful for me but i also need to import data from sql server to excel. Do you have any idea about it, if you have can you share it.
Thanks a lot,
Wendetta
|
|
|
|
 |
Hi,
with Visual Studio designer you can easily create ADO.NET typed DataTable from your SQL database table and import data to it.
After that you can easily import DataTable to Excel with this C# / VB.NET Excel component.
Here is a sample C# code:
ExcelFile ef = new ExcelFile();
DataTable dataTable = new DataTable();
dataTable.Columns.Add("FirstName", typeof(string));
dataTable.Columns.Add("LastName", typeof(string));
ef.LoadXls("FileName.xls");
ExcelWorksheet ws = ef.Worksheets[0];
ws.ExtractToDataTable(dataTable, 10, ExtractDataOptions.StopAtFirstEmptyRow, ws.Rows[0], ws.Columns[0]);
dataTable.Rows[0][0] = "Hello world!";
ws.InsertDataTable(dataTable, "A1", true);
ef.SaveXls("DataTable.xls")
|
|
|
|
 |
Hi Vivekananda,
While trying to run the website, I'm getting an error saying that "An attempt to attach an auto-named database for file E:\Dot NET Practice\WebSite3\App_Data\exceltosql.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
I'd created the database while in visual studio itself. Created a new table emp with the specified columns. Can you pl;ease tell me whats wrong ?
Thanks & regards,
Shiban
|
|
|
|
 |
Also, the downloaded code itself doesn't work. It gives an error
"CREATE DATABASE permission denied in database 'master'.
An attempt to attach an auto-named database for file C:\Shiban\Microsoft .NET 3.5\ImportExcel2SQLServer\ImportExcel2SQLServer\ImportExcel2SQLServer\App_Data\exceltosql.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
It will be great if somebody can give me the cause of this error.
|
|
|
|
 |
Hi vivekananda, i tried to import using dts from sql server, when i look sql server 2000 table, i found that value is unvalid, which can be changed into null...Please give me advice to fix it, using vb.net code...
Thanks in advance..
Regards,
|
|
|
|
 |
I have a quick question regarding my project. I have looked for the information on the web, but was not able to find relevant information.
We are getting Source data from two of our client in the form of Excel Sheet. These Excel sheets
Are generated using reporting services. On examining the excel sheet, I found out that the name Of the columns contain data itself, so the names are not static such as Jan 2007 Sales, Feb 2007 Sales etc etc.
And even the number of columns are not static. It depends upon the range of date selected by the user.
I wanted to know, if there is a way to import Excel sheet using Integration Services by defining the position Of column, instead of column name and I am not sure if there is a way for me to import excel with dynamic Number of columns.
Your help in this respect is highly appreciated!
Thanks,
Praveen Kumar.R
|
|
|
|
 |
Dear Vivekanand,
Ur article really helped me a lot.Thanks a ton for that.By the way if want to import excel that contains multiple sheets then how do i. Please help me n this regard dude.
|
|
|
|
 |
Dear Vivekananda,
I am Mahesh,My requriement is like this
I have to browse the excel sheets from local disk and on click of upload button,each excel sheet data should get inserted into the respective tables.
for eg:
every user uploads 4 excel sheets say
e1
e2
e3
e4
these excel sheets data should enter the corresponding tables in
sql server say
t1
t2
t3
t4
How should i proceed for this,kindly help me with this
|
|
|
|
 |
i want to know the meaning of child nodes not allowed
because when i run my web site ,this message appeared to me
|
|
|
|
 |
I also want some focous on the same...Vivekananda Please...
Hi this is Prashant.
|
|
|
|
 |
Please correct the formatting in order to save editors work. Use code blocks, avoid long lines.
|
|
|
|
 |
In the catch block you show an error message in red.
But in the finally block, the color is changed to green and a successful message is shown.
With the current code, an error message will NEVER show. The finally code should be moved above the catch block as the last statements in the try block.
|
|
|
|
 |
If you search on CP or Google you'll find many article on this topic
which will help you reading data from excel.
Be an Eagle, Sky is Yours.
|
|
|
|
 |
I have reading on this same problem for a few days now. It seems to me that you should pull all the data into a datatable and then pass the data as and XML string to a stored procedure using the XML datatype in SQL Server 2005. This allows you to pass all the rows at one time allowig for much better performance. During your insert statement you are opening a new connection and then closing it for each row you want to insert. These are expensive if you have a large data set that you need to insert. I am not an expert and dont even code for a living, so I could be way off base. This is how I got it to work. I need to insert 20K+ rows at a time so this is working great for me so far. http://stackoverflow.com/questions/424626/-net-datatable-to-sql-server-stored-procedure-as-xml
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
This article mainly describes how to import data from Excel sheet to SQL Server, Viewing the data from SQL Server using the Gridview Control, Deleting the Data. In case the Excel sheet does not contain data (null values), those values are replaced by zero.........
Type | Article |
Licence | CPOL |
First Posted | 16 Jan 2009 |
Views | 207,345 |
Downloads | 14,121 |
Bookmarked | 61 times |
|
|