Dear Syncfusion support,
I am trying to export data from SfDataGrid to pdf and excel. I have followed your documentation and the code executes without error, it just does not save the file to the device. On your sample app 'Syncfusion Components for Xamarin.Forms' downloaded from the Google Play store, it also does not seem to work. Any guidance for this would be greatly appreciated.
Here is some sample code:
Code behind:
private void ExportExcel()
{
DataGridExcelExportingController excelExport = new DataGridExcelExportingController();
var excelEngine = excelExport.ExportToExcel(this.dataGrid);
var workbook = excelEngine.Excel.Workbooks[0];
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
workbook.Close();
excelEngine.Dispose();
Xamarin.Forms.DependencyService.Get<ISave>().Save("DataGrid.xlsx", "application/msexcel", stream);
}
Interface in shared project:
public interface ISave
{
void Save(string filename, string contentType, MemoryStream stream);
}
Android renderer in Android project:
internal class SaveAndroid : ISave
{
public void Save(string filename, string contentType, MemoryStream stream)
{
string exception = string.Empty;
string root = null;
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Android.OS.Environment.ExternalStorageDirectory.ToString();
}
else
root = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
Java.IO.File myDir = new Java.IO.File(root + "/Forward");
myDir.Mkdir();
Java.IO.File file = new Java.IO.File(myDir, filename);
if (file.Exists()) file.Delete();
try
{
FileOutputStream outs = new FileOutputStream(file);
outs.Write(stream.ToArray());
outs.Flush();
outs.Close();
}
catch (Exception e)
{
exception = e.ToString();
}
}
}
This line in your documentation is deprecated ->
root = Android.OS.Environment.ExternalStorageDirectory.ToString();
|
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Android.App.Application.Context.GetExternalFilesDir
(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
] |
Hi Suja,
Could you please let me know where I can find the package 'Syncfusion.Xamarin.Data'? I am not able to find it in nuget package manager. I had to remove that package from the solution you provided to build the application. That might be the reason that the exporting of the SfDataGrid still does not work. I tested on API level 29 as well (Android 10.0), but still no luck. Please see the attached screen recording
Hi Suja,
Thank you for your response and the sample project. The sample project resolves the issue I was facing.