0

I'm trying to whrite a file in the system, in azure but I get one error I can't figure out. The code works on my enviroment, but not on the zure.

I tried my code in different enviroments, they all work except for azure.

Below is my code.

    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        await _dlsService.Init();
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        log.LogInformation("Processing request: " + requestBody);
        CDTO cFile = JsonConvert.DeserializeObject<CDTO>(requestBody);

        string filePath = $"{cFile.DataLakeFolder}/{cFile.FileName}";
        var summary = _dlsService.GetMetadata(filePath);
        //string csvFileName = @"";

        try
        {
            string pathToFile = $"{cFile.DataLakeFolder}/x.xlsx";

            using (var str = await _dlsService.CreateFile(pathToFile))
            {

                using (var streamReader =  await _dlsService.OpenStream(filePath))
                {
                    StreamReader sr = new StreamReader(streamReader);
                    var parser = new CsvParser(sr);

                    //get the first row of csv as the header 
                    var header = parser.Read();
                    if (header == null)
                        return null;
                    var columns = header.ToList();
                    //check if there are empty columns
                    columns.RemoveAll(o => string.IsNullOrWhiteSpace(o));
                    int i = 1;
                    using (var report = new Application.Helpers.ExcelReportHelper(str)) // **this line gives me error** 
                    {
                        report.WriteHeader(columns);
                        if (i >= 1)
                        {
                            //if (parser.read == null) break, else continue to write the excel
                            //..work work work ...

                        }
                        report.Save();
                    }
                }
            }
            return new ObjectResult(new { fileCreated = pathToFile });
        }
        catch (Exception ex)
        {
            log.LogInformation("Processing request: " + ex.Message);
            return ErrorHandler.HandleException(ex, log);
        }
  }

than my excelhelper class is :

class ExcelReportHelper : IDisposable
{
    private ExcelPackage _package;
    private string _currentQueryName;

    public ExcelReportHelper(Stream destStream)
    {
        _package = new ExcelPackage(destStream); // **this is the line that raises the error**
        _package.Workbook.Worksheets.Add("Sheet1");
    }

    //bla bla bla
    //bla bla bla
 }

Any idea on how to approach to this problem?? I would very much need some help. Thanks :)

PS : This is the stack trace :

{System.NotSupportedException: Specified method is not supported.
        at Microsoft.Azure.DataLake.Store.AdlsOutputStream.get_Length()
        at OfficeOpenXml.ExcelPackage..ctor(Stream newStream)
        at Roar.EnrolService.Application.Helpers.ExcelReportHelper..ctor(Stream destStream) in  

C:\Projects\Roar\Roar.Ingestion\Roar.EnrolService\Application\Helpers\ExcelReportHelper.cs:line 16
   at 
 Roar.EnrolService.Functions.FileConversion.FromCsvToExcelFunction.Run(HttpRequest req, ILogger log) in C:\Projects\Roar\Roar.Ingestion\Roar.EnrolService\Functions\FileConversion\FromCsvToExcelFunction.cs:line 66}
Pavel
  • 153
  • 1
  • 4
  • 14
  • Can you show us the full stack trace you get back from the Azure-hosted error? or at very least, a little more info on where (what line) the error is being thrown? – Casey Crookston Sep 09 '19 at 17:28
  • Hello @CaseyCrookston If you look at the code you can clearly see where I have written `//this line raises the error` . Please take a look – Pavel Sep 09 '19 at 17:29
  • :) Ah, fair point. I missed that. Taking a look. – Casey Crookston Sep 09 '19 at 17:32
  • Ok ok. Thanks for the Stack Trace. That is very helpful. So the problem is OfficeOpenXml.ExcelPackage. Some things to check... does Azure support OfficeOpenXml? If so, what version do they support? You may be using a newer version than what Azure supports? Azure is great, but it can be really finicly about stuff like this. I've run into these problems before where they support older versions that what is available. – Casey Crookston Sep 09 '19 at 17:37
  • Another possibility... Maybe try using OpenXML instead of OfficeOpenXML? See this thread: https://social.msdn.microsoft.com/Forums/azure/en-US/831c76b8-d73a-4e04-a054-a57de03a1140/unable-to-create-excel-file-on-windows-azure?forum=windowsazuredevelopment – Casey Crookston Sep 09 '19 at 17:41

1 Answers1

0

Using ExcelPackage requires that you have Excel installed.

Probably it works on your environment since you have Excel, and does not work in Azure (not sure where in Azure you are running the code) since the code does not have access to Excel there.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252