Skip to content

[Feature] Add support for images#392

Open
andrzejchm wants to merge 11 commits intojustkawal:mainfrom
andrzejchm:main
Open

[Feature] Add support for images#392
andrzejchm wants to merge 11 commits intojustkawal:mainfrom
andrzejchm:main

Conversation

@andrzejchm
Copy link
Copy Markdown

@andrzejchm andrzejchm commented Jan 23, 2025

This PR adds support for reading and writing images in Excel (.xlsx) files. The implementation:

Key Features:

  • Support for reading images from existing Excel files
  • Support for adding new images to Excel files

Technical Implementation:

  • Adds ImageCellValue for representing images in cells
  • Creates dedicated _ImageCellParser for reading images from Excel files
  • Creates dedicated _ImageCellCreator for writing images to Excel files
  • Handles Excel's internal image storage format and relationships
  • Properly manages Excel's drawing relationships and media files
  • Supports EMU (English Metric Unit) conversion for accurate image sizing

Example usage:

// Reading an image from a cell                                                                                                                                                                                                                     
final imageCellValue = excel.sheets['Sheet1']!.cell(CellIndex.indexByString("A1")).value as ImageCellValue;                                                                                                                                          
final imageBytes = imageCellValue.bytes;                                                                                                                                                                                                            
final imageFormat = imageCellValue.format; // "png", "jpg", etc.                                                                                                                                                                                    
                                                                                                                                                                                                                                                    
// Writing an image to a cell                                                                                                                                                                                                                       
final imageBytes = File('image.png').readAsBytesSync();                                                                                                                                                                                             
final imageCellValue = ImageCellValue(bytes: imageBytes, format: 'png');                                                                                                                                                                            
excel.sheets['Sheet1'].cell(CellIndex.indexByString("A1")).value = imageCellValue;     

I've augmented excel_example.dart and excel_test.dart files with image support showcase/testing.

@falerr
Copy link
Copy Markdown

falerr commented Mar 20, 2025

Really good concept and very necessary, but tried it with diferent images and their cell appears empty.
Tried using it inside a List<CellValue?> to use on a sheet.insertRowIterables(cellvalues, rowIndex);

Edit: Tried also like in example and nothing, newSheet.cell(CellIndex.indexByColumnRow(columnIndex: colIndex, rowIndex: rowIndex)).value.toString() looks like: [Image: jpg autox50]

Really hoping a fix to this

@Abbasi233
Copy link
Copy Markdown

this feature works very well

@falerr
Copy link
Copy Markdown

falerr commented Mar 21, 2025

this feature works very well

How did you manage to make it work?

File gets bigger with the ImageCellValues but doesnt show any images even when editing the file

@Abbasi233
Copy link
Copy Markdown

this feature works very well

How did you manage to make it work?

File gets bigger with the ImageCellValues but doesnt show any images even when editing the file

I did not do anything special. I just followed the example codes.

@andrzejchm
Copy link
Copy Markdown
Author

@falerr could you tell me which software you use for opening the excel file? I can see there are major discrepancies between Apple's Numbers and MS Excel from office 365 on Mac. Also, the solution proposed here is more of a proof-of-concept and is not suitable for HUGE number of images, as I didn't have time to introduce image reuse etc. I can post a more sophisticated approach I've built after testing it in my own app, probably as part of this PR

@justkawal justkawal self-assigned this Mar 30, 2025
@falerr
Copy link
Copy Markdown

falerr commented Apr 3, 2025

@falerr could you tell me which software you use for opening the excel file? I can see there are major discrepancies between Apple's Numbers and MS Excel from office 365 on Mac. Also, the solution proposed here is more of a proof-of-concept and is not suitable for HUGE number of images, as I didn't have time to introduce image reuse etc. I can post a more sophisticated approach I've built after testing it in my own app, probably as part of this PR

I've been using Microsoft 365 on android and LibreOffice on PC, both of them didn't show the pictures. Also thanks for your help and work

@Abbasi233
Copy link
Copy Markdown

There is a very strange problem. If I add multiple pictures to the file, it probably adds the biggest picture (this is my guess) to all the cells I have added. For example, a small car picture must be added to the cells I marked here, but the following map image shrinks.
I changed the lines of the codes in many different combinations. But the result was always the same. When I commented out the part I only added the map image, the car image seemed correctly.

image

@falerr
Copy link
Copy Markdown

falerr commented May 6, 2025

Any news? Looking forward to them!

@justkawal justkawal assigned andrzejchm and unassigned justkawal Jun 22, 2025
@justkawal justkawal requested a review from justraman as a code owner June 22, 2025 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants