java excel 设置背景图,在Excel中将背景图像设置为单元格

Hi Developer,

I want to export data from Asp.net datagrid to excel.

the grid cell contains images(png image),

i cannot set the image in excel cell.

plz anyone help to me...

Thanks

解决方案The Shapes.AddPicture[^] method is used to insert a picture.

The Range[^] Top and Left properties are used to define the top left corner.

Width and Height should be in points instead of pixels. Use the 3/4 multiplier.

You may scale your pictures also.

Here is a short example in VBA:

Dim ws As Worksheet

Dim r As Range

Dim fileName As String

Dim pictureWidth As Integer

Dim pictureHeight As Integer

Dim shape As shape

Set ws = Worksheets(1)

Set r = ws.Cells(5, 2)

fileName = "d:\picture.png"

pictureWidth = 195

pictureHeight = 102

Set shape = ws.Shapes.AddPicture(fileName, msoTrue, msoTrue, r.Left, r.Top, pictureWidth * 3 / 4, pictureHeight * 3 / 4)

shape.LockAspectRatio = msoTrue

shape.ScaleHeight 2, msoTrue, msoScaleFromTopLeft

i added just the below code,

public override void VerifyRenderingInServerForm(Control control)

{

/* Confirms that an HtmlForm control is rendered for the specified ASP.NET

server control at run time. */

}

result came...

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();

}

}

'/>

ID1IDFileNameFilePath

11Imageimages/fir.png

22Img2images/sec.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值