private string InitImage(string id) { Guid retValue; var rel = _docRels.Where(c => c.Id == id).First(); string ext = new FileInfo(rel.TargetUri.ToString()).Extension.ToLower(); PackagePart part = _package.GetPart(new Uri("/word/" + rel.TargetUri.ToString(), UriKind.Relative)); using (Stream partStream = part.GetStream()) { Image img = null; if (ext == ".wmf" || ext == ".emf") { img = new Metafile(partStream); } else { img = Bitmap.FromStream(partStream); } using (MemoryStream ms = new MemoryStream()) { img.Save(ms, ImageFormat.Png); retValue = _currentQuestion.AddImage(ms.ToArray()); } } return(retValue.ToString()); }
private void LoadOrCreateOrigin() { PackageRelationshipCollection relationships = aasxPackage.GetRelationshipsByType(ORIGIN_RELATIONSHIP_TYPE); originPart = relationships?.Where(r => r.TargetUri == ORIGIN_URI)?.Select(p => aasxPackage.GetPart(p.TargetUri))?.FirstOrDefault(); if (originPart == null) { originPart = aasxPackage.CreatePart(ORIGIN_URI, System.Net.Mime.MediaTypeNames.Text.Plain, CompressionOption.Maximum); originPart.GetStream(FileMode.Create).Dispose(); aasxPackage.CreateRelationship(originPart.Uri, TargetMode.Internal, ORIGIN_RELATIONSHIP_TYPE); } }
public static Stream GenerateExcel() { var stream = new MemoryStream(); var document = SpreadsheetDocument .Create(stream, SpreadsheetDocumentType.Workbook); var workbookpart = document.AddWorkbookPart(); workbookpart.Workbook = new Workbook(); // Add a new worksheet part to the workbook. WorksheetPart newWorksheetPart = document.WorkbookPart.AddNewPart <WorksheetPart>(); newWorksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData()); //Sheets sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>(); var sheets = document.WorkbookPart.Workbook.AppendChild <Sheets>(new Sheets()); string relationshipId = document.WorkbookPart.GetIdOfPart(newWorksheetPart); //This bit is required for iPad to be able to read the sheets inside the xlsx file. The file will still work fine in Excel string relationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"; document.Package.GetPart(document.WorkbookPart.Uri).CreateRelationship(new Uri(newWorksheetPart.Uri.OriginalString.Replace("/xl/", String.Empty).Trim(), UriKind.Relative), TargetMode.Internal, relationshipType); document.Package.GetPart(document.WorkbookPart.Uri).DeleteRelationship(relationshipId); PackageRelationshipCollection sheetRelationships = document.Package.GetPart(document.WorkbookPart.Uri).GetRelationshipsByType(relationshipType); relationshipId = sheetRelationships.Where(f => f.TargetUri.OriginalString == newWorksheetPart.Uri.OriginalString.Replace("/xl/", String.Empty).Trim()).Single().Id; // Get a unique ID for the new sheet. uint sheetId = 1; if (sheets.Elements <Sheet>().Count() > 0) { sheetId = sheets.Elements <Sheet>().Max(s => s.SheetId.Value) + 1; } // Append the new worksheet and associate it with the workbook. Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = "test" }; sheets.Append(sheet); //worksheets.Add(new Worksheet(newWorksheetPart.Worksheet, sheetId)); var worksheetPart = workbookpart.AddNewPart <WorksheetPart>(); var sheetData = new SheetData(); worksheetPart.Worksheet = new Worksheet(sheetData); /* * var sheet = new Sheet() * { * Id = document.WorkbookPart * .GetIdOfPart(worksheetPart), * SheetId = 1, * Name = "Sheet 1" * }; * sheets.AppendChild(sheet); */ // Add header UInt32 rowIdex = 0; var row = new Row { RowIndex = ++rowIdex }; sheetData.AppendChild(row); int cellIdex = 0; /* * foreach (var header in data.Headers) * { * row.AppendChild(CreateTextCell(ColumnLetter(cellIdex++), * rowIdex, header ?? string.Empty)); * } * if (data.Headers.Count > 0) * { * // Add the column configuration if available * if (data.ColumnConfigurations != null) * { * var columns = (Columns)data.ColumnConfigurations.Clone(); * worksheetPart.Worksheet * .InsertAfter(columns, worksheetPart * .Worksheet.SheetFormatProperties); * } * } */ // Add sheet data foreach (var rowData in new [] { new [] { "a", "b", "c" }, new [] { "d", "e", "f" } }) { cellIdex = 0; row = new Row { RowIndex = ++rowIdex }; sheetData.AppendChild(row); foreach (var callData in rowData) { var cell = CreateTextCell(ColumnLetter(cellIdex++), rowIdex, callData ?? string.Empty); row.AppendChild(cell); } } workbookpart.Workbook.Save(); document.Close(); return(stream); }