protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
OleDbConnection con = new OleDbConnection ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:/Guna Sankari Documents/WebSite2/db1.accdb") ;
string Query2 = "select * from tblfiles" ;
OleDbDataAdapter da;
DataSet ds=new DataSet();
DataTable dtb;
con.Open() ;
da = new OleDbDataAdapter(Query2, con) ;
da.Fill(ds);
con.Close() ;
dtb = ds.Tables[0] ;
GridView1.DataSource = dtb ;
GridView1.DataBind();
}
}
protected string GetUrl(string imagepath)
{
string[] splits = Request.Url.AbsoluteUri.Split('/');
if (splits.Length >= 2)
{
string url = splits[0] + "//";
for (int i = 2; i < splits.Length - 1; i++)
{
url += splits[i];
url += "/";
}
return url + imagepath;
}
return imagepath;
}
private void Excel_Export()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
row.Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
string style = @" .textmode { mso-number-format:\@; } ";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
static DataTable GetData()
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(string));
table.Columns.Add("FileName", typeof(string));
//table.Columns.Add("Location", typeof(string));
table.Columns.Add("Image");
table.Rows.Add("JP", "XXX", "http://localhost/images/fir.png");
table.Rows.Add("HP", "TTT", "http://localhost/images/sec.png");
table.Rows.Add("SQ", "YYY", "http://localhost/images/thr.png");
table.Rows.Add("XS", "EEE", "http://localhost/images/fur.png");
return table;
}
protected void Button1_Click(object sender, EventArgs e)
{
Excel_Export();
}
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "False"
Font-Names = "Arial" >
<Columns>
<asp:BoundField DataField = "ID" HeaderText = "ID"
ItemStyle-Height = "150" />
<asp:BoundField DataField = "FileName" HeaderText = "Image Name"
ItemStyle-Height = "150" />
<asp:TemplateField ItemStyle-Height = "150" ItemStyle-Width = "170">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl = '<%# Eval("FilePath", GetUrl("{0}")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
ID1 ID FileName FilePath
1 1 Image images/fir.png
2 2 Img2 images/sec.png