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
How to get Dynamically Linked Comboboxes Set?
Coding for .NET Delegates?
What is the code of Password Recovery or Forget your password? Plz tell in c # language.
Code for Document Validation in XML.NET?
Code for Using Keyboard Events?
how to track links visited in google using iframes
Code for Presenting Parent/Child Data in a Data Grid Row?
how to create a search bar which access data from various websites and retrieves the data
How to use Client-side Script to Focus Controls in ASP.NET?
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
Code for Communicating over Sockets?
Code for Creating a Form Using PlaceHolder Controls?
How to Bind Nested XML to a Repeater Control with Container.DataItem?
ArrayList declaration in .net
Coding for Synchronizing Cache Access in ASP.NET?