ClosedXML / ClosedXML/ClosedXML

Corruputed Excel file when trying add multiple pictures into cells with Placement = MoveAndSize

Open
#1,627 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
5.7k
Forks
933
Avg merge
12h 14m
Merged PRs (30d)
1

Description

First of all, I want to thank you for this great library. I always thought working with Excel files was a pain but this project proved otherwise :smile:

I am currently building a small F# library called [ClosedXML.SimpleSheets](https://github.com/Zaid-Ajaj/ClosedXML.SimpleSheets) on top of this project to make even easier to build sheets.

### Bug description
Adding a single picture to a single cell where the picture size is fixed to the size of the cell (Placement = MoveAndSize) works perfectly! However, once I repeat the same process for multiple cells, I get a corrupted Excel file.

I can still open it and Excel prompts me to "recover" the lost bit. When I click "yes", I do get the images but one is missing and placement is lost

> This issue is somewhat similar to #971 but here the order of assignments of placement vs. moving to cells is *not* the issue

**Do you want to request a *feature* or report a *bug*?**
- [x] Bug
- [ ] Feature
- [ ] Question

**Did you test against the latest CI build?**
- [ ] Yes
- [x] No

If you answered `No`, please test with [the latest development build](https://ci.appveyor.com/project/ClosedXML/ClosedXML/branch/develop/artifacts) first.

**Version of ClosedXML**

e.g. 0.95.4

**What is the current behavior?**

The current behavior generates an invalid Excel file

**What is the expected behavior or new feature?**

Expected behavior is to generate a valid Excel file where each cell contains the image in subject and is fixed to that cell.

**Is this a regression from the previous version?**

No, tested against previous versions, the behavior is the same.

## Reproducibility

I already created a repo that includes a console application which reproduces the problem

see [https://github.com/Zaid-Ajaj/closed-xml-repro-multiple-images](https://github.com/Zaid-Ajaj/closed-xml-repro-multiple-images)

First of all, you need a image to test with. I am using this image called `test.jpg`

Then I use this image to add it to cells in the Excel file.

The console application has two functions, `WriteSingleImage` and `WriteMultipleImages`. The first function, `WriteSingleImage` works perfectly:
```csharp
static void WriteSingleImage(byte[] image, string fileName)
{
using var workbook = new XLWorkbook();
var sheet = workbook.AddWorksheet("Images");
var firstCell = sheet.Cell(1, 1);
var picture = sheet.AddPicture(new MemoryStream(image));
picture.MoveTo(firstCell);
picture.Placement = XLPicturePlacement.MoveAndSize;
workbook.SaveAs(fileName);
}
```
It creates a single cell with the image inside of it resized to the size of the cell which is what I want.

The second function tries to extend the logic of the `WriteSingleImage` to embed multiple images
```csharp
static void WriteMultipleImages(byte[] image, string fileName)
{
using var workbook = new XLWorkbook();
var sheet = workbook.AddWorksheet("Images");
foreach (var rowIndex in Enumerable.Range(1, 5))
{
var currentCell = sheet.Row(rowIndex).Cell(1);
var picture = sheet.AddPicture(new MemoryStream(image));
picture.MoveTo(currentCell);
picture.Placement = XLPicturePlacement.MoveAndSize;
}
workbook.SaveAs(fileName);
}
```
Which produces the seemingly corrupt Excel file, losing one image in the process and the sizing and placement is no longer consistent.

Finally the main function simply loads the image and calls the two functions
```c#
static void Main(string[] args)
{
var image = File.ReadAllBytes("test.jpg");
WriteSingleImage(image, "SingleImage.xlsx");
WriteMultipleImages(image, "MultipleImages.xlsx");
}
```

- [x] I attached a sample spreadsheet. See the generated spreadsheets from the linked repo above (and try to regenerate them yourself)

I hope I provided enough information to debug, if that is not the case, please let me know what is missing.
Thanks in advance for your time 🙏

Contributor guide

Open the contributing guide

Research direction

Use the linked closed-xml-repro-multiple-images console app, comparing WriteSingleImage and WriteMultipleImages, and begin at workbook.SaveAs. Verify the generated MultipleImages.xlsx in Excel and confirm that all five images remain valid and retain MoveAndSize placement.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.