Excel 组文本框和图片

本文关键字:文本 Excel | 更新日期: 2023-09-27 18:33:45

我找到了一些代码,可以使用SDK 2.0将图像添加到Excel工作表中。这部分工作正常。现在我想要图像下的文本框,但我不知道如何获得文本框。

我需要哪些类,什么是应用什么或哪个属性?

此外,如果它是分组的,那就太好了。这样当你拖动一个时,另一个就会跟随。

代码看起来像这样(我知道它有点多,但我可以削减更多(:

private void addImage(Offset offset, Extents extents, string sImagePath, string description)
    {
        WorksheetPart worksheetPart = this.arbeitsBlatt.WorksheetPart;
        DrawingsPart drawingsPart;
        ImagePart imagePart;
        XDrSp.WorksheetDrawing worksheetDrawing;
        ImagePartType imagePartType = getImageType(sImagePath);
        {
            // --- use the existing DrawingPart
            drawingsPart = worksheetPart.DrawingsPart;
            imagePart = drawingsPart.AddImagePart(imagePartType);
            drawingsPart.CreateRelationshipToPart(imagePart);
            worksheetDrawing = drawingsPart.WorksheetDrawing;
        }
        using (FileStream fileStream = new FileStream(sImagePath, FileMode.Open))
        {
            imagePart.FeedData(fileStream);
        }
        int imageNumber = drawingsPart.ImageParts.Count<ImagePart>();
        if (imageNumber == 1)
        {
            Drawing drawing = new Drawing();
            drawing.Id = drawingsPart.GetIdOfPart(imagePart);
            this.arbeitsBlatt.Append(drawing);
        }
        XDrSp.NonVisualDrawingProperties noVisualDrawingProps = new XDrSp.NonVisualDrawingProperties();
        XDrSp.NonVisualPictureDrawingProperties noVisualPictureDrawingProps = new XDrSp.NonVisualPictureDrawingProperties();
        noVisualDrawingProps.Id = new UInt32Value((uint)(1024 + imageNumber));
        noVisualDrawingProps.Name = "Picture " + imageNumber.ToString();
        noVisualDrawingProps.Description = beschreibung;
        PictureLocks picLocks = new PictureLocks();
        picLocks.NoChangeAspect = true;
        picLocks.NoChangeArrowheads = true;
        noVisualPictureDrawingProps.PictureLocks = picLocks;
        XDrSp.NonVisualPictureProperties noVisualPictureProps = new XDrSp.NonVisualPictureProperties();
        noVisualPictureProps.NonVisualDrawingProperties = noVisualDrawingProps;
        noVisualPictureProps.NonVisualPictureDrawingProperties = noVisualPictureDrawingProps;
        Stretch stretch = new Stretch();
        stretch.FillRectangle = new FillRectangle();
        XDrSp.BlipFill blipFill = new XDrSp.BlipFill();
        Blip blip = new Blip();
        blip.Embed = drawingsPart.GetIdOfPart(imagePart);
        blip.CompressionState = BlipCompressionValues.Print;
        blipFill.Blip = blip;
        blipFill.SourceRectangle = new SourceRectangle();
        blipFill.Append(stretch);
        Transform2D t2d = new Transform2D();
        t2d.Offset = offset;
        t2d.Extents = extents;
        XDrSp.ShapeProperties sp = new XDrSp.ShapeProperties();
        sp.BlackWhiteMode = BlackWhiteModeValues.Auto;
        sp.Transform2D = t2d;
        PresetGeometry prstGeom = new PresetGeometry();
        prstGeom.Preset = ShapeTypeValues.Rectangle;
        prstGeom.AdjustValueList = new AdjustValueList();
        sp.Append(prstGeom);
        sp.Append(new NoFill());
        XDrSp.Picture picture = new XDrSp.Picture();
        picture.NonVisualPictureProperties = noVisualPictureProps;
        picture.BlipFill = blipFill;
        picture.ShapeProperties = sp;
        XDrSp.OneCellAnchor anchor = this.getCellAnchor();
        XDrSp.Extent extent = new XDrSp.Extent();
        extent.Cx = extents.Cx;
        extent.Cy = extents.Cy;
        anchor.Extent = extent;
        anchor.Append(picture);
        anchor.Append(new XDrSp.ClientData());
        worksheetDrawing.Append(anchor);
        worksheetDrawing.Save(drawingsPart);
        #endregion
    }

Excel 组文本框和图片

我想你是OpenXml SDK的新手

首先,您需要使用最新版本的Open XMl SDK - 2.5版 [下载 - http://www.microsoft.com/en-us/download/details.aspx?id=30425]

在这里下载OpenXMLSDKV25.msi,OpenXMLSDKToolV25.msi.。同时安装两者。

现在这就是诀窍,OpenXML生产力工具是您需要的工具。它允许您浏览现有的Excel文件并将其分解为代码[在此处观看 - https://www.youtube.com/watch?v=KSSMLR19JWA]

现在您需要做的是使用您想要的内容手动创建一个 Excel 工作表 [在您的情况下,在图像下添加文本框] 然后使用生产力工具打开此 Excel 文件并了解代码。请注意,您需要了解电子表格文件结构才能理解此代码 [ 参考此 - https://www.google.com/#q=open+xml+sdk]。现在使用生产力工具的代码编写代码以满足您的要求

注意 - 一旦您使用生产力工具分析了虚拟电子表格,您就会明白为什么给出或指导 CODE 示例作为答案是不切实际的。
- 快乐编码-