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; }