ClosedXML: Comments do not work if Image Added (my first error posting)

Read and complete the full issue template

(My First Error Post) There is error I have if I add a comment and if I have also added an image ? The error only happens on opening a generated excel file “we found a problem with some content in ‘filename.xlsx’. Do you want us to try to recover as much as we can? if you trust the source of this workbook, click yes”

Do you want to request a feature or report a bug?

  • Bug
  • Feature

Version of ClosedXML

e.g. 0.87.1

What is the current behavior? The resulting file is currupt with error message “we found a problem with some content in ‘filename.xlsx’. Do you want us to try to recover as much as we can? if you trust the source of this workbook, click yes” Complete this.

What is the expected behavior or new feature? Both Add Comments and Add Image should both work together Complete this.

Did this work in previous versions of our tool? Which versions? I have only used this latest version Yes/No/v0.XX

Reproducibility

This is an important section. Read it carefully. Failure to do so will cause a ‘RTFM’ comment. I only get an error is I call this line of code AddImage(wb, wbSummary.Name, 1, 1, @“Q:\images\Logo.jpg”);

Without a code sample, it is unlikely that your issue will get attention. Don’t be lazy. Do the effort and assist the developers to reproduce your problem. Code samples should be minimal complete and verifiable. Sample spreadsheets should be attached whenever applicable. Remove sensitive information.

Code to reproduce problem:

public void Main()
{
    // Code standards:
    // - Fully runnable. I should be able to copy and paste this code into a 
    //   console application and run it without having to edit it much.
    // - Declare all your variables (this follows from the previous point)
    // - The code should be a minimal code sample to illustrate issue. The code 
    //   samples on the wiki are good examples of the terseness that I want. Don't
    //   post your full application.
}
  • I attached a sample spreadsheet. (You can drag files on to this issue)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 22 (12 by maintainers)

Most upvoted comments

What I was finding was that if Inside AddImage if you comment out both .Save and .SaveAs on a NEW sheet it processes fine but the resulting excel file when opening comes up with an error message

(reply all)

OK I did a minimal cut down version (MCVE) below and it works fine …

using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClosedXML_Issue_172
{
    class Program
    {
        static void Main(string[] args)


        {
            using (XLWorkbook wb = new XLWorkbook())
            {
                var wbSummary = wb.AddWorksheet("Summary");
                wbSummary.Cell("D4").Value = "Test Value";

                AddImage(wb, wbSummary.Name, 1, 1, "image.jpg");

                // Set the comment as visible
                wbSummary.Cell("D4").Comment.SetVisible().AddText("This is a comment");

                wb.SaveAs("outputfile_stage2.xlsx");
                Console.WriteLine("Finished ... this minimal sample works fine ");
            }


            void AddImage(XLWorkbook wb, string sheetName, int col, int row, string strImageLocation)
            {
                if (!File.Exists(strImageLocation)) return;
                var ws = wb.Worksheet(sheetName);
                var image = ws.AddPicture(strImageLocation);
                image.MoveTo(ws.Cell(row, col).Address);
                image.Scale(.5); // optional: resize picture
                wb.SaveAs("outputfile_stage1.xlsx");
                // wb.Save(); // System.InvalidOperationException: 'This is a new file. Please use one of the 'SaveAs' methods.'

            }

        }
    }
}