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

你可能感兴趣的:(java,excel,设置背景图)