Click here to Skip to main content
12,953,937 members (42,927 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Dear Sir
I want to upload a excel File and put excel file data in database though . my data base is SQL server 2005 .please help me .
I have Search in Google but I can't got any answer.
Posted 2-Nov-12 20:38pm
Anusha SR 3-Nov-12 3:12am
its not a matter of excel files ,u can upload any files and store it in database , refine your sarch in google as upload and save files using, u will get lot of sources
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you need to upload your excel file to datatabase, the first step is to export excel to datatable and then, connect your datatable to sql server database see below code:
//Create a workbook
Workbook workbook = new Workbook();
//Load the file
//Initailize worksheet
Worksheet sheet = workbook.Worksheets[0];
//Export datatable
DataTable dataTable = sheet.ExportDataTable();

Then, you can connect your datatable to sql server.If you still not know very clear, you can see a similar question here:
Export excel to database through c#[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Diagnostics;
Boolean IsClick = true;
byte[] Attachmnt;
ClassLibrary1.Entity objEntity = new ClassLibrary1.Entity();
Query objQuery = new Query();
string Connstr = System.Configuration.ConfigurationManager.AppSettings["Connstr"].ToString();
private void btnBrowse_Click(object sender, EventArgs e)
OpenFileDialog openFileDialog1 = new OpenFileDialog();                openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Select ur file ";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "doc";
openFileDialog1.Filter = "Excel file(*.xls)|*.xls";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
tbUpload.Text = openFileDialog1.SafeFileName;
FilePath = openFileDialog1.FileName;
if (openFileDialog1.FileName != null)   
v_Filepath = FilePath;
System.IO.FileStream fs = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
long byteLength = new System.IO.FileInfo(FilePath).Length;
Attachmnt = binaryReader.ReadBytes((Int32)byteLength);
 private void btnSubmit_Click(object sender, EventArgs e)
Document objDoc = new Document();
objDoc.DocContent = Attachmnt;
objEntity.ResumeData_ = objDoc.DocContent;
public class Document   
 public int DocId { get; set; }
 public string DocName { get; set; }
 public byte[] DocContent { get; set; }

namespace ClassLibrary1
    public class Entity
        private byte[] ResumeData;
        public byte[] ResumeData_
            get { return ResumeData; }
            set { ResumeData = value; }
 string Connstr=System.Configuration.ConfigurationManager.AppSettings["Connstr"].ToString();
        public void Submit()
            SqlCommand Cmd = new SqlCommand();
            SqlConnection Conn = new SqlConnection(Connstr);
            Cmd.Connection = Conn;
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.CommandText = "SP_Insert_Joinee";
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Get data table from excel uploaded:
public DataTable GetTable(string filename, string SheetName, string outTableName)
           string Con = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                       @"Data Source=" + filename + ";" +
                       @"Extended Properties=" + Convert.ToChar(34).ToString() +
                       @"Excel 8.0;" + "Imex=2;" + "HDR=Yes;" + Convert.ToChar(34).ToString();
           OleDbConnection oleConn = new OleDbConnection(Con);
           OleDbCommand oleCmdSelect = new OleDbCommand();
           oleCmdSelect = new OleDbCommand(
                   @"SELECT * FROM ["
                   + SheetName
                   + "$" + "]", oleConn);
           OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
           oleAdapter.SelectCommand = oleCmdSelect;
           DataTable dt = new DataTable(outTableName);
           oleAdapter.FillSchema(dt, SchemaType.Source);
           oleCmdSelect = null;
           oleAdapter = null;
           oleConn = null;
           return dt;
       catch (Exception ex)
           throw ex;

Then using bulk operation dataadapter insert it into sql table

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,464
CHill60 3,490
Maciej Los 3,123
ppolymorphe 2,020
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100