Hi,
I have tried your code to export gridview data to word with images and it is working excellent.
My question is when I export images from gridview to word image default size export on word document. I want to export the image size which i have defined on aspx page. like height 150 px and width 150
My Aspx.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
Font-Names = "Arial" >
<Columns>
<asp:BoundField DataField = "ImageName" HeaderText = "Image Name" />
<asp:ImageField DataImageUrlField="ImagePath" ControlStyle-Width="100"
ControlStyle-Height = "100" HeaderText = "Preview Image"/>
<asp:TemplateField ItemStyle-Height = "150" ItemStyle-Width = "170">
<ItemTemplate>
d
<asp:Image ID="Image1" runat="server"
ImageUrl = '<%# Eval("ImagePath", GetUrl("{0}")) %>' Height="150" Width="150" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />
<asp:Button ID="btnExportWord" CommandArgument = "Word" runat="server" Text="Export-Word" OnClick = "Export_Grid" />
<asp:Button ID="btnExportExcel" CommandArgument = "Excel" runat="server" Text="Export-Excel" OnClick = "Export_Grid" />
</div>
</form>
</body>
</html>
page behind code
------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.
ConnectionStrings["VsmConnection"].ConnectionString;
string strQuery = "select * from Utility where ImageName ='urdu banner.png'";
SqlCommand cmd = new SqlCommand(strQuery);
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
protected void Export_Grid(object sender, EventArgs e)
{
Button btn = (Button)sender;
switch (btn.CommandArgument)
{
case "Word":
Word_Export();
break;
case "Excel":
Excel_Export();
break;
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
private void Word_Export()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
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];
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
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;
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Save files to disk
FileUpload1.SaveAs(Server.MapPath("UtilityImages/" + FileName));
//Add Entry to DataBase
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["VsmConnection"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "insert into Utility (Itemname,Purchasedprice, Retailprice,Discountedprice,Maincategory,Subcategory,ImageName, ImagePath, Status)" +
" values(@Itemname,@Purchasedprice, @Retailprice,@Discountedprice,@Maincategory,@Subcategory,@ImageName, @ImagePath, @Status)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@Itemname","Hello");
cmd.Parameters.AddWithValue("@Purchasedprice", 0);
cmd.Parameters.AddWithValue("@Retailprice", 0);
cmd.Parameters.AddWithValue("@Discountedprice", 0);
cmd.Parameters.AddWithValue("@MainCategory", "Hello");
cmd.Parameters.AddWithValue("@Subcategory", "Hello");
cmd.Parameters.AddWithValue("@Imagename", FileName);
cmd.Parameters.AddWithValue("@ImagePath", "UtilityImages/" + FileName);
cmd.Parameters.AddWithValue("@Status", "Active");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
}
}
}
}