Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
XamarinAdventCalendarApp: PublicationViewModel.cs
using System;
using System.Windows.Input;
using System.Threading.Tasks;
using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Essentials;
using XamarinAdventCalendarApp.Models;
using XamarinAdventCalendarApp.Services;
//... more namespaces omitted
namespace XamarinAdventCalendarApp.ViewModels
{
public class PublicationViewModel : BaseViewModel
{
//... some ViewModel code omitted
public ICommand ExportToExcelCommand { private set; get; }
private ExcelService excelService;
public PublicationViewModel()
{
//... some constructor code omitted
ExportToExcelCommand = new Command(async () => await ExportToExcel());
excelService = new ExcelService();
}
async Task ExportToExcel()
{
var fileName = $"{Guid.NewGuid()}.xlsx";
string filePath = excelService.GenerateExcel(fileName);
var header = new List<string>() { "ID", "Date", "Publication Title", "Author" };
var data = new ExcelData();
data.Headers = header;
foreach (var publication in Publications)
{
var row = new List<string>()
{
publication.Id.ToString(),
publication.ScheduledDate,
publication.Title,
publication.Author
};
data.Values.Add(row);
}
excelService.InsertDataIntoSheet(filePath, "Publications", data);
await Launcher.OpenAsync(new OpenFileRequest()
{
File = new ReadOnlyFile(filePath)
});
}
//... more ViewModel code omitted
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.