Advertisement
Guest User

Untitled

a guest
Sep 17th, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. //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
  2.  
  3. private void CreateExcelButton_Click(object sender, EventArgs e)
  4. {
  5. filename = ExcelFilenameTextbox.Text;
  6. imagelocation = @"C:\Users\Administrator\Desktop\cheese.jpg";
  7. if (!string.IsNullOrWhiteSpace(filename))
  8. {
  9. var workbook = new XLWorkbook();
  10. var worksheet = workbook.Worksheets.Add("Sheet1");
  11. worksheet.Cell("A1").Value = "Hello World!";
  12. workbook.SaveAs(filename + ".xlsx");
  13. MessageBox.Show("Excel file has been created");
  14. ExcelFilenameTextbox.Text = "";
  15. }
  16. else
  17. MessageBox.Show(ReturnErrorFromParameters(1));
  18. }
  19.  
  20.  
  21. //This is the method to add an image to the pre created excel file
  22. //The way I was planning to make it work was to just open a pre loaded location for testing purposes however it wont work.
  23.  
  24. public void AddImageToExcel(MemoryStream imagestream)
  25. {
  26. //filename = filename + ".xlsx";
  27.  
  28. using (SpreadsheetDocument sd = SpreadsheetDocument.Open(filelocation, true))
  29. {
  30. //using (SpreadsheetDocument sd = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
  31. //{
  32. // Add a WorkbookPart to the document.
  33. //WorkbookPart workbookpart = sd.AddWorkbookPart();
  34. WorkbookPart workbookpart = sd.WorkbookPart;
  35. // workbookpart.Workbook = new Workbook();
  36.  
  37. // WorksheetPart w
  38.  
  39. // Add a WorksheetPart to the WorkbookPart.
  40. WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
  41. worksheetPart.Worksheet = new Worksheet(new SheetData());
  42.  
  43. // Add Sheets to the Workbook.
  44. Sheets sheets = sd.WorkbookPart.Workbook.
  45. AppendChild<Sheets>(new Sheets());
  46.  
  47. // Append a new worksheet and associate it with the workbook.
  48. Sheet sheet = new Sheet()
  49. {
  50. Id = sd.WorkbookPart.GetIdOfPart(worksheetPart),
  51. SheetId = 2,
  52. Name = "Sheet2"
  53. };
  54. sheets.Append(sheet);
  55.  
  56. DrawingsPart dp = sd.WorkbookPart.WorksheetParts.First().AddNewPart<DrawingsPart>();
  57. ImagePart imgp = dp.AddImagePart(ImagePartType.Jpeg, sd.WorkbookPart.WorksheetParts.First().GetIdOfPart(dp));
  58.  
  59. MemoryStream bmstream = new MemoryStream(imagestream.ToArray());
  60. bmstream.Seek(0, SeekOrigin.Begin);
  61.  
  62. MemoryStream fs;
  63. using (fs = imagestream)
  64. {
  65. fs.Position = 0;
  66. imgp.FeedData(fs);
  67. }
  68.  
  69. DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualDrawingProperties nvdp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualDrawingProperties();
  70. nvdp.Id = 1025;
  71. nvdp.Name = "Chart Image";
  72. nvdp.Description = "Image";
  73. DocumentFormat.OpenXml.Drawing.PictureLocks piclocks = new DocumentFormat.OpenXml.Drawing.PictureLocks();
  74. piclocks.NoChangeAspect = true;
  75. piclocks.NoChangeArrowheads = true;
  76. DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureDrawingProperties nvpdp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureDrawingProperties();
  77. nvpdp.PictureLocks = piclocks;
  78. DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureProperties nvpp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualPictureProperties();
  79. nvpp.NonVisualDrawingProperties = nvdp;
  80. nvpp.NonVisualPictureDrawingProperties = nvpdp;
  81.  
  82. DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
  83. stretch.FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle();
  84.  
  85. DocumentFormat.OpenXml.Drawing.Spreadsheet.BlipFill blipfill = new DocumentFormat.OpenXml.Drawing.Spreadsheet.BlipFill();
  86. DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip();
  87. blip.Embed = dp.GetIdOfPart(imgp);
  88. blip.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print;
  89. blipfill.Blip = blip;
  90. blipfill.SourceRectangle = new DocumentFormat.OpenXml.Drawing.SourceRectangle();
  91. blipfill.Append(stretch);
  92.  
  93. DocumentFormat.OpenXml.Drawing.Transform2D t2d = new DocumentFormat.OpenXml.Drawing.Transform2D();
  94. DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset();
  95. offset.X = 0;
  96. offset.Y = 0;
  97. t2d.Offset = offset;
  98. Bitmap bm = new Bitmap(bmstream);
  99.  
  100. DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents();
  101. extents.Cx = ((long)bm.Width * (long)((float)914400 / bm.HorizontalResolution));
  102. extents.Cy = ((long)bm.Height * (long)((float)914400 / bm.VerticalResolution));
  103. bm.Dispose();
  104. t2d.Extents = extents;
  105. DocumentFormat.OpenXml.Drawing.Spreadsheet.ShapeProperties sp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.ShapeProperties();
  106. sp.BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto;
  107. sp.Transform2D = t2d;
  108. DocumentFormat.OpenXml.Drawing.PresetGeometry prstgeom = new DocumentFormat.OpenXml.Drawing.PresetGeometry();
  109. prstgeom.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle;
  110. prstgeom.AdjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList();
  111. sp.Append(prstgeom);
  112. sp.Append(new DocumentFormat.OpenXml.Drawing.NoFill());
  113.  
  114. DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture();
  115. picture.NonVisualPictureProperties = nvpp;
  116. picture.BlipFill = blipfill;
  117. picture.ShapeProperties = sp;
  118.  
  119. DocumentFormat.OpenXml.Drawing.Spreadsheet.Position pos = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Position();
  120.  
  121. //The position corrosponds these numbers. X= 600000 & y = 200000 adds up to 1 cell
  122. pos.X = 600000;
  123. pos.Y = 600000;
  124.  
  125. Extent ext = new Extent();
  126. ext.Cx = extents.Cx;
  127. ext.Cy = extents.Cy;
  128. AbsoluteAnchor anchor = new AbsoluteAnchor();
  129.  
  130. Xdr.Position pp = new Xdr.Position();
  131. pp.X = 0;
  132. pp.Y = 0;
  133.  
  134. anchor.Position = pp;
  135. anchor.Position = pos;
  136. anchor.Extent = ext;
  137. anchor.Append(picture);
  138. anchor.Append(new ClientData());
  139. WorksheetDrawing wsd = new WorksheetDrawing();
  140. wsd.Append(anchor);
  141. Drawing drawing = new Drawing();
  142. drawing.Id = dp.GetIdOfPart(imgp);
  143. wsd.Save(dp);
  144. sd.WorkbookPart.WorksheetParts.First().Worksheet.Append(drawing);
  145.  
  146.  
  147. // Close the document.
  148. //sd.Close();
  149. //}
  150. }
  151. MessageBox.Show("Excel File created");
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement