Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NETC#4.0
Dear Sir
I want to upload a excel File and put excel file data in database though asp.net . 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:38

Comments
Anusha SR - 3 Nov '12 - 3:12
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 asp.net, u will get lot of sources

4 solutions

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
            workbook.LoadFromFile("DataTableSample.xls");
            //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#[^]
  Permalink  
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);
fs.Close();
fs.Dispose();
binaryReader.Close();
  }                    
 }
}   
 
 private void btnSubmit_Click(object sender, EventArgs e)
{            
Document objDoc = new Document();
objDoc.DocContent = Attachmnt;
objEntity.ResumeData_ = objDoc.DocContent;
objEntity.Submit();
}
  
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);
            Conn.Open();
            Cmd.Connection = Conn;
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.CommandText = "SP_Insert_Joinee";
            Cmd.ExecuteNonQuery();
        }
}
}
  Permalink  
Get data table from excel uploaded:
 public DataTable GetTable(string filename, string SheetName, string outTableName)
    {
        try
        {
            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);
            oleConn.Open();
            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);
            oleAdapter.Fill(dt);
            oleCmdSelect.Dispose();
            oleCmdSelect = null;
            oleAdapter.Dispose();
            oleAdapter = null;
            oleConn.Dispose();
            oleConn = null;
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
 
Then using bulk operation dataadapter insert it into sql table
  Permalink  
hello
plz study following code
check it[^]
 
may be this will help you
Thank You
Chetan V.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 218
1 Sergey Alexandrovich Kryukov 159
2 Santhosh G_ 155
3 Richard MacCutchan 145
4 Maciej Los 136
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,937
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,135


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid