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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

How to get Dynamically Linked Comboboxes Set?

2195


Coding for .NET Delegates?

2281


What is the code of Password Recovery or Forget your password? Plz tell in c # language.

3739


Code for Document Validation in XML.NET?

2145


Code for Using Keyboard Events?

2297


how to track links visited in google using iframes

2454


Code for Presenting Parent/Child Data in a Data Grid Row?

2281


how to create a search bar which access data from various websites and retrieves the data

2725


How to use Client-side Script to Focus Controls in ASP.NET?

2586


i have a gird with columns all are coming from database,this will bind in item templete in gridview as textboxex.and i have button below named Update.i want to update all the records in the grid,but if user change the value of one textbox,what is the easy way 2 do this

2314


Code for Communicating over Sockets?

2110


Code for Creating a Form Using PlaceHolder Controls?

2533


How to Bind Nested XML to a Repeater Control with Container.DataItem?

3377


ArrayList declaration in .net

2955


Coding for Synchronizing Cache Access in ASP.NET?

3319