How to display Excel file in android using aspose Cells lib

I want to display Excel file, it should be from within app.

Is it possible to display Excel file using aspose Cells lib?
I’m using Android studio as IDE.

Hi,

Thanks for your posting and considering Aspose.Cells.

Aspose.Cells for Android is not a GUI component and cannot display excel files. However, you can use it to get the image of your entire worksheet and then can display those images.

Please see the following documentation article that explains how to convert worksheet into image.

( Converting Worksheet to Image and Worksheet to Image by Page|Documentation )

Please note, you should set ImageOrPrintOptions.setOnePagePerSheet() to true if you want to convert entire worksheet into image.

Thanks Shakeel for your quick response.
I have created a new project and in that i am adding aspose-cells-8.7.0.jar in the libs folder and aspose-cells-8.7.0-libs.apk in the assets folder. I am loading and creating workbook with the following functions:

LoadOptions loadOptions = new LoadOptions(FileFormatType.XLSX);

//Creating an Workbook object with 2007 xlsx file path and the loadOptions object
Workbook workbook = new Workbook(sdPath + “/TestExcel.xlsx”, loadOptions);
On compiling the project the build runs more than 30 minutes and it does not even build successfully after the given time.On top of that during the running build it takes a heavy load on the machine. I am working on OS X Yosemite 10.10.5. Kindly address the given problem. Awaiting your response.

Hi,

Thanks for your posting and using Aspose.Cells for Android.

We are having problems on Android Studio and we researched on it and could not fix it. But the issue is still open. However, we support a JAR file which should work well with any Android
Development IDE. For now, we recommend you to use Eclipse because Android Studio is new and not stable.

Please check these threads for your reference.

( Using Aspose.Cells for Android in Android Studio 1.0 )

We have also logged this issue in our database for investigation. We will look into it and fix this issue. Once the issue is resolved or we have some other update for you, we will let you know asap.

This issue has been logged as

  • CELLSANDROID-59 - Aspose.Cells should work well on Android Studio

Hi,

Thanks for using Aspose.Cells for Android.

Please set the following in build.gradle in app model. It will work OK.

dexOptions {
javaMaxHeapSize “4g”
}

Also see this post:

( Not able to integrate aspose.pdf for android in android studio )

Thanks a lot Shakeel for your quick response and valuable help.
This above solution work for me and now there is not any problem with Aspose Cell lib.

Hi,

Thanks for your valuable feedback and using Aspose.Cells for Android.

It is good to know that the suggested solution resolved your issue. Let us know if you encounter any other issue, we will be glad to look into it and help you further.

Hi Shakeel,
I am able to convert the sheet into jpeg images.However the sheet is getting divided into many images which is not presentable at all . I want to view the sheet as an entire image so that it looks presentable. Is it possible to convert the sheet into a single image.
Thank you

Hi Pratibha,


Please set the ImageOrPrintOptions.OnePagePerSheet property to true. This way you can force the Aspose.Cells APIs to render all contents of a given worksheet to a single image. Please also check an article on this subject.

Hi,

Thanks for your solution,now i’m able to display one image per sheet.
But i’m facing one issue that if sheet is small then it will display it into image properly whenever sheet is large then it will gives error that,

"java.lang.OutOfMemoryError:Failed to allocate a 81549580 byte allocation with
16777120 free bytes and 36MB until OOM."

Is there is any solution for that?

Hi,

Thanks for your posting and using Aspose.Cells for Android.

OnePagePerSheet is valid only when there are few pages like 4-10. If there are more pages like 10-1000 etc then OnePagePerSheet will throw out of memory exception. Because it is not possible to render lots of pages in a single image.

So first check how many pages are in your worksheet and then decide if you can use OnePagePerSheet option or not.

You can find number of pages in a worksheet using SheetRender.getPageCount() method.

Hi,

Thanks for using Aspose.Cells for Android.

There is another workaround that you can try. First save your workbook in html format and then display it in your app.

Please see the following sample code for your reference.

Java
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(“file:///sdcard/testtmp/test.htm”);

Where test.htm is saved by workbook.save(“test.htm”);

Thanks Shakeel for your additional information!

Hi Shakeel,
I am getting the images now but now the problem is that when the sheet is very large, the generated image is also large and hence out of memory error occurs and my app crashes.

I want to compress the images. Is there any way i can obtain compressed images to prevent this error. I have tried adding largeHeapSize=true in the manifest but it is not recommended. I have been told to look for another solution.

So can you please advice me on this matter. Is there any solution to this?

Thank you

Hi,

Thanks for your posting and using Aspose.Cells.

The workaround is to first check how many pages are inside your worksheet. You can find it using SheetRender.getPageCount() method. If number of pages are 10-30, then you can use OnePagePerSheet option. If they are greater than that, then don’t use this option because it will cause out of memory exception.

The following sample code shows how to find the number of pages in a worksheet.

Java
Worksheet ws = wb.getWorksheets().get(0);

ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.setImageFormat(ImageFormat.getPng());

SheetRender sr = new SheetRender(ws, opts);

//Print the number of pages in this sheet
System.out.println(sr.getPageCount());

Hi,

Thanks for using Aspose.Cells.

Please also consider this option.

Please try the API:
setDesiredSize(int desiredWidth, int desiredHeight) in class ImageOrPrintOptions to set the desired width and height to be the
width and height of the screen.

Hi Shakeel,

Thank you for your help. The second solution worked for me.

Hi,

Thanks for your valuable feedback and using Aspose.Cells.

It is good to know that second solution worked for you. We are hopeful, this solution will also be helpful for other users who are working on the similar problems. Let us know if you encounter any other issue, we will be glad to look into it and help you further.

Hi Shakeel,
I have one problem. The image is not showing grids. I used the following property to enable gridline.

Worksheet sheet = workbook.getWorksheets().get(i);
sheet.setGridlinesVisible(true);

But the image is still not displaying the gridlines after
setting this property.Is there any other way by which i can
generate image with gridline.

Thank you,
Pratibha

Hi,

Thanks for your posting and using Aspose.Cells.

Please change your code like this and see if it works for you.

Java
Worksheet sheet = workbook.getWorksheets().get(i);
sheet.getPageSetup().setPrintGridlines(true);