<table> <tr> <td> <span style="color: Red">*</span>Attach Excel file </td> <td> <asp:FileUpload ID="fileuploadExcel" runat="server" /> </td> </tr> <tr> <td></td> <td> <asp:Button ID="btnSend" runat="server" Text="Export" onclick="btnSend_Click" /> </td> </tr> </table> <asp:GridView ID="GridView1" runat="server"> </asp:GridView>
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSend_Click(object sender, EventArgs e) { String strConnection = "Data Source=(local);Initial Catalog=MyDB;Integrated Security=True"; //file upload path string path = fileuploadExcel.PostedFile.FileName; //Create connection string to Excel work book string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False"; //Create Connection to Excel work book OleDbConnection excelConnection = new OleDbConnection(excelConnectionString); //Create OleDbCommand to fetch data from Excel OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]", excelConnection); excelConnection.Open(); OleDbDataReader dReader; dReader = cmd.ExecuteReader(); SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection); //Give your Destination table name sqlBulk.DestinationTableName = "Excel2TB"; sqlBulk.WriteToServer(dReader); excelConnection.Close(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)