Excel组文本框和图片

时间:2014-12-09 14:27:58

标签: c# excel openxml-sdk

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

我需要哪些课程,什么是appands什么或属性?

此外,如果它是凝固的话会很好。所以当你拖动一个时,另一个跟着。

代码看起来像这样(我知道它有点多,但我不能再削减它):

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
    }

1 个答案:

答案 0 :(得分:0)

我认为您是OpenXml SDK的新手

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

这里下载OpenXMLSDKV25.msi,OpenXMLSDKToolV25.msi。安装两个。

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

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

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