title: 'The file is corrupt and cannot be opened' OpenXML
body: <p>I got that code from <strong>OpenXML sdk</strong> and instead of <code>SpreadsheetDocument.Create</code>, I used <code>SpreadsheetDocument.Open</code></p>
  
  <p>This code is working and add an image in <code>.xlsx</code>, after image added to the file. I open the file which shows -> </p>
  
  <blockquote>
    <p>The file is corrupt and cannot be opened</p>
  </blockquote>
  
  <p>If you want more details Please! let me know. </p>
  
  <p>Reference URL -> <a href="https://code.msdn.microsoft.com/office/How-to-insert-image-into-93964561" rel="nofollow noreferrer">https://code.msdn.microsoft.com/office/How-to-insert-image-into-93964561</a></p>
  
  <p>Thanks for the help!</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// add sheet in xlsx then add image into it.
      /// &lt;/summary&gt;
      /// &lt;param name="sFile"&gt;&lt;/param&gt;
      /// &lt;param name="imageFileName"&gt;&lt;/param&gt;
      public void InsertimginExcel(string sFile, string imageFileName)
      {
          try
          {
              // Create a spreadsheet document by supplying the filepath. 
              using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                  Open(sFile, true))
              {
  
                  // Add a WorkbookPart to the document. 
                  //WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
                  //workbookpart.Workbook = new Workbook();
  
                  // Add a WorksheetPart to the WorkbookPart. 
                  WorksheetPart worksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart&lt;WorksheetPart&gt;();
                  worksheetPart.Worksheet = new Worksheet(new SheetData());
  
                  // Add Sheets to the Workbook. 
                  Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
                      AppendChild&lt;Sheets&gt;(new Sheets());
  
                  // Append a new worksheet and associate it with the workbook. 
                  Sheet sheet = new Sheet()
                  {
                      Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
                      SheetId = 1,
                      Name = "mySheet"
                  };
                  sheets.Append(sheet);
  
                  var drawingsPart = worksheetPart.AddNewPart&lt;DrawingsPart&gt;();
  
                  if (!worksheetPart.Worksheet.ChildElements.OfType&lt;DocumentFormat.OpenXml.Spreadsheet.Drawing&gt;().Any())
                  {
                      worksheetPart.Worksheet.Append(new DocumentFormat.OpenXml.Spreadsheet.Drawing { Id = worksheetPart.GetIdOfPart(drawingsPart) });
                  }
  
                  if (drawingsPart.WorksheetDrawing == null)
                  {
                      drawingsPart.WorksheetDrawing = new WorksheetDrawing();
                  }
  
                  var worksheetDrawing = drawingsPart.WorksheetDrawing;
  
                  var imagePart = drawingsPart.AddImagePart(ImagePartType.Jpeg);
  
                  using (var stream = new FileStream(imageFileName, FileMode.Open))
                  {
                      imagePart.FeedData(stream);
                  }
  
                  Bitmap bm = new Bitmap(imageFileName);
                  DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents();
                  var extentsCx = (long)bm.Width * (long)((float)914400 / bm.HorizontalResolution);
                  var extentsCy = (long)bm.Height * (long)((float)914400 / bm.VerticalResolution);
                  bm.Dispose();
  
                  var colOffset = 0;
                  var rowOffset = 0;
                  int colNumber = 5;
                  int rowNumber = 10;
  
                  var nvps = worksheetDrawing.Descendants&lt;Xdr.NonVisualDrawingProperties&gt;();
                  var nvpId = nvps.Count() &gt; 0 ?
                      (UInt32Value)worksheetDrawing.Descendants&lt;Xdr.NonVisualDrawingProperties&gt;().Max(p =&gt; p.Id.Value) + 1 :
                      1U;
  
                  var oneCellAnchor = new Xdr.OneCellAnchor(
                      new Xdr.FromMarker
                      {
                          ColumnId = new Xdr.ColumnId((colNumber - 1).ToString()),
                          RowId = new Xdr.RowId((rowNumber - 1).ToString()),
                          ColumnOffset = new Xdr.ColumnOffset(colOffset.ToString()),
                          RowOffset = new Xdr.RowOffset(rowOffset.ToString())
                      },
                      new Xdr.Extent { Cx = extentsCx, Cy = extentsCy },
                      new Xdr.Picture(
                          new Xdr.NonVisualPictureProperties(
                              new Xdr.NonVisualDrawingProperties { Id = nvpId, Name = "Picture " + nvpId, Description = imageFileName },
                              new Xdr.NonVisualPictureDrawingProperties(new A.PictureLocks { NoChangeAspect = true })
                          ),
                          new Xdr.BlipFill(
                              new A.Blip { Embed = drawingsPart.GetIdOfPart(imagePart), CompressionState = A.BlipCompressionValues.Print },
                              new A.Stretch(new A.FillRectangle())
                          ),
                          new Xdr.ShapeProperties(
                              new A.Transform2D(
                                  new A.Offset { X = 0, Y = 0 },
                                  new A.Extents { Cx = extentsCx, Cy = extentsCy }
                              ),
                              new A.PresetGeometry { Preset = A.ShapeTypeValues.Rectangle }
                          )
                      ),
                      new Xdr.ClientData()
                  );
  
                  worksheetDrawing.Append(oneCellAnchor);
  
                  //workbookpart.Workbook.Save();
  
                  // Close the document. 
                  spreadsheetDocument.Close();
              }
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
              //log.Error("error occur while creating sheet and adding image --&gt; " + ex.Message.ToString());
          }
      }
  </code></pre>