Unable to use Aspose.Cells API in SP2010 EventReceiver project

Hi,

We could not directly access the file for the issue of FileIOPermission.

And please set CellsHelper.FontDir property, otherwise we could not get the font settings.

Please try the following code:

private void ConvertFile(SPFile sourceFile, SPFolder destFolder, string destFileName, SaveFormat destFileFormat, bool overwrite)
{

try
{

Stream inputStream = sourceFile.OpenBinaryStream();

Stream outputStream = new MemoryStream();

string sourceExt = Path.GetExtension(sourceFile.Name).ToLower();


try
{

     Workbook book = <span class="kwrd">new</span> Workbook(inputStream);<br>
     book.Save(outputStream, SaveFormat.Pdf);<br>
  }<br>      <span class="kwrd">catch</span> (Exception e)<br>      {<br>
     <span class="kwrd">throw</span> <span class="kwrd">new</span> ApplicationException("Convertion Failed. " + e.Message, e);<br>

  }<br>
  outputStream.Seek(0, SeekOrigin.Begin);<br>
  destFolder.Files.Add(destFolder.Url + "/" + destFileName, outputStream, overwrite);<br>

}
catch (ArgumentException)
{
throw;

}
catch
{
throw;
}


}


BTW, you can try Aspose.Cells for SharePoint :
( http://www.aspose.com/community/files/73/sharepoint-components/aspose.cells-for-sharepoint/entry349440.aspx )



I tried your code but I am getting the following error:

[System.IO.FileLoadException] = {“Could not load file or assembly ‘Aspose.Cells, Version=7.1.2.7, Culture=neutral, PublicKeyToken=716fcc553a201e56’ or one of its dependencies. An operation is not legal in the current state. (Exception from HRESULT: 0x80131509)”:"Aspose.Cells, Version=7.1…

See image attached to this post.

Do you have any suggestions on why this is not working? Could you provide clear instructions on how to use Aspose.Cells dll with SharePoint 2010? Is adding a project reference enough to be able to use the API or are there any additional steps required??

Thanks!


Hi,

Just adding a reference should be ok, there is no dependency of Aspose.Cells for .NET.

I have already logged your issue in our database and forwarded your issue to development team.

Now, I have added your comment/feedback against the issue id: SHAREPOINT-40008

We will get back to you asap.

Hi,

Please register the DLL in GAC.

I registered Aspose DLL in GAC and the line in blue does not generate errors anymore.

However, I am getting ‘Null reference’ error on the line in red below:

Aspose.Cells.License license = new Aspose.Cells.License();
string strLicenseFile = @"\servername01\inetpub\asposelic\Aspose.Cells.lic";
//string strLicenseFile = “C:\Projects\EventReceiverSolution\Aspose\Aspose.Cells.lic”; // same error
license.SetLicense(strLicenseFile);

Any suggestions on how to fix this? The lic file is located on the web server and on C: and the paths should work…

If there is another way to reference the license file, please provide me with some clear instructions.


Thanks!


Hi,

Please try providing a physical path not a mapped or virtual path.

So, if your license is in C:\myFolder and license file name is Aspose.Cells.lic, then your code will look like this.

Make sure, you have a read access to C:\myFolder\Aspose.Cells.lic file.

C#


license.SetLicense(@“C:\myFolder\Aspose.Cells.lic”);

Hi,

a) Please use the latest fix: Aspose.Cells
for .NET v7.2.0.4



b) Please use License.SetLicense(stream ) method.

I am able to open the license file now, but I am not able to open my Excel workbook using Aspose.Cells.

I am trying to convert XLS to PDF in SharePoint 2010. Here is my code (line in red causes error):

fileAsposeLicense = properties.Web.GetFile(“CM/Aspose.Cells.lic”);
if (fileAsposeLicense.Exists)
{
byte[] data = fileAsposeLicense.OpenBinary();
MemoryStream licenseStream = new MemoryStream(data, 0, data.Length);
licenseStream.Seek(0, SeekOrigin.Begin);
Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense(licenseStream);
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(file.Name);
wb.Save(file.Name.Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);
licenseStream.Close();
}

Error: "Request for the permission of type 'System.Security.Permissions.FileIOPermission failed"

I changed the trust level to ‘Full’ in my config.web but it did not fix the issue.

Please let me know how this IO error can be resolved. Thanks!


Hi,


I think you may try to load the Excel file in the same way (using the similar code/apis) as you have done with license file which works fine, it should also work fine on your SP2010 environment.
e.g

fileExcel = properties.Web.GetFile(“CM/MyFile.xls”);
byte[] filedata = fileExcel.OpenBinary();
MemoryStream fileStream = new MemoryStream(filedata, 0, filedata.Length);
fileStream.Seek(0, SeekOrigin.Begin);
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(fileStream);

Thanks. That worked. However, I still get the same IO error when trying to convert XLS to PDF:

Request for the permission of type ‘System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

Here is the line that produces the error:

wb.Save(file.Name.Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);

Do you have any suggestions on how to resolve this problem? Thanks!






Hi,

Replace this line with the following code and let me know if it works fine now.

wb.Save(file.Name.Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);

C#


Stream outputStream = new MemoryStream();

wb.Save(ms, SaveFormat.Pdf);


outputStream.Seek(0, SeekOrigin.Begin);

destFolder.Files.Add(destFolder.Url + “/” + file.Name + “.out.pdf”, outputStream, overwrite);



What is ‘ms’ in the following line: wb.Save(ms, SaveFormat.Pdf);

Should it be replaced with outputStream ??


Thanks!


Can this ‘save’ code be used in a sandboxed solution?

developer2010:
What is 'ms' in the following line: wb.Save(ms, SaveFormat.Pdf);

Should it be replaced with outputStream ??


Thanks!

Hi,

Yes, it is typo, you should use outputstream actually.

The SAVE method in your code throws a ‘null reference’ error. Please take a look at the attached image and let me know how to resolve this problem. Thanks!

FYI: I have a sandboxed solution. Here is the code:

SPList list = properties.List;
SPListItem item = properties.ListItem;
SPFile file = properties.ListItem.File;
SPFile fileAsposeLicense;
SPFolder libraryFolder = item.Folder;

fileAsposeLicense = properties.Web.GetFile(“Aspose.Cells.lic”);

if (fileAsposeLicense.Exists)
{
byte[] data = fileAsposeLicense.OpenBinary();
MemoryStream licenseStream = new MemoryStream(data, 0, data.Length);
licenseStream.Seek(0, SeekOrigin.Begin);

Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense(licenseStream);

byte[] filedata = file.OpenBinary();
MemoryStream fileStream = new MemoryStream(filedata, 0, filedata.Length);
fileStream.Seek(0, SeekOrigin.Begin);
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(fileStream);

Stream outputStream = new MemoryStream();
wb.Save(outputStream, SaveFormat.Pdf); // this line fails

///outputStream.Seek(0, SeekOrigin.Begin);
///libraryFolder.Files.Add(libraryFolder.Url + “/” + file.Name + “.out.pdf”, outputStream, true);

///Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(file.Name);
///wb.Save((file.Name.Replace(".xlsx", “.pdf”)).Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);

licenseStream.Close();
}

return;







Hi,

Could you please try some other xls/xlsx file? It could be possible that Aspose.Cells crashes because of some bug.

Could you please attach your xls/xlsx file which is failing?

However, you can run the same code but with the following modification so that I know that exception is not occurring because of source xls/xlsx file?

Change your following code


//Here you are creating workbook from source file

Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(fileStream);


into
//Now create workbook from scratch and see the code runs or not
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();

Hi,

We have looked into your issue and screenshot.

We found that the exception is caused because we could not find font setting files.

Please add them to your project by setting CellsHelper.FontDir() method.

Hi.

What should I set CellsHelper.FontDir to other than default? This really looks like a bug in your software b/c I should not be required to know and/or hard code the location of the system fonts on the web server in order to be able to convert from Excel to PDF.

In any case, I tried setting CellsHelper.FontDir as follows:

CellsHelper.FontDir = @“C:\Windows\Fonts”;
(that’s where all my font files are)

But I am still getting the following error on calling the Save method:
[Aspose.Cells.CellsException] = {“Object reference not set to an instance of an object.”}

(I also created and tried converting a new XLS file but still get the same error)

Any suggestions on how to finally resolve this problem?

Also, do you know whether your PDF conversion works with sandboxed solutions?



Thanks!

Hi,

I have forwarded your question to development team and logged your comments against the issue id: SHAREPOINT-40008

We will look into your issue soon and give you advice.

Did you try my other suggestion in which I said, create a workbook object from scratch and see if it works fine or still throws exception? It will help me sort out your issue.

Hi,

In the a medium trust environment, the application only can access the file in the current application directory.

If you set CellsHelper.FontDir = @“C:\Windows\Fonts”, an inner FileIOPermission exception will be caught in Aspose.Cells.

If you want to open and save the file, please check whether the file could be accessed by File.Open and File.Create without calling Aspose.Cells.

We created a sample web project in the attached zip for medium trust environment. Please check it . We hope it could help you.