Click here to Skip to main content
15,996,250 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to export as it is in excel to sql using c#,c#,c#.
need to use index lke A,B,C in top nd 1,2,3, in left side f excel fr export.
i could export by column mapping but it need 2 export as it is in excel
also tel me how 2 create tables in sql if we are exportng as it is in excel
Posted
Comments
OriginalGriff 9-Feb-12 3:08am    
Nope. Don't understand that.
Try English, and use the vowels on your keyboard.
Use the "Improve question" widget to edit your question and provide better information.
[no name] 9-Feb-12 3:48am    
It is English, sortof. The words, I understand them well, but what is he trying to tell us? :)
OriginalGriff 9-Feb-12 3:51am    
Ah! The eternal question!
Sharearries 22-Apr-16 5:24am    
There is a straightforward and easy way to do this with an excel library for C#.
Here is how:

using (SqlConnection conn = new SqlConnection("your connection"))
{
conn.Open();
using (SqlBulkCopy bc = new SqlBulkCopy(conn))
{
// Load an Excel file.
ExcelFile ef = ExcelFile.Load("Sample.xlsx");

// Select targeted worksheet.
ExcelWorksheet ws = ef.Worksheets[0];

// Create DataTable options.
var options = new CreateDataTableOptions();
// Set required options, for example:
options.ColumnHeaders = true;

// Create DataTable object.
DataTable dataTable = ws.CreateDataTable(options);

// Execute bulk copy.
bc.WriteToServer(dataTable);
}
}

In short you export an excel data into a DataTable object with C# and then execute an SqlBulkCopy. The only thing you need is to replace the "your connection" in above sample code with your SQL database connection.

1 solution

Nice reference is available at InSqlServer.com : http://insqlserver.com/comment/4[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900