111,097
社区成员




或者用pictureBox控件添加图片到excel,我这样写的但是excel表格里显示System.Drawing.Bitmap
xlWorksheet.Cells[10, "A"] =this.pictureBox1.InitialImage
//插入图片到Excel
public void InsertImage(Range xlRange, string srcFile)
{
if (!File.Exists(srcFile))
return;
xlRange.Select();
float left = (float)xlRange.Left + 0.25f;
float top = (float)xlRange.Top + 0.25f;
float width = (float)xlRange.Width - 0.25f;
float height = (float)xlRange.Height - 0.25f;
Shape xlShape = _xlWorkSheet.Shapes.AddPicture(srcFile, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, left, top, width, height);
xlShape.Width = width;
xlShape.Height = height;
xlShape.Placement = XlPlacement.xlMoveAndSize;
}
using Spire.Xls;
using System.Drawing;
namespace InsertImage_XLS
{
class Program
{
static void Main(string[] args)
{
//创建Workbook对象
Workbook workbook = new Workbook();
//获取第一张工作表
Worksheet sheet = workbook.Worksheets[0];
//指定列宽、行高
sheet.Columns[0].ColumnWidth = 50;
sheet.Rows[0].RowHeight = 160;
//加载图片,添加到指定单元格(第一行,第一列)
ExcelPicture picture = sheet.Pictures.Add(1,1, "tp.png");
//指定图片宽度和高度
picture.Width = 300;
picture.Height = 180;
//通过LeftColumnOffset和TopRowOffset属性值设置图片在单元格中的横向、纵向对齐
picture.LeftColumnOffset = 75;
picture.TopRowOffset = 20;
//保存文档
workbook.SaveToFile("result.xlsx", FileFormat.Version2013);
System.Diagnostics.Process.Start("result.xlsx");
}
}
}
这个是spire.xls.dll的方法,代码源自其他文章,这个可以插入到指定单元格。