Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

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

2535


how to track links visited in google using iframes

2697


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

2944


Code for Creating a Form Using PlaceHolder Controls?

2783


Code for Document Validation in XML.NET?

2334


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

2931


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

3602


How we use ajax in asp.net through javaScript. Please givee me an example.

2946


Code for Communicating over Sockets?

2310


Coding for .NET Delegates?

2499


ArrayList declaration in .net

3186


How to get Dynamically Linked Comboboxes Set?

2409


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

3930


Code for Using Keyboard Events?

2549


Common UI for Multiple web applications. Suppose there are 35 websites using same third party controls.These 3rd party controls are made together that all 35 websites can use these controls.If we put all 3rd party controls and use its dll in 35 websites,only class files will be accessable. But I want to use CSS,images also in all 35 websites. how I can design the N-tier solution for this project.

2664