最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

excel - Draw lines in .xlsx files with C# (preferred OpenXml) - Stack Overflow

matteradmin5PV0评论

I want to draw a Line in a .xlsx file using a C# program. The line should be able to go from a starting-point to a lower placed point, that is to the left side of the starting point.

Example: from E5 (5/5) to C10 (3/10).

I already did the following code, but every time I open the file, it tells me that a problem occurred and if they should recreate as much as possible. If I press no, nothing happens and if yes its a line from (3/5) to (5/10).

I created the code using the OpenXml v2.19 nuget package (my company uses this):

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using D = DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Drawing.Spreadsheet;
using DS = DocumentFormat.OpenXml.Drawing.Spreadsheet;
using System;

public class ExcelLinie
{
    static void Main()
    {
        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string filePath = System.IO.Path.Combine(desktopPath, "Example.xlsx");

        using (SpreadsheetDocument document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
        {
            // Create Workbook-Part
            WorkbookPart workbookPart = document.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();

            // Add Worksheet
            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add Sheets in workbook
            Sheets sheets = document.WorkbookPart.Workbook.AppendChild(new Sheets());

            // New Sheet
            Sheet sheet = new Sheet()
            {
                Id = document.WorkbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "MySheet"
            };
            sheets.Append(sheet);

            // Add Drawing
            DrawingsPart drawingsPart;

            if (worksheetPart.DrawingsPart == null)
            {
                drawingsPart = worksheetPart.AddNewPart<DrawingsPart>();
                worksheetPart.Worksheet.AppendChild(new Drawing() { Id = worksheetPart.GetIdOfPart(drawingsPart) });
            }
            else
            {
                drawingsPart = worksheetPart.DrawingsPart;
            }

            // Creates XML structure
            var worksheetDrawing = new WorksheetDrawing();

            // Creates Line
            var twoCellAnchor = new TwoCellAnchor(
                new DS.FromMarker( // Start point
                    new ColumnId("5"),
                    new ColumnOffset("0"),
                    new RowId("5"),
                    new RowOffset("0")
                ),
                new DS.ToMarker( // End point
                    new ColumnId("3"),
                    new ColumnOffset("0"),
                    new RowId("10"),
                    new RowOffset("0")
                ),
                new ConnectionShape(
                    new NonVisualConnectionShapeProperties(
                        new NonVisualDrawingProperties() { Id = (UInt32Value)3U, Name = "Straight Connector 2" },
                        new NonVisualConnectorShapeDrawingProperties()
                    ), // Shape
                    new ShapeProperties(
                        new D.Transform2D(
                            new D.Offset() { X = 0, Y = 0 },
                            new D.Extents() { Cx = 0, Cy = 0 }
                        ),
                        new D.PresetGeometry(
                            new D.AdjustValueList()
                        )
                        { Preset = D.ShapeTypeValues.Line },
                        new D.Outline(
                            new D.SolidFill(
                                new D.RgbColorModelHex() { Val = "326417" } // Color Hexcode
                            )
                        )
                        {
                            Width = 12700 * 2 // Thickness 1 Point = 12700 EMUs
                        }
                    )
                ),
                new ClientData()
            );
            worksheetDrawing.Append(twoCellAnchor);

            drawingsPart.WorksheetDrawing = worksheetDrawing;
            drawingsPart.WorksheetDrawing.Save();

            // Save Workbook
            workbookPart.Workbook.Save();
        }

        Console.WriteLine($"File saved at {filePath}");
    }
}
Post a comment

comment list (0)

  1. No comments so far