We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Export SfDataGrid to PDF and Excel

Hi, I try to export SfDataGrid to Excel or PDF documents. My goal is to create an excel / pdf document (after button click) and then offer to open it (like your sample application). Unfortunately, I still can't. Can I ask for advice on where I am making a mistake? I attach a sample of my code.

Attachment: GridDataExport_f605b11a.zip

3 Replies

KK Karthikraja Kalaimani Syncfusion Team November 25, 2019 12:04 PM UTC

Hi Tomas,

 
Thanks for contacting Syncfusion support.

We have checked the reported issue “Exporting does not happen when export SfDatagrid to excel or pdf ” on your provided sample. You have wrongly defined file path and extension for pdf and excel on your sample. Now we have corrected that path and file extension for pdf and excel.

Please refer the below code example, 

[SaveAndroid.cs] 
if (file.Exists() && contentType != "application/html") 
{ 
 
                Android.Net.Uri path = Android.Net.Uri.FromFile(file); 
                string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString()); 
                string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension); 
                Intent intent = new Intent(Intent.ActionView); 
                intent.SetDataAndType(path, mimeType); 
                Forms.Context.StartActivity(Intent.CreateChooser(intent, "Choose App")); 
} 

We hope this helps, please let us know if need further assistance from us.

Regards, 
Karthik Raja


TT Tomas Turek November 25, 2019 02:56 PM UTC

Thank you for your reply. Unfortunately, your sample not working. After click button nothing happened. After debug - code stuck on a row with (FileOutputStream outs = new FileOutputStream(file);) without any exception. I also check permission in the manifest (READ_External_Storage, Write_External_Storage).  Any suggestion?


KK Karthikraja Kalaimani Syncfusion Team November 26, 2019 07:52 PM UTC

Hi Tomas,

Sorry for the inconvenience caused.

If we are not allow the storage permission for GridDataExport app it will throw expection “java.iO.FileNotFoundExpection” while creating FileOutPutStream. So make ensure before launching the app we have to allow the storage permission for GridDataExport app on App permission settings of Device.

For android SDK version 23 and above, we need to include the following code for enabling the Android file provider to save and view the generated PDF document in Android. 
 
1)     Create a new XML file with the name of provider_paths.xml under the Android project Resources folder and add the following code in it. 
Eg: Resources/xml/provider_paths.xml  

 
<?xml version="1.0" encoding="utf-8" ?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
  <external-path name="external_files" path="."/> 
</paths> 

2)     Add the following code to the AndroidManifest.xml file located under Properties/AndroidManifest.xml. 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.griddataexport" android:installLocation="auto"> 
       <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" /> 
       <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
  <application android:label="GridDataExport.Android"> 
    <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> 
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> 
    </provider> 
  </application> 
</manifest> 

3)     Include the following code in the SaveAndroid class of the Android project 
….. 
if (file.Exists() && contentType != "application/html") 
            { 
                string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString()); 
                string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension); 
                Intent intent = new Intent(Intent.ActionView); 
                intent.SetFlags( ActivityFlags.NewTask); 
 
                //// Forms.Context is obsolete, Hence used local context 
 
                Android.Net.Uri path = FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".provider", file); 
                intent.SetDataAndType(path, mimeType); 
                intent.AddFlags(ActivityFlags.GrantReadUriPermission); 
                Android.App.Application.Context.StartActivity(intent); 
            }
….
 


We hope this helps please let us know if need further assistance from us.

Regards, 
Karthik Raja 


Loader.
Live Chat Icon For mobile
Up arrow icon