0

I have a model with 4 properties. ItemName, ItemLastName, ItemAge, ItemImage.

I can export the list to excel (.xlsx) and it's working great, the problem is... I can't export the Images.

What I'm trying to get is... when I export the list to .xlsx I also want to copy (or add or append I guess) all the images inside the excel sheet.

Thank you so much.

I know there are a lot of information about that, and I tried so many of them without luck. (most of the info is to add a single image and not a list with images).

Since I'm new to coding, It will be great if you can help me with some codes or example (with no links if I can ask), Please.

This is the Import Method

IWorkbook workbook;
ISheet sheet;
FileResult file;

private async void Import_Button(object sender, EventArgs e)
{
    try
    {
        file = await FilePicker.PickAsync();
                    
        if (file.FileName.EndsWith(".xlsx"))
        {
            workbook = null;
            FileStream fs = new FileStream(file.FullPath, FileMode.Open, FileAccess.Read);
            if (file.FullPath.IndexOf(".xlsx") > 0)
                workbook = new XSSFWorkbook(fs);

            sheet = workbook.GetSheetAt(0);

            if (sheet != null)
            {
                for (int i = 1; i < sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                }
            }
        }
        else
        {
            return;
        }
    }
    catch (Exception) { }
}

and this is the Save Method

public async Task ExportDataToExcelAsync()
{
    var storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
    if (storageStatus != Plugin.Permissions.Abstractions.PermissionStatus.Granted)
    {
        var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Storage });
        storageStatus = results[Permission.Storage];
    }
            
    try
    {
        var newDir = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/TestFolder/";
        if (!Directory.Exists(newDir))
        {
            Directory.CreateDirectory(newDir);
        }

        string newFileName = DateTime.Now.ToString("Test_" + "yyyyMMdd");
        newFileName = Path.ChangeExtension(newFileName, "xlsx");
        var path = Path.Combine(newDir, newFileName);
                    
        FilePath = path;

        using (SpreadsheetDocument document = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook))
        {
            WorkbookPart workbookPart = document.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();

            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet();
                        
            Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
            Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Xamarin list" };
            sheets.Append(sheet);
                        
            workbookPart.Workbook.Save();
                        
            SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
                
            Row row = new Row();
                        
            row.Append(
                ConstructCell("Name", CellValues.String),
                ConstructCell("Last Name", CellValues.String),
                ConstructCell("Age", CellValues.String),
                ConstructCell("Item Image", CellValues.String)
                );

            sheetData.AppendChild(row);

            foreach (var d in allItems)
            {
                row = new Row();

                row.Append(
                    ConstructCell(d.ItemName.ToString(), CellValues.String),
                    ConstructCell(d.ItemLastName, CellValues.String),
                    ConstructCell(d.ItemAge, CellValues.String),
                    ConstructCell(d.ItemImage.ToString(), CellValues.String)
                    );

                sheetData.AppendChild(row);
            }

            worksheetPart.Worksheet.Save();
            await Navigation.PopAsync();
        }

    }
    catch (Exception) { }
}

private Cell ConstructCell(string value, CellValues dataType)
{
    return new Cell()
    {
        CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(value),
        DataType = new EnumValue<CellValues>(dataType)
    };
}

public string FilePath { get; set; }
Noah
  • 15
  • 1
  • 6
  • I tried all of them (I guess), I couldnt find a solution. And most of them talking about adding a single image and not a list of images. – Noah Jun 22 '21 at 18:49
  • this shows how to add an image anchored to a cell - https://stackoverflow.com/questions/33712621/how-put-a-image-in-a-cell-of-excel-java/33721090 – Jason Jun 22 '21 at 18:51
  • Thank you so much, but this is in Java, how can i do it in C#? (Im sorry for the question but I'm really trying to get it done somehow) – Noah Jun 22 '21 at 18:58
  • the basic API calls should be the same - you will need to try to figure it out and if you have specific question about it you should ask that. Your question doesn't even mention which library you are using to create the files - I'm just guessing at which one it is. – Jason Jun 22 '21 at 19:01
  • here is a C# example - https://github.com/nissl-lab/npoi-examples/tree/main/xssf/InsertPicturesInXlsx – Jason Jun 22 '21 at 19:03
  • Ok Bro, thank you so much im still learning – Noah Jun 22 '21 at 19:03

0 Answers0