Hi,
we are doing the export to excel code in our project:
so like that u can use according such as:
protected void ExportExcel()
{
string items = "";
DataSet ds = new DataSet();
int[] indexes = this.lstCustomer.GetSelectedIndices();
if (indexes.Length > 0)
{
for (int index = 0; index < indexes.Length; index++)
{
items += this.lstCustomer.Items[indexes[index]].Value + ",";
}
}
else
{
items = string.Empty;
}
Int32 len;
len = items.Length;
if (items.Length > 0)
{
items = items.Substring(0, len - 1);
}
ds = obj.Get_Mail_Alerts(hdnUserId.Value.ToString(), items, string.Empty);
string[] allColumns = { "scorpclm_name", "scorpclm_StartDate", "scorpclm_ContractEffectiveDate", "scorpclm_ContractExpiryDate", "CustomerName", "username", "EmailType", "scorpclm_EmailSentOn", "scorpclm_Acknowledgement", "scorpclm_AcknowledgementDate", "scorpclm_completedStatus", "scorpclm_CompletedDate" };
string[] allCaptions = { "Contract Name", "Contract start date", "Contract effective date", "Contract expired date", "Customer Name", "Owner Name", "Context", "Email Sent On", "Acknowledgement", "Acknowledged Date", "Completed Status", "Completed Date" };
Response.AddHeader("Content-Disposition", "attachment; filename=MailAlert.xls");
Response.ContentType = "application/vnd.msexcel";
Response.Write("<TABLE BORDER='0' CELLPADDING='2' CELLSPACING='0' width='100%'>");
Response.Write("<tr>");
Response.Write("<h3><center>Mail Alerts</center></h3></td>");
Response.Write("</tr>");
Response.Write("</TABLE>");
Response.Write("<br />");
Response.Write("<TABLE BORDER='1' CELLPADDING='2' CELLSPACING='2' width='100%'>");
Response.Write("<tr>");
for (int i = 0; i < alCaptions.Length; i++)
{
Response.Write("<td width='20%'>");
Response.Write("<b>" + alCaptions[i] + "</b>");
Response.Write("</td>");
}
Response.Write("</tr>");
foreach (DataRow dr in ds.Tables[0].Rows)
{
Response.Write("<tr>");
for (int i = 0; i < alColumns.Length; i++)
{
Response.Write("<td>");
Response.Write(dr[alColumns[i]]);
Response.Write("</td>");
}
Response.Write("</tr>");
}
Response.Write("</TABLE>");
Response.End();
}
Regards,
Chitaranjan Mallick
+919894730854
mallickchitaranjan@gmail.com