Hint:-
When Excel Sheet Data Uploading and sheet data is not well formatted then Excel Sheet not read data properly due to mix data format
But this issue resolved just change excel connection string :
1) connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + @";Extended Properties=" + Convert.ToChar(34).ToString() + @"Excel 12.0;Imex=1;HDR=Yes;" + Convert.ToChar(34).ToString();
2) connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilePath + @";Extended Properties=" + Convert.ToChar(34).ToString() + @"Excel 8.0;Imex=1;HDR=Yes;" + Convert.ToChar(34).ToString();
some time numeric data then varchar data insert into database by excel that time geting problam
--------------------------------------------------------------
using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Crypto;
using System.Text;
public partial class Admin_reports : System.Web.UI.Page
{
public SqlConnection Connection;
public Admin_reports()
{
Crypto.Crypto crp = new Crypto.Crypto(Crypto.Crypto.Providers.RC2);
string SqlConnectinString = (ConfigurationManager.ConnectionStrings["ConnStr_Leads"].ConnectionString);
SqlConnectinString = crp.Decrypt(SqlConnectinString);
Connection = new SqlConnection(SqlConnectinString);
}
public SqlCommand cmd;
public DataSet ds;
public int i;
/// <summary>
/// Generate excel file to download data.
/// </summary>
private void WriteOutputToExcel()
{
try
{
lblDataMessage.Text = "";
Connection.Open();
SqlDataAdapter da = new SqlDataAdapter();
ds = new DataSet();
cmd = new SqlCommand("spGetCustomerEmailReport", Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fromdate", txtFromDate.Text);
cmd.Parameters.AddWithValue("@todate", txtToDate.Text);
da.SelectCommand = cmd;
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
StringBuilder str = new StringBuilder();
str.Append("<table width='100%' border='1'>");
str.Append("<tr>");
str.Append("<td colspan='12' align='center'>");
str.Append(" Email ID confirmation and update report");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td colspan='12' align='center'>");
str.Append(" ");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td colspan='12' align='center'>");
str.Append("From Date: " + txtFromDate.Text + " To Date: " + txtToDate.Text);
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td colspan='12' align='center'>");
str.Append(" ");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr >");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;'>Sr.No.</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Customer Name</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Customer Id</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Mobile No</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >E-Mail Id</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Verification Code</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Verify Status</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Entry Date/Time</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Verify Date/Time</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >No. of Attempt</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Validity</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Campaign Id</td>");
str.Append("</tr>");
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
str.Append("<tr >");
str.Append("<td >" + ds.Tables[0].Rows[i]["Sr.No."].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Customer Name"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Customer Id"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Mobile No"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["E-Mail Id"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Verification Code"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Verify Status"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Entry Date/Time"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Verify Date/Time"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["No. of Attempt"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Validity"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Campaign Id"].ToString() + "</td>");
str.Append("</tr>");
}
str.Append("</table>");
//DataGrid dggrid = new DataGrid();
//dggrid.DataSource = ds;
//dggrid.DataBind();
//StringWriter tw = new StringWriter();
//HtmlTextWriter hw = new HtmlTextWriter(tw);
//dggrid.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
//dggrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "attachment; filename=Customer_Email_Report.xls");
Response.Write(str.ToString());
Response.End();
}
else
{
lblDataMessage.Text = "No data found.";
}
Connection.Close();
}
catch (Exception ex)
{
}
}
}
When Excel Sheet Data Uploading and sheet data is not well formatted then Excel Sheet not read data properly due to mix data format
But this issue resolved just change excel connection string :
1) connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + @";Extended Properties=" + Convert.ToChar(34).ToString() + @"Excel 12.0;Imex=1;HDR=Yes;" + Convert.ToChar(34).ToString();
2) connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilePath + @";Extended Properties=" + Convert.ToChar(34).ToString() + @"Excel 8.0;Imex=1;HDR=Yes;" + Convert.ToChar(34).ToString();
some time numeric data then varchar data insert into database by excel that time geting problam
--------------------------------------------------------------
using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Crypto;
using System.Text;
public partial class Admin_reports : System.Web.UI.Page
{
public SqlConnection Connection;
public Admin_reports()
{
Crypto.Crypto crp = new Crypto.Crypto(Crypto.Crypto.Providers.RC2);
string SqlConnectinString = (ConfigurationManager.ConnectionStrings["ConnStr_Leads"].ConnectionString);
SqlConnectinString = crp.Decrypt(SqlConnectinString);
Connection = new SqlConnection(SqlConnectinString);
}
public SqlCommand cmd;
public DataSet ds;
public int i;
/// <summary>
/// Generate excel file to download data.
/// </summary>
private void WriteOutputToExcel()
{
try
{
lblDataMessage.Text = "";
Connection.Open();
SqlDataAdapter da = new SqlDataAdapter();
ds = new DataSet();
cmd = new SqlCommand("spGetCustomerEmailReport", Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fromdate", txtFromDate.Text);
cmd.Parameters.AddWithValue("@todate", txtToDate.Text);
da.SelectCommand = cmd;
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
StringBuilder str = new StringBuilder();
str.Append("<table width='100%' border='1'>");
str.Append("<tr>");
str.Append("<td colspan='12' align='center'>");
str.Append(" Email ID confirmation and update report");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td colspan='12' align='center'>");
str.Append(" ");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td colspan='12' align='center'>");
str.Append("From Date: " + txtFromDate.Text + " To Date: " + txtToDate.Text);
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr>");
str.Append("<td colspan='12' align='center'>");
str.Append(" ");
str.Append("</td>");
str.Append("</tr>");
str.Append("<tr >");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;'>Sr.No.</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Customer Name</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Customer Id</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Mobile No</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >E-Mail Id</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Verification Code</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Verify Status</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Entry Date/Time</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Verify Date/Time</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >No. of Attempt</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Validity</td>");
str.Append("<td style='font-weight:bold;background-color:#BDBDBD;' >Campaign Id</td>");
str.Append("</tr>");
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
str.Append("<tr >");
str.Append("<td >" + ds.Tables[0].Rows[i]["Sr.No."].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Customer Name"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Customer Id"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Mobile No"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["E-Mail Id"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Verification Code"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Verify Status"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Entry Date/Time"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Verify Date/Time"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["No. of Attempt"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Validity"].ToString() + "</td>");
str.Append("<td >" + ds.Tables[0].Rows[i]["Campaign Id"].ToString() + "</td>");
str.Append("</tr>");
}
str.Append("</table>");
//DataGrid dggrid = new DataGrid();
//dggrid.DataSource = ds;
//dggrid.DataBind();
//StringWriter tw = new StringWriter();
//HtmlTextWriter hw = new HtmlTextWriter(tw);
//dggrid.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
//dggrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "attachment; filename=Customer_Email_Report.xls");
Response.Write(str.ToString());
Response.End();
}
else
{
lblDataMessage.Text = "No data found.";
}
Connection.Close();
}
catch (Exception ex)
{
}
}
}
No comments:
Post a Comment