vendredi 17 avril 2015

reading data from excel and show it in gridview is not working in live windows server


string exe = Path.GetExtension(FileUpload1.FileName);
if (exe == ".xlsx" || exe == ".xls")
{
var timer = System.Diagnostics.Stopwatch.StartNew();
try
{
// Open connection
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FilePath = Server.MapPath(FolderPath + FileName);
// String excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ FilePath +";Extended Properties=Excel 8.0;Persist Security Info=False";

FileUpload1.SaveAs(FilePath);
Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);

l2.Text = n1.ToString();
l11.Text = n2.ToString();
timer.Stop();
float s = timer.ElapsedMilliseconds;
float a11 = s / 1000;
l33.Text = a11.ToString() + " Sec";

}
catch
{
}
finally
{
File.Delete(Server.MapPath("~/File/") + filename);
}
}

private void Import_To_Grid(string FilePath, string Extension, string isHDR)
{
try
{
string connString = "";
switch (Extension)
{
case ".xls": //Excel 97-03
connString = ConfigurationManager.ConnectionStrings["Excel03ConString"]
.ConnectionString;
break;
case ".xlsx": //Excel 07
connString = ConfigurationManager.ConnectionStrings["Excel07ConString"]
.ConnectionString;
break;
}
connString = String.Format(connString, FilePath, rbHDR.SelectedItem.Text);
OleDbConnection connExcel = new OleDbConnection(connString);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
cmdExcel.Connection = connExcel;

connExcel.Open();
System.Data.DataTable dtExcelSchema;

dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();

//Read Data from First Sheet


connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();

//Bind Data to GridView


GridView1.Caption = Path.GetFileName(FilePath);
GridView1.DataSource = dt;
GridView1.DataBind();

int b = GridView1.Rows.Count;
l1.Text = b.ToString();
foreach (GridViewRow row in GridView1.Rows)
{
string str = row.Cells[0].Text;
Email(str);
if (u == 1)
{
row.Cells[0].ForeColor = System.Drawing.Color.Green;
n1++;

}
else
{
row.Cells[0].ForeColor = System.Drawing.Color.Red;
n2++;
}

}

}
catch (Exception)
{

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "File not found", true);
}

}


i used above code retrive data from excel sheet and show it in gridview .its working in localhost but not in live windows server. its not reading data from excel .so please suggest me to come out from this problem.


Aucun commentaire:

Enregistrer un commentaire