Image ClosedXML添加图像
我能够使用OpenXML将图像添加到excel电子表格中。 但是,对于程序的其余部分,我使用ClosedXML添加数据。 我可以使用列和行索引在特定单元格中添加数据。 如果我可以将图像添加到excel中(它目前似乎是一个单独的层,悬停在单元格上),如何使用ClosedXML将其添加到单元格中Image ClosedXML添加图像,image,cell,add,openxml,closedxml,Image,Cell,Add,Openxml,Closedxml,我能够使用OpenXML将图像添加到excel电子表格中。 但是,对于程序的其余部分,我使用ClosedXML添加数据。 我可以使用列和行索引在特定单元格中添加数据。 如果我可以将图像添加到excel中(它目前似乎是一个单独的层,悬停在单元格上),如何使用ClosedXML将其添加到单元格中 //Adds an image to the excel file public void AddImageToExcel(SpreadsheetDocument sd, MemoryStr
//Adds an image to the excel file
public void AddImageToExcel(SpreadsheetDocument sd, MemoryStream imagestream)
{
DrawingsPart dp = sd.WorkbookPart.WorksheetParts.First().AddNewPart<DrawingsPart>();
ImagePart imgp = dp.AddImagePart(ImagePartType.Jpeg, sd.WorkbookPart.WorksheetParts.First().GetIdOfPart(dp));
MemoryStream bmstream = new MemoryStream(imagestream.ToArray());
bmstream.Seek(0, SeekOrigin.Begin);
MemoryStream fs;
using (fs = imagestream)
{
fs.Position = 0;
imgp.FeedData(fs);
}
DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualDrawingProperties nvdp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualDrawingProperties();
nvdp.Id = 1025;
nvdp.Name = "Chart Image";
nvdp.Description = "Image";
DocumentFormat.OpenXml.Drawing.PictureLocks piclocks = new DocumentFormat.OpenXml.Drawing.PictureLocks();
piclocks.NoChangeAspect = true;
piclocks.NoChangeArrowheads = true;
DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureDrawingProperties nvpdp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureDrawingProperties();
nvpdp.PictureLocks = piclocks;
DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureProperties nvpp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureProperties();
nvpp.NonVisualDrawingProperties = nvdp;
nvpp.NonVisualPictureDrawingProperties = nvpdp;
DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
stretch.FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle();
DocumentFormat.OpenXml.Drawing.Spreadsheet.BlipFill blipfill = new DocumentFormat.OpenXml.Drawing.Spreadsheet.BlipFill();
DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip();
blip.Embed = dp.GetIdOfPart(imgp);
blip.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print;
blipfill.Blip = blip;
blipfill.SourceRectangle = new DocumentFormat.OpenXml.Drawing.SourceRectangle();
blipfill.Append(stretch);
DocumentFormat.OpenXml.Drawing.Transform2D t2d = new DocumentFormat.OpenXml.Drawing.Transform2D();
DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset();
offset.X = 0;
offset.Y = 0;
t2d.Offset = offset;
Bitmap bm = new Bitmap(bmstream);
DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents();
extents.Cx = ((long)bm.Width * (long)((float)914400 / bm.HorizontalResolution));
extents.Cy = ((long)bm.Height * (long)((float)914400 / bm.VerticalResolution));
bm.Dispose();
t2d.Extents = extents;
DocumentFormat.OpenXml.Drawing.Spreadsheet.ShapeProperties sp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.ShapeProperties();
sp.BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto;
sp.Transform2D = t2d;
DocumentFormat.OpenXml.Drawing.PresetGeometry prstgeom = new DocumentFormat.OpenXml.Drawing.PresetGeometry();
prstgeom.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle;
prstgeom.AdjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList();
sp.Append(prstgeom);
sp.Append(new DocumentFormat.OpenXml.Drawing.NoFill());
DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture();
picture.NonVisualPictureProperties = nvpp;
picture.BlipFill = blipfill;
picture.ShapeProperties = sp;
DocumentFormat.OpenXml.Drawing.Spreadsheet.Position pos = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Position();
//The position corrosponds these numbers. X= 600000 & y = 200000 adds up to 1 cell
pos.X = 600000;
pos.Y = 200000;
Extent ext = new Extent();
ext.Cx = extents.Cx;
ext.Cy = extents.Cy;
AbsoluteAnchor anchor = new AbsoluteAnchor();
Xdr.Position pp = new Xdr.Position();
pp.X = 0;
pp.Y = 0;
anchor.Position = pp;
anchor.Position = pos;
anchor.Extent = ext;
anchor.Append(picture);
anchor.Append(new ClientData());
WorksheetDrawing wsd = new WorksheetDrawing();
wsd.Append(anchor);
Drawing drawing = new Drawing();
drawing.Id = dp.GetIdOfPart(imgp);
wsd.Save(dp);
sd.WorkbookPart.WorksheetParts.First().Worksheet.Append(drawing);
MessageBox.Show("Excel File created");
}
谢谢你的帮助 我来晚了一点,但万一有人来找我,我已经为ClosedXML添加了(有限的)图像支持 叉子可以在 编辑:为其工作方式添加一些细节 新增了XLPicture和XLMarker类 图片可以通过以下方式创建:
XLPicture pic = new XLPicture
{
NoChangeAspect = true,
NoMove = true,
NoResize = true,
ImageStream = fIn,
Name = "Test Image"
};
到目前为止,它只接受流,不接受文件,并且只输出JPEG。如果有需求,这可以改变
一旦你的图片被创建,你就为它的去向创建一个标记
XLMarker fMark = new XLMarker
{
ColumnId = 2,
RowId = 2
};
pic.AddMarker(fMark);
如果添加单个标记,它将直接将图像嵌入单元格中。如果添加2个标记,它将跨越图像的两个标记
然后将其添加到工作表中
worksheet.AddPicture(pic);
大家好。ClosedXML现在有了基本的图像/图片支持。根据:
此功能已包含在0.89.0版的封闭XML中 此代码适用于MVC5,但您也可以在其他地方使用它
public ActionResult Contact()
{
//Creating excelsheet and adding workbook with name Picture
XLWorkbook workbook = new XLWorkbook();
IXLWorksheet worksheet = workbook.Worksheets.Add("Picture");
//Adjusting size of the excel cell as per requirement
worksheet.Column(3).Width = 60;
worksheet.Row(3).Height = 50;
var imagePath = @"~/Images/Picture2.png";
//Adding image to the worksheet and moving it to the cell
var image = worksheet.AddPicture(Server.MapPath(imagePath))
.MoveTo(worksheet.Cell(3, 3).Address);
image.Name = "Logo";
//Scaling down image as per our cell size
image.ScaleWidth(.5);
image.ScaleHeight(.3);
//Formating the cell with border and color
worksheet.Cell(3, 3).Style.Border.OutsideBorder = XLBorderStyleValues.Thick;
worksheet.Cell(3, 3).Style.Border.OutsideBorderColor = XLColor.Blue;
//asving worksheet in memory stream
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
stream.Position = 0;
//returning the final excelsheet with name Picture
return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{ FileDownloadName = "Pictures.xlsx" };
}
有关详细说明,请参阅链接。希望它能帮助别人
叉杆不工作。你能分享你创建的DLL文件吗?谢谢您的努力。当然,我使用了您的dll和以下代码,插入图像后无法打开excel文件。它已损坏:
FileStream fs=newfilestream(“c:\\tempdir\\sign3.png”,FileMode.Open);MemoryStream memo=新的MemoryStream();财政司司长(备忘录),;memo.Position=0;XLPicture pic=newxlpicture{…ImageStream=memo,…};XLMarker fMark=newxlmarker{ColumnId=1,RowId=1};图.添加标记(fMark);ws.AddPicture(pic)代码>所以我知道发生了什么。我认为这是在工作表中添加了一个重复的绘图部分。我将看看是否可以更正此问题,重新编译DLL,并在有时间时再次发布。请注意,ClosedXML现在支持添加图像,而无需@Ajwhiteway的fork。ClosedXML API在Thank you sir中有文档记录。上述代码中是否有错误,imagePath和ImageLocation是否应该是一个且相同的?此外,是否可以指定实际图像文件内容,例如ASP标记中的位置,例如“data:image/PNG,…”@Zeek2请查看
using (var wb = new XLWorkbook())
{
var ws = wb.AddWorksheet("Sheet1");
var imagePath = @"c:\path\to\your\image.jpg";
var image = ws.AddPicture(imagePath)
.MoveTo(ws.Cell("B3").Address)
.Scale(.5); // optional: resize picture
wb.SaveAs("file.xlsx");
}
public ActionResult Contact()
{
//Creating excelsheet and adding workbook with name Picture
XLWorkbook workbook = new XLWorkbook();
IXLWorksheet worksheet = workbook.Worksheets.Add("Picture");
//Adjusting size of the excel cell as per requirement
worksheet.Column(3).Width = 60;
worksheet.Row(3).Height = 50;
var imagePath = @"~/Images/Picture2.png";
//Adding image to the worksheet and moving it to the cell
var image = worksheet.AddPicture(Server.MapPath(imagePath))
.MoveTo(worksheet.Cell(3, 3).Address);
image.Name = "Logo";
//Scaling down image as per our cell size
image.ScaleWidth(.5);
image.ScaleHeight(.3);
//Formating the cell with border and color
worksheet.Cell(3, 3).Style.Border.OutsideBorder = XLBorderStyleValues.Thick;
worksheet.Cell(3, 3).Style.Border.OutsideBorderColor = XLColor.Blue;
//asving worksheet in memory stream
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
stream.Position = 0;
//returning the final excelsheet with name Picture
return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{ FileDownloadName = "Pictures.xlsx" };
}