how to upload an excel in c# ASP.Net?

Answers were Sorted based on User's Feedback



how to upload an excel in c# ASP.Net?..

Answer / kris

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 ?    7 Yes 0 No

how to upload an excel in c# ASP.Net?..

Answer / om shivaya namaha

How to upload Excel in Asp.Net
Click the Below link

http://www.codeproject.com/Articles/313637/Upload-Excel-Spreadsheet-File-to-Server-then-Displ

Is This Answer Correct ?    0 Yes 0 No

how to upload an excel in c# ASP.Net?..

Answer / sona

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 ?    2 Yes 4 No

Post New Answer

More ASP.NET Code Interview Questions

how can we close a web page in asp.net without using jscript?

4 Answers  


Give coding for Exception Handling Techniques in ASP.NET?

3 Answers   Microsoft,


How we implement the paypal in my website and how we make a payment through Credit Card.

2 Answers  


Code for Sending E-Mail with System.Web.Mail?

2 Answers  


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 Answers  






how to upload a photo? i need to use it in a matrimonial applicaton...

1 Answers  


Code for Document Validation in XML.NET?

0 Answers   TCS,


Data Reader Vs DataSet

5 Answers   TCS,


Coding for Synchronizing Cache Access in ASP.NET?

0 Answers  


How to Snap the Cursor to a Button?

1 Answers  


how to design a ListView control?

1 Answers  


How to integrate the regional language in asp.net and c# like Telugu, Hindi etc,. send a sample program

3 Answers   Concept, NIIT, Wipro,


Categories
  • ASP.NET Code Interview Questions ASP.NET Code (46)
  • VB.NET Code Interview Questions VB.NET Code (9)
  • C Sharp Code Interview Questions C Sharp Code (51)
  • ADO.NET Code Interview Questions ADO.NET Code (8)