Hello friends
I m facing a very Big problem right now actually I have transaction form which is bind by data grid view.and all the columns is validated in data grid view now when I m export it all the validation is not coming in excel for example how to define range in cell or columns in excel ...
If user enter 10 marks from 10 then grade should be come A in next cell so I don't have any idea how to make it in excel file.... Hope u r understand what I want ...
Thanks in advance
Loading

Anshul JainPosted Jul 15, 2015, 4:12 AM
Add this straight forwad funtion used to help convert datatable data to excel
public bool exporttoexcel(DataTable dt, string path)
With Thanks
----------------
web design in agra
Upendra Pratap ShahiPosted Jul 6, 2015, 4:32 AM
Naeem KhanPosted Jul 6, 2015, 4:19 AM
Upendra Pratap ShahiPosted Jul 6, 2015, 1:15 AM
Nilesh JadavPosted Jul 6, 2015, 12:51 AM
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>title>
head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="GridView1"runat="server"AllowPaging="True"
AutoGenerateColumns="False"BackColor="White"BorderColor="#999999"
BorderStyle="None"BorderWidth="1px"CellPadding="3"DataKeyNames="id"
GridLines="Vertical"onpageindexchanging="GridView1_PageIndexChanging1"
PageSize="5">
<AlternatingRowStyleBackColor="#DCDCDC"/>
<Columns>
<asp:TemplateFieldHeaderText="Name">
<EditItemTemplate>
<asp:TextBoxID="TextBox1"runat="server"Text='<%# Bind("name") %>'>asp:TextBox>
EditItemTemplate>
<ItemTemplate>
<asp:LabelID="Label1"runat="server"Text='<%# Bind("name") %>'>asp:Label>
ItemTemplate>
asp:TemplateField>
<asp:TemplateFieldHeaderText="Education">
<EditItemTemplate>
<asp:TextBoxID="TextBox2"runat="server"Text='<%# Bind("education") %>'>asp:TextBox>
EditItemTemplate>
<ItemTemplate>
<asp:LabelID="Label2"runat="server"Text='<%# Bind("education") %>'>asp:Label>
ItemTemplate>
asp:TemplateField>
<asp:TemplateFieldHeaderText="Email">
<EditItemTemplate>
<asp:TextBoxID="TextBox3"runat="server"Text='<%# Bind("email") %>'>asp:TextBox>
EditItemTemplate>
<ItemTemplate>
<asp:LabelID="Label3"runat="server"Text='<%# Bind("email") %>'>asp:Label>
ItemTemplate>
asp:TemplateField>
<asp:TemplateFieldHeaderText="Location">
<EditItemTemplate>
<asp:TextBoxID="TextBox4"runat="server"Text='<%# Bind("location") %>'>asp:TextBox>
EditItemTemplate>
<ItemTemplate>
<asp:LabelID="Label4"runat="server"Text='<%# Bind("location") %>'>asp:Label>
ItemTemplate>
asp:TemplateField>
Columns>
<FooterStyleBackColor="#CCCCCC"ForeColor="Black"/>
<HeaderStyleBackColor="#000084"Font-Bold="True"ForeColor="White"/>
<PagerStyleBackColor="#999999"ForeColor="Black"HorizontalAlign="Center"/>
<RowStyleBackColor="#EEEEEE"ForeColor="Black"/>
<SelectedRowStyleBackColor="#008A8C"Font-Bold="True"ForeColor="White"/>
<SortedAscendingCellStyleBackColor="#F1F1F1"/>
<SortedAscendingHeaderStyleBackColor="#0000A9"/>
<SortedDescendingCellStyleBackColor="#CAC9C9"/>
<SortedDescendingHeaderStyleBackColor="#000065"/>
asp:GridView>
<br/>
<br/>
div>
<asp:ButtonID="Button1"runat="server"onclick="Button1_Click1"
Text="Export To PDF"/>
form>
body>
html>
Your design.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
publicpartialclass_Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
refreshdata();
}
}
// Bind the gridview here
publicvoid refreshdata()
{
SqlConnection con = newSqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = newSqlCommand("select * from tbl_data", con);
SqlDataAdapter sda = newSqlDataAdapter(cmd);
DataTable dt = newDataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
// Gridview Paging code here
protectedvoid GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
refreshdata();
}
// Exporting Gridview to pdf code here
protectedvoid Button1_Click1(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter swr = newStringWriter();
HtmlTextWriter htmlwr = newHtmlTextWriter(swr);
GridView1.AllowPaging = false;
refreshdata();
GridView1.RenderControl(htmlwr);
Response.Output.Write(swr.ToString());
Response.Flush();
Response.End();
}
publicoverridevoid VerifyRenderingInServerForm(Control control)
{
}
}