Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i really blank and found no solution to my problem. i want to create a webpage which has a page that allows user to upload excel file to server.then i want to display the excel file so that other user can view or download the file.

i really hope anyone can help me on this.its really urgent as this is the last page to complete my project.the deadline is near.

thanks in advance.
musiw.
Posted
Updated 30-May-12 0:39am
v3

1. use the fileupload control to upload the file in a virtual directory of server.
2. Once the upload is done read the excel into a datatable using ADO.NET calls (details below)
3. Once you have datatable ready mark this as the datasource for your gridview.

To read the excel into datatable Try this (change excel path in connectionstring
C#
OleDbConnection con = null;
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\testExcel.xls;Extended Properties='Excel 8.0;HDR=YES;'";

con = new OleDbConnection(connectionString);
            con.Open();
 
            if (con.State == ConnectionState.Open)
            {
                OleDbCommand cmd = con.CreateCommand();
                cmd.CommandText = "Select * from [Sheet1$]";
                cmd.CommandType = CommandType.Text;
                
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                DataTable table = new DataTable();
 
                da.Fill(table);
            }

Once this is done you can use this datatable object to put the data anywhere be it UI or any DB table.
 
Share this answer
 
Comments
Sandeep Mewara 30-May-12 12:07pm    
5!
hai,
try this
first insert a file upload control
create a folder in root as "SomeFolderName" and save the excel file with time and filename
create a query to fetch the details from excel and save it in datatable "dt"

you can use the getExcelTable function default..jus u need to change the connection name


<pre lang="vb">
Dim filename As String = Path.GetFileName(FileUpload.FileName)

                filepath = "\SomeFolderName\" & Now.Date().ToString("Mdy") & "_" & filename

                FileUpload.SaveAs(Server.MapPath("~/") & filepath)

                strsql = "select   [Col1name]," & vbCrLf & _
"[Col2name]  from [Sheetname$]" & vbCrLf & _</pre>


dim dt as  Datatable= objdb.getExcelTable(strsql, Server.MapPath("~/") & filepath)


Public Function getExcelTable(ByVal sqlQry As String, ByVal strPath As String) As DataTable
       Try
           Dim ocmd As New OleDbCommand
           Dim oadp As OleDbDataAdapter

           Dim con1 As OleDb.OleDbConnection = New OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & _
                   "data source=" & strPath & ";Extended Properties=Excel 8.0;")
           con1 = New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & _
           "data source=" & strPath & ";Extended Properties=Excel 8.0;")
           Dim tb As New DataTable
           ocmd = New OleDbCommand(sqlQry, con1)
           oadp = New OleDbDataAdapter(ocmd)
           tb.Clear()
           tb.Rows.Clear()
           tb.Columns.Clear()
           oadp.Fill(tb)
           Return tb
       Catch ex As Exception
           Throw (ex)
       End Try
   End Function



for downloading the excel u can simple use hyperlink
 
Share this answer
 
v2
Comments
musiw 30-May-12 7:05am    
i want to upload a file not data in excel file.it is a report file actually.i want to store in server then user can download the report file.
Praveen2886 30-May-12 7:22am    
then u can simple save the file using
<pre lang="vb">
Dim filename As String = Path.GetFileName(FileUpload.FileName)

filepath = "\SomeFolderName\" & Now.Date().ToString("Mdy") & "_" & filename

FileUpload.SaveAs(Server.MapPath("~/") & filepath)

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