{"id":18001465,"url":"https://github.com/egbakou/exportdatatoexcel","last_synced_at":"2025-08-12T02:35:45.746Z","repository":{"id":150080119,"uuid":"173444943","full_name":"egbakou/ExportDataToExcel","owner":"egbakou","description":"Export Xamarin.Forms ListView Items to Excel File.","archived":false,"fork":false,"pushed_at":"2019-03-08T11:21:00.000Z","size":8417,"stargazers_count":18,"open_issues_count":0,"forks_count":8,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T04:41:32.373Z","etag":null,"topics":["android","export-excel","listview","xamarin","xamarin-forms"],"latest_commit_sha":null,"homepage":"https://lioncoding.com/2019/03/07/2019-03-07-Export-de-donnees-sous-format-Excel-dans-Xamarin-Forms/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/egbakou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-02T12:25:12.000Z","updated_at":"2024-07-08T05:05:06.000Z","dependencies_parsed_at":"2023-06-26T19:11:46.078Z","dependency_job_id":null,"html_url":"https://github.com/egbakou/ExportDataToExcel","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/egbakou/ExportDataToExcel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egbakou%2FExportDataToExcel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egbakou%2FExportDataToExcel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egbakou%2FExportDataToExcel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egbakou%2FExportDataToExcel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/egbakou","download_url":"https://codeload.github.com/egbakou/ExportDataToExcel/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egbakou%2FExportDataToExcel/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269990228,"owners_count":24508819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","export-excel","listview","xamarin","xamarin-forms"],"created_at":"2024-10-29T23:17:35.500Z","updated_at":"2025-08-12T02:35:45.696Z","avatar_url":"https://github.com/egbakou.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Export ListView Items to Excel File in Xamarin.Forms\n\n\u003cimg src=\"ExportDataToExcel/screenshots/post_img.png\" /\u003e\n\n### Lioncoding article Link\n\n[Data Export in Xamarin.Forms](https://lioncoding.com/2019/03/07/2019-03-07-export-de-donnees-sous-format-excel-dans-xamarin-forms/)\n\n### Nuget packages used\n\n|                           Packages                           | Version  |           Use for           |\n| :----------------------------------------------------------: | :------: | :-------------------------: |\n|      [Autofac](https://www.nuget.org/packages/Autofac/)      |  4.9.1   |    Inversion of control     |\n| [DocumentFormat.OpenXml](https://www.nuget.org/packages/DocumentFormat.OpenXml/) |  2.9.0   | Data export to Excel format |\n| [Plugin.Permissions](https://www.nuget.org/packages/Plugin.Permissions/3.0.0.12) | 3.0.0.12 |  Adding storage permission  |\n\n### Initializations and Configurations\n\n##### Android project\n\n- MainActivity.cs\n\n```c#\nprotected override void OnCreate(Bundle bundle)\n{         \n// ...\n    Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle);\n// ...\n}\n\npublic override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)\n{\n\tPermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);\n\tbase.OnRequestPermissionsResult(requestCode, permissions, grantResults);\n}\n```\n\n- AssemblyInfo.cs (Add storage permission)\n\n```c#\n[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]\n```\n\n\n\n### Model source code\n\n```c#\nusing System;\nusing System.Collections.Generic;\nusing System.Text;\n\nnamespace ExportDataToExcel.Models\n{\n    public class XFDeveloper\n    {\n        public int ID { get; set; }\n        public string FullName { get; set; }\n        public string Phone { get; set; }\n    }\n}\n\n```\n\n### Service which provide List items\n\n```c#\nusing ExportDataToExcel.Models;\nusing System.Collections.Generic;\n\nnamespace ExportDataToExcel.Services\n{\n    public class XFDeveloperService\n    {\n        public static List\u003cXFDeveloper\u003e GetAllXamarinDevelopers()\n        {\n            List\u003cXFDeveloper\u003e developers = new List\u003cXFDeveloper\u003e\n            {\n                new XFDeveloper\n                {\n                    ID = 1,\n                    FullName = \"James Montemagno\",\n                    Phone = \"+00 0000 0001\"\n                },\n                new XFDeveloper\n                {\n                    ID = 2,\n                    FullName = \"Leomaris Rayes\",\n                    Phone = \"+00 0000 0002\"\n                },\n                new XFDeveloper\n                {\n                    ID = 3,\n                    FullName = \"K. Laurent egbakou\",\n                    Phone = \"+00 0000 0003\"\n                },\n                new XFDeveloper\n                {\n                    ID = 4,\n                    FullName = \"Houssem Dellai\",\n                    Phone = \"+00 0000 0004\"\n                },\n                new XFDeveloper\n                {\n                    ID = 5,\n                    FullName = \"Yves Gaston\",\n                    Phone = \"+00 0000 0005\"\n                },\n                new XFDeveloper\n                {\n                    ID = 6,\n                    FullName = \"John Doe\",\n                    Phone = \"+00 0000 0006\"\n                },\n                new XFDeveloper\n                {\n                    ID = 7,\n                    FullName = \"Marcel Adama\",\n                    Phone = \"+00 0000 0007\"\n                },\n                new XFDeveloper\n                {\n                    ID = 8,\n                    FullName = \"Carlos Ognankotan\",\n                    Phone = \"+00 0000 0008\"\n                }\n            };\n\n            return developers;\n        }                        \n    }\n}\n\n```\n\n\n\n### View source code(We want to export ListView items to Excel file)\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\" ?\u003e\n\u003cContentPage\n    x:Class=\"ExportDataToExcel.Views.MainMenuView\"\n    xmlns=\"http://xamarin.com/schemas/2014/forms\"\n    xmlns:x=\"http://schemas.microsoft.com/winfx/2009/xaml\"\n    Title=\"{Binding Title}\"\u003e\n\n\n    \u003cContentPage.ToolbarItems\u003e\n        \u003cToolbarItem\n            Command=\"{Binding ExportToExcelCommand}\"\n            Icon=\"shareexcel.png\"\n            Text=\"Export excel\" /\u003e\n    \u003c/ContentPage.ToolbarItems\u003e\n\n\n    \u003cContentPage.Content\u003e\n        \u003cStackLayout\u003e\n            \u003cStackLayout VerticalOptions=\"FillAndExpand\"\u003e\n                \u003cListView\n                    CachingStrategy=\"RecycleElement\"\n                    HasUnevenRows=\"False\"\n                    ItemsSource=\"{Binding Developers}\"\n                    SeparatorColor=\"Transparent\"\n                    SeparatorVisibility=\"None\"\u003e\n                    \u003cListView.RowHeight\u003e\n                        \u003cOnIdiom\n                            x:TypeArguments=\"x:Int32\"\n                            Phone=\"80\"\n                            Tablet=\"120\" /\u003e\n                    \u003c/ListView.RowHeight\u003e\n                    \u003cListView.ItemTemplate\u003e\n                        \u003cDataTemplate\u003e\n                            \u003cViewCell\u003e\n                                \u003cGrid ColumnSpacing=\"20\" RowSpacing=\"0\"\u003e\n\n                                    \u003cGrid.ColumnDefinitions\u003e\n                                        \u003cColumnDefinition Width=\"10\" /\u003e\n                                        \u003cColumnDefinition Width=\"Auto\" /\u003e\n                                        \u003cColumnDefinition Width=\"Auto\" /\u003e\n                                    \u003c/Grid.ColumnDefinitions\u003e\n\n                                    \u003cGrid.RowDefinitions\u003e\n                                        \u003cRowDefinition Height=\"*\" /\u003e\n                                        \u003cRowDefinition Height=\"1\" /\u003e\n                                    \u003c/Grid.RowDefinitions\u003e\n\n                                    \u003cBoxView\n                                        Grid.Row=\"0\"\n                                        Grid.Column=\"0\"\n                                        BackgroundColor=\"LightGray\"\n                                        HorizontalOptions=\"Start\"\n                                        WidthRequest=\"10\" /\u003e\n\n\n                                    \u003cImage\n                                        Grid.Row=\"0\"\n                                        Grid.Column=\"1\"\n                                        HeightRequest=\"50\"\n                                        Source=\"user.png\"\n                                        WidthRequest=\"50\" /\u003e\n\n\n                                    \u003cStackLayout\n                                        Grid.Row=\"0\"\n                                        Grid.Column=\"2\"\n                                        HorizontalOptions=\"FillAndExpand\"\n                                        Orientation=\"Vertical\"\n                                        VerticalOptions=\"Center\"\u003e\n                                        \u003c!--  FULLNAME  --\u003e\n                                        \u003cLabel\n                                            FontSize=\"18\"\n                                            HorizontalOptions=\"FillAndExpand\"\n                                            Text=\"{Binding FullName}\"\n                                            VerticalOptions=\"Start\" /\u003e\n                                        \u003c!--  PHONE  --\u003e\n                                        \u003cLabel\n                                            FontSize=\"15\"\n                                            Text=\"{Binding Phone}\"\n                                            TextColor=\"#43A047\"\n                                            VerticalOptions=\"Start\" /\u003e\n                                    \u003c/StackLayout\u003e\n\n\n                                    \u003cBoxView\n                                        Grid.Row=\"1\"\n                                        Grid.Column=\"0\"\n                                        Grid.ColumnSpan=\"4\"\n                                        BackgroundColor=\"LightGray\"\n                                        HeightRequest=\"1\"\n                                        Opacity=\".5\"\n                                        VerticalOptions=\"End\" /\u003e\n                                \u003c/Grid\u003e\n                            \u003c/ViewCell\u003e\n                        \u003c/DataTemplate\u003e\n                    \u003c/ListView.ItemTemplate\u003e\n                \u003c/ListView\u003e\n            \u003c/StackLayout\u003e\n        \u003c/StackLayout\u003e\n    \u003c/ContentPage.Content\u003e\n\u003c/ContentPage\u003e\n```\n\n### Code behind\n\n```c#\nusing ExportDataToExcel.ViewModels;\n\nusing Xamarin.Forms;\nusing Xamarin.Forms.Xaml;\n\nnamespace ExportDataToExcel.Views\n{\n    [XamlCompilation(XamlCompilationOptions.Compile)]\n    public partial class MainMenuView : ContentPage\n    {\n        private MainMenuViewModel viewModel;\n        public MainMenuView()\n        {\n            InitializeComponent();\n            BindingContext = viewModel = new MainMenuViewModel();\n            RegisterMesssages();\n        }\n\n\n        private void RegisterMesssages()\n        {\n            MessagingCenter.Subscribe\u003cMainMenuViewModel\u003e(this, \"DataExportedSuccessfully\", (m) =\u003e\n            {\n                if (m != null)\n                {\n                    DisplayAlert(\"Info\", \"Data exported Successfully. The location is :\"+m.FilePath, \"OK\");\n                }\n            });\n\n            MessagingCenter.Subscribe\u003cMainMenuViewModel\u003e(this, \"NoDataToExport\", (m) =\u003e\n            {\n                if (m != null)\n                {\n                    DisplayAlert(\"Warning !\", \"No data to export.\", \"OK\");\n                }\n            });\n        }\n\n    }\n}\n```\n\n### View Model code\n\n```c#\nusing DocumentFormat.OpenXml;\nusing DocumentFormat.OpenXml.Packaging;\nusing DocumentFormat.OpenXml.Spreadsheet;\nusing ExportDataToExcel.Interfaces;\nusing ExportDataToExcel.Models;\nusing ExportDataToExcel.Services;\nusing Plugin.Permissions;\nusing Plugin.Permissions.Abstractions;\nusing System;\nusing System.Collections.ObjectModel;\nusing System.Diagnostics;\nusing System.Linq;\nusing System.Windows.Input;\nusing Xamarin.Forms;\nusing Cell = DocumentFormat.OpenXml.Spreadsheet.Cell;\n\nnamespace ExportDataToExcel.ViewModels\n{\n    public class MainMenuViewModel : BaseViewModel\n    {\n       \n        public MainMenuViewModel()\n        {\n            Title = \"Xamarin Developers\";\n            LoadData();\n            ExportToExcelCommand = new Command(async () =\u003e await ExportDataToExcelAsync());\n        }\n\n        /* Get Xamarin developers list from Service*/\n        private void LoadData()\n        {\n            Developers = new ObservableCollection\u003cXFDeveloper\u003e(XFDeveloperService.GetAllXamarinDevelopers());\n        }\n\n\n        /* Export the list to excel file at the location provide by DependencyService */\n        public async System.Threading.Tasks.Task ExportDataToExcelAsync()\n        {\n            // Granted storage permission\n            var storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);\n\n            if (storageStatus != PermissionStatus.Granted)\n            {\n                var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Storage });\n                storageStatus = results[Permission.Storage];\n            }\n\n            if (Developers.Count() \u003e 0)\n            {\n                try\n                {\n                    string date = DateTime.Now.ToShortDateString();\n                    date = date.Replace(\"/\", \"_\");\n\n                    var path = DependencyService.Get\u003cIExportFilesToLocation\u003e().GetFolderLocation() + \"xfdevelopers\" + date + \".xlsx\";\n                    FilePath = path;\n                    using (SpreadsheetDocument document = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook))\n                    {\n                        WorkbookPart workbookPart = document.AddWorkbookPart();\n                        workbookPart.Workbook = new Workbook();\n\n                        WorksheetPart worksheetPart = workbookPart.AddNewPart\u003cWorksheetPart\u003e();\n                        worksheetPart.Worksheet = new Worksheet();\n\n                        Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());\n                        Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = \"Xamarin Forms developers list\" };\n                        sheets.Append(sheet);\n\n                        workbookPart.Workbook.Save();\n\n                        SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());\n\n                        // Constructing header\n                        Row row = new Row();\n\n                        row.Append(\n                            ConstructCell(\"No\", CellValues.String),\n                            ConstructCell(\"FullName\", CellValues.String),\n                            ConstructCell(\"Phone\", CellValues.String)\n                            );\n\n                        // Insert the header row to the Sheet Data\n                        sheetData.AppendChild(row);\n\n                        // Add each product\n                        foreach (var d in Developers)\n                        {\n                            row = new Row();\n                            row.Append(\n                                ConstructCell(d.ID.ToString(), CellValues.String),\n                                ConstructCell(d.FullName, CellValues.String),\n                                ConstructCell(d.Phone, CellValues.String));\n                            sheetData.AppendChild(row);\n                        }\n\n                        worksheetPart.Worksheet.Save();\n                        MessagingCenter.Send(this, \"DataExportedSuccessfully\");\n                    }\n\n                }\n                catch (Exception e)\n                {\n                    Debug.WriteLine(\"ERROR: \"+ e.Message);\n                }\n            }\n            else\n            {\n                MessagingCenter.Send(this, \"NoDataToExport\");\n            }\n        }\n\n\n        /* To create cell in Excel */\n        private Cell ConstructCell(string value, CellValues dataType)\n        {\n            return new Cell()\n            {\n                CellValue = new CellValue(value),\n                DataType = new EnumValue\u003cCellValues\u003e(dataType)\n            };\n        }\n\n\n        public ICommand ExportToExcelCommand { get; set; }\n\n        private ObservableCollection\u003cXFDeveloper\u003e _developers;\n        public ObservableCollection\u003cXFDeveloper\u003e Developers\n        {\n            get { return _developers; }\n            set { SetProperty(ref _developers, value); }\n        }\n\n        private string _filePath;\n        public string FilePath\n        {\n            get { return _filePath; }\n            set { SetProperty(ref _filePath, value); }\n        }\n\n    }   \n}\n\n```\n\n### Screenshots\n\n\u003cimg src=\"ExportDataToExcel/screenshots/post_img.png\" /\u003e","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegbakou%2Fexportdatatoexcel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fegbakou%2Fexportdatatoexcel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegbakou%2Fexportdatatoexcel/lists"}