admin管理员组文章数量:1023213
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}");
}
}
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}");
}
}
本文标签: excelDraw lines in xlsx files with C (preferred OpenXml)Stack Overflow
版权声明:本文标题:excel - Draw lines in .xlsx files with C# (preferred OpenXml) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598529a2158317.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论