XlsxReaderWriter
XlsxReaderWriter copied to clipboard
Can't add image to worksheet
Hey, I'm doing a simple test:
- Open a simpel .xlsx-file from the Bundle
- Add a image to the worksheet
- Save the spreadsheet to documents If I add a string value to a cell in step 2 instead of an image, I can save and view the file in the Documents folder. Seems there is an issue with adding an image to a worksheet. Here's the code:
func simpleTest() {
// 1: Load worksheet
let loadPath = Bundle.main.path(forResource: "simpleTemplate", ofType: "xlsx")
let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(loadPath)
var worksheet: BRAWorksheet = spreadsheet.workbook.worksheets[0] as! BRAWorksheet
// 2: Add Image
let image: UIImage = UIImage(named: "awesome-face.png")!
var drawing: BRAWorksheetDrawing = worksheet.add(image, inCellReferenced: "B2", withOffset: CGPoint(x: 30, y: 30), size: image.size, preserveTransparency: true)
drawing.insets = UIEdgeInsetsMake(0.0, 0.0, 0.5, 0.5)
// 3: Save worksheet
var savePath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!
savePath += "/template-saved.xlsx"
spreadsheet.save(as: savePath)
}
I'm using Swift 3, Xcode 8.1 and a current version of XlsxReaderWriter. I've tried with all the three different worksheet.addImage()-methods.
Have you tried to print image
, image.size
and drawing
value in debugger ? Are they not null ?
Try to add the following code before "2: Add Image":
worksheet.cellForCellReference("B2", shouldCreate: true).stringValue = ""
I am also seeing this same problem. I'm using Objective C.
image, image.size, and drawing all look good. I added the code you suggested but it didn't fix the problem.
When I stepped through the debugger, I found that self.relationships is nil in BRAWorksheet:drawings at the line:
[self.relationships addRelationship:_drawings];
I wonder if that's part of the problem.
@yotmylmwyd i am also unable to add image in worksheet. Did you solve the problem?
I tracked the problem down to the point where self.relationships is nil in BRAWorksheet:drawings, but wasn't able to dig any deeper. Hopefully that information will be helpful to renebigot.
Hi there, same problem for me.
I'm trying to add and image with
worksheet.add(image, inCellReferenced: cellRef, withOffset: .zero, size: imgSize, preserveTransparency: false)
And after saving, I get no image.
When I try to do
let savedImage = worksheet.image(forCellReference: cellRef).uiImage
right after addImage, savedImage returns nil.
Any idea ?
Edit: Finally using pod 'libxlsxwriter'
, which can create & write xlsx (no reading available, so can't fill a template, must re-create it)
Hi,
First of all, thanks to @renebigot for this great library.
Regarding the issue relating to adding images, I avoided it by adding an image to my "template" spreadsheet, just a 1x1 pixel image. Since then I can add any image programmatically.
IMHO the reason why this "fix" works is due to the way the XSLX file format is defined. Given that the XLSX format is actually a folder with XML files and folders where other resources are stored, such as images, I thought that probably the library assumed that this "media" folder existed already. And this is what seems to happen. Once an image is added manually to the template spreadsheet the media folder is created and it works.
This is why the unit tests work succesfully, they use a template spreadsheet with a preexistant image, so the media folder inside the XLSX file exists already.
Maybe I'm wrong and @renebigot can give a better explanation.
Thanks again!
jsanchis's solution has fixed the issue in ios 11 as well. Thank you!