private static void testShape() { SpreadsheetDocument SpreadSheet = SpreadsheetDocument.Open(@"C:\Users\Федотов РР\Desktop\test.xlsx", true); WorksheetPart worksheetPart = SpreadSheet.WorkbookPart.WorksheetParts.First(); OpenXmlElement[] childs = worksheetPart.DrawingsPart.WorksheetDrawing.ChildElements.ToArray(); Shape s = childs.First().Descendants <Shape>().First(); OpenXmlElement[] sp = s.Descendants <ShapeProperties>().First().ChildElements.ToArray(); OpenXmlElement[] ss = s.Descendants <ShapeStyle>().First().ChildElements.ToArray(); OpenXmlElement[] stb = s.Descendants <TextBody>().First().ChildElements.ToArray(); //WordManager.AddShape(sp, ss, stb); SpreadSheet.Close(); return; ExcelDoc d2 = new ExcelDoc(@"C:\Users\Федотов РР\Desktop\test2.xlsx", true); d2.AddSheet("test").CreateColumns(new double[] { 9.140625, 5, 4, 3 }); WorksheetPart wsp = d2.SpreadSheet.WorkbookPart.WorksheetParts.First(); if (wsp.DrawingsPart == null) { wsp.AddNewPart <DrawingsPart>("rId1"); wsp.Worksheet.AppendChild(new Drawing() { Id = "rId1" }); wsp.DrawingsPart.WorksheetDrawing = new WorksheetDrawing(); for (int i = 0; i < childs.Length; i++) { if (childs[i] is TwoCellAnchor) { TwoCellAnchor na = childs[i].Clone() as TwoCellAnchor; na.FromMarker.ColumnId.Text = "1"; na.FromMarker.RowId.Text = "1"; na.FromMarker.ColumnOffset.Text = ((int)(2.5 * 66691.28)).ToString(); na.FromMarker.RowOffset.Text = ((int)(7.5 * 12700)).ToString(); na.ToMarker.ColumnId.Text = "4"; na.ToMarker.RowId.Text = "4"; na.ToMarker.ColumnOffset.Text = "0"; na.ToMarker.RowOffset.Text = "0"; wsp.DrawingsPart.WorksheetDrawing.AppendChild(na); } } } d2.Save(); d2.Close(); }
private static void AddTable(ExcelDoc d, string name, double[] columnsSize, string[] header, string[,] content) { if (columnsSize == null || header == null || content == null || columnsSize.Length != header.Length || columnsSize.Length != content.GetLength(1)) { throw new Exception("Входные параметры имели не верный формат"); } if (columnsSize.Length == 0) { return; } ExcelSheet s = d.AddSheet(name); s.CreateColumns(columnsSize); for (int c = 0; c < columnsSize.Length; c++) { if (!string.IsNullOrEmpty(header[c]) && header[c][0] == '=') { s.SetCellFormulaValue(0, c, header[c], false, ValueTypes.String, CellStyles.BoldBorderCenter); } else { s.SetCellStringValue(0, c, header[c], ValueTypes.String, CellStyles.BoldBorderCenter); } } for (int r = 0; r < content.GetLength(0); r++) { for (int c = 0; c < content.GetLength(1); c++) { if (!string.IsNullOrEmpty(content[r, c]) && content[r, c][0] == '=') { s.SetCellFormulaValue(r + 1, c, content[r, c], false, ValueTypes.String, CellStyles.NormalBorderLeft); } else { s.SetCellStringValue(r + 1, c, content[r, c], ValueTypes.String, CellStyles.NormalBorderLeft); } } } d.Save(); d.Close(); }