插入一个图像Excel表格不工作
我试图插入一个图像像下面的Excel表格。
Image img = Image.FromFile("test.jpg"); sheet.get_Range("A1").set_Item(1,1,img);
但是当excel在A1单元格打开时没有任何图片,
但只有这个结果: System.Drawing.Bitmap
那么问题是怎么解决的。 而第二个问题是:
如果我的图片位于数据库中,如何插入excel。 我不想将它们保存到计算机插入到Excel中? [WinForm的]
Try this: object missing = System.Reflection.Missing.Value; Excel.Range picPosition = sheet.get_Range("A1"); // retrieve the range for picture insert Excel.Pictures p = yourWorksheet.Pictures(missing) as Excel.Pictures; Excel.Picture pic = null; pic = p.Insert(yourImageFilePath, missing); pic.Left = Convert.ToDouble(picRange.Left); pic.Top = picRange.Top; pic.Placement = // Can be any of Excel.XlPlacement.XYZ value
System.Data.SqlClient.SqlDataReader rdr = null; System.Data.SqlClient.SqlConnection conn = null; System.Data.SqlClient.SqlCommand selcmd = null; try { conn = new System.Data.SqlClient.SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString); //here set your query to get image from databse selcmd = new System.Data.SqlClient.SqlCommand ("select pic1 from msg where msgid=" + context.Request.QueryString["imgid"], conn); conn.Open(); rdr = selcmd.ExecuteReader(); while (rdr.Read()) { byte[] YourImagebytearray = ((byte[])rdr["pic1"]); MemoryStream stream = new MemoryStream(bytes); var newImage = System.Drawing.Image.FromStream(stream); stream.Dispose(); } if (rdr != null) rdr.Close(); } finally { if (conn != null) conn.Close(); }