ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  Dot Net Code  >>  ASP.NET Code
 
 


 

 
 ASP.NET Code interview questions  ASP.NET Code Interview Questions
 VB.NET Code interview questions  VB.NET Code Interview Questions
 C Sharp Code interview questions  C Sharp Code Interview Questions
 ADO.NET Code interview questions  ADO.NET Code Interview Questions
Question
how to upload an excel in c# ASP.Net?
 Question Submitted By :: Kris
I also faced this Question!!     Rank Answer Posted By  
 
  Re: how to upload an excel in c# ASP.Net?
Answer
# 1
protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fuUpload.HasFile)
        {
            string filename = fuUpload.PostedFile.FileName;
           
            string strConn;
            strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
+"Data Source="+ filename +";" +"Extended Properties=Excel
8.0;";
            OleDbConnection con = new OleDbConnection(strConn);
            con.Open();
            OleDbCommand cmd = new OleDbCommand("select *
from [Sheet1$]",con);
            OleDbDataAdapter dad = new OleDbDataAdapter();
            dad.SelectCommand = cmd;
            DataSet ds= new DataSet();
            dad.Fill(ds);
            DataGrid1.DataSource = ds.Tables[0].DefaultView;
            DataGrid1.DataBind();
            con.Close();
        }

    }
 
Is This Answer Correct ?    2 Yes 0 No
Kris
 
  Re: how to upload an excel in c# ASP.Net?
Answer
# 2
var uploadPath = Server.MapPath("xx/fdd") + @"\" + 
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                .
                if (flupload.PostedFile.FileName.Length > 0)
                {
                    if (flupload.PostedFile.ContentType !
= "application/octet-stream") //  text/plain
                    {
                        ShowMessage("' Error'");
                        return;
                    }
                    if (GetFileType
(flupload.PostedFile.FileName) != ".xls")
                    {
                           ShowMessage("'Error'");
                        return;
                    } 
                    //Upload the selected diary file for 
furhter processing.
                    varFileName = Path.GetFileName
(flupload.PostedFile.FileName);
                    varFileName = uploadPath + @"\" + 
varFileName;
                    flupload.SaveAs(varFileName);






  //Get File type - only csv file is considered here..
        private string GetFileType(string fileName)
        {
            return fileName.Substring(fileName.LastIndexOf
("."), fileName.Length - fileName.LastIndexOf("."));
        }





 public DataTable ReadExcelIntoDataTable(string filename, 
string SheetName)
        {
            string connectionString 
= "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                      "Data Source=" + 
filename + ";" +
                                      "Extended 
Properties=Excel 8.0;";

            OleDbConnection objConn = new OleDbConnection
(connectionString);
            objConn.Open();
            OleDbCommand ObjCommand = new OleDbCommand
("SELECT * FROM [" + SheetName + "$]", objConn);
            OleDbDataAdapter objAdp = new OleDbDataAdapter
();
            objAdp.SelectCommand = ObjCommand;
            DataSet excelSheetDataSet = new DataSet();
            objAdp.Fill(excelSheetDataSet);
            DataTable excelSheetTable = 
excelSheetDataSet.Tables[0];
            objConn.Close();
            return excelSheetTable;

        }



 private string ReadImportedDataFromFile(string uploadPath)
        {
            string[] StrSheetNames = GetExcelSheetNames
(uploadPath);
            DataTable excelSheetTable = 
ReadExcelIntoDataTable(uploadPath, StrSheetNames[5]);
            StringBuilder sbExcel= new StringBuilder();
            int rowCount = 0, colCount = 0;             
            sbExcel.AppendFormat("<{0}>", "home");  
            foreach (DataRow dr in excelSheetTable.Rows)
            {
                if (rowCount >= 1)
                {
                    sbExcel.AppendFormat("<{0}>", "Table");
                    foreach (DataColumn dc in 
excelSheetTable.Columns)
                    {
                                             
                        if (dr[dc].ToString().Trim
().Length != 0)
                        {
                            if (colCount > 1)
                            {
                                sbExcel.AppendFormat
("<EmpCode>{0}</EmpCode>", dr[0].ToString().Trim());  
                                sbExcel.AppendFormat
("<SalHeadId>{0}</SalHeadId>", dc.ColumnName.Trim());
                                //sbExcel.AppendFormat("<" 
+ dc.ColumnName.Trim() + ">{0}</" + dc.ColumnName.Trim() 
+ ">", dr[dc].ToString());
                                sbExcel.AppendFormat("<Rate>
{0}</Rate>", dr[dc].ToString());                         
                            }
                            //else
                            //{
                            //    //string strFormat = 
(colCount == 0 ? "<EmpCode>{0}</EmpCode>" : "<EmpName>{0}
</EmpName>");
                            //    //sbExcel.AppendFormat
(strFormat,dr[dc].ToString().Trim());
                            //}
                            colCount++;
                        }                    
                    }
                    sbExcel.AppendFormat("</{0}>", "Table");
                    colCount = 0;
                }
                rowCount++;
            }           
            sbExcel.AppendFormat("</{0}>", "home");



        }
 
Is This Answer Correct ?    1 Yes 0 No
Sona
 
 
 

 
 
 
Other ASP.NET Code Interview Questions
 
  Question Asked @ Answers
 
Data Reader Vs DataSet TCS1
how can we close a web page in asp.net without using jscript?  1
Code for Sending E-Mail with System.Web.Mail?  1
how to design a ListView control?  1
How to get the row index on checking a Checkbox in a ListView  1
working with fileUpload ?  1
How to Create Scrollable Micro Windows?  2
how to upload a photo? i need to use it in a matrimonial applicaton...  1
Give coding for Exception Handling Techniques in ASP.NET? Microsoft3
How we implement the paypal in my website and how we make a payment through Credit Card.  2
How to send e-mail from an ASP.NET application? TCS10
hold checkbox values  4
How to add checkbox to datagrid?  4
How to Export Data to Excel? Eastcom-Systems3
Code for a Simple Way to Write XML in .NET (XmlTextWriter)  1
how to convert Dataset to Object Array or list in c# .net  3
what is the value that reside in a hidden field when no value is assigned to it?how it can be used in a if statement??  1
Code for Getting Information About A File?  1
What do you create for easier access of data? NIIT2
How to integrate the regional language in asp.net and c# like Telugu, Hindi etc,. send a sample program NIIT2
 
For more ASP.NET Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com