Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is the method to create an excel file and calling it whatever the user enters in a textbox. It is saved in the bin directory by default
- private void CreateExcelButton_Click(object sender, EventArgs e)
- {
- filename = ExcelFilenameTextbox.Text;
- imagelocation = @"C:\Users\Administrator\Desktop\cheese.jpg";
- if (!string.IsNullOrWhiteSpace(filename))
- {
- var workbook = new XLWorkbook();
- var worksheet = workbook.Worksheets.Add("Sheet1");
- worksheet.Cell("A1").Value = "Hello World!";
- workbook.SaveAs(filename + ".xlsx");
- MessageBox.Show("Excel file has been created");
- ExcelFilenameTextbox.Text = "";
- }
- else
- MessageBox.Show(ReturnErrorFromParameters(1));
- }
- //This is the method to add an image to the pre created excel file
- //The way I was planning to make it work was to just open a pre loaded location for testing purposes however it wont work.
- public void AddImageToExcel(MemoryStream imagestream)
- {
- //filename = filename + ".xlsx";
- using (SpreadsheetDocument sd = SpreadsheetDocument.Open(filelocation, true))
- {
- //using (SpreadsheetDocument sd = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
- //{
- // Add a WorkbookPart to the document.
- //WorkbookPart workbookpart = sd.AddWorkbookPart();
- WorkbookPart workbookpart = sd.WorkbookPart;
- // workbookpart.Workbook = new Workbook();
- // WorksheetPart w
- // Add a WorksheetPart to the WorkbookPart.
- WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
- worksheetPart.Worksheet = new Worksheet(new SheetData());
- // Add Sheets to the Workbook.
- Sheets sheets = sd.WorkbookPart.Workbook.
- AppendChild<Sheets>(new Sheets());
- // Append a new worksheet and associate it with the workbook.
- Sheet sheet = new Sheet()
- {
- Id = sd.WorkbookPart.GetIdOfPart(worksheetPart),
- SheetId = 2,
- Name = "Sheet2"
- };
- sheets.Append(sheet);
- DrawingsPart dp = sd.WorkbookPart.WorksheetParts.First().AddNewPart<DrawingsPart>();
- ImagePart imgp = dp.AddImagePart(ImagePartType.Jpeg, sd.WorkbookPart.WorksheetParts.First().GetIdOfPart(dp));
- MemoryStream bmstream = new MemoryStream(imagestream.ToArray());
- bmstream.Seek(0, SeekOrigin.Begin);
- MemoryStream fs;
- using (fs = imagestream)
- {
- fs.Position = 0;
- imgp.FeedData(fs);
- }
- DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualDrawingProperties nvdp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualDrawingProperties();
- nvdp.Id = 1025;
- nvdp.Name = "Chart Image";
- nvdp.Description = "Image";
- DocumentFormat.OpenXml.Drawing.PictureLocks piclocks = new DocumentFormat.OpenXml.Drawing.PictureLocks();
- piclocks.NoChangeAspect = true;
- piclocks.NoChangeArrowheads = true;
- DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureDrawingProperties nvpdp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureDrawingProperties();
- nvpdp.PictureLocks = piclocks;
- DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureProperties nvpp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureProperties();
- nvpp.NonVisualDrawingProperties = nvdp;
- nvpp.NonVisualPictureDrawingProperties = nvpdp;
- DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
- stretch.FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle();
- DocumentFormat.OpenXml.Drawing.Spreadsheet.BlipFill blipfill = new DocumentFormat.OpenXml.Drawing.Spreadsheet.BlipFill();
- DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip();
- blip.Embed = dp.GetIdOfPart(imgp);
- blip.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print;
- blipfill.Blip = blip;
- blipfill.SourceRectangle = new DocumentFormat.OpenXml.Drawing.SourceRectangle();
- blipfill.Append(stretch);
- DocumentFormat.OpenXml.Drawing.Transform2D t2d = new DocumentFormat.OpenXml.Drawing.Transform2D();
- DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset();
- offset.X = 0;
- offset.Y = 0;
- t2d.Offset = offset;
- Bitmap bm = new Bitmap(bmstream);
- DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents();
- extents.Cx = ((long)bm.Width * (long)((float)914400 / bm.HorizontalResolution));
- extents.Cy = ((long)bm.Height * (long)((float)914400 / bm.VerticalResolution));
- bm.Dispose();
- t2d.Extents = extents;
- DocumentFormat.OpenXml.Drawing.Spreadsheet.ShapeProperties sp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.ShapeProperties();
- sp.BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto;
- sp.Transform2D = t2d;
- DocumentFormat.OpenXml.Drawing.PresetGeometry prstgeom = new DocumentFormat.OpenXml.Drawing.PresetGeometry();
- prstgeom.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle;
- prstgeom.AdjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList();
- sp.Append(prstgeom);
- sp.Append(new DocumentFormat.OpenXml.Drawing.NoFill());
- DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture();
- picture.NonVisualPictureProperties = nvpp;
- picture.BlipFill = blipfill;
- picture.ShapeProperties = sp;
- DocumentFormat.OpenXml.Drawing.Spreadsheet.Position pos = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Position();
- //The position corrosponds these numbers. X= 600000 & y = 200000 adds up to 1 cell
- pos.X = 600000;
- pos.Y = 600000;
- Extent ext = new Extent();
- ext.Cx = extents.Cx;
- ext.Cy = extents.Cy;
- AbsoluteAnchor anchor = new AbsoluteAnchor();
- Xdr.Position pp = new Xdr.Position();
- pp.X = 0;
- pp.Y = 0;
- anchor.Position = pp;
- anchor.Position = pos;
- anchor.Extent = ext;
- anchor.Append(picture);
- anchor.Append(new ClientData());
- WorksheetDrawing wsd = new WorksheetDrawing();
- wsd.Append(anchor);
- Drawing drawing = new Drawing();
- drawing.Id = dp.GetIdOfPart(imgp);
- wsd.Save(dp);
- sd.WorkbookPart.WorksheetParts.First().Worksheet.Append(drawing);
- // Close the document.
- //sd.Close();
- //}
- }
- MessageBox.Show("Excel File created");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement