0

I have a problem in C# when i try to generate new excel file with 4 values. When i try to open file i receive an error telling me : excel cannot open the file because the file format or file extension is not valid

When i try to debug it i have noticed that output is trowing : System invalid operation exception and i don't know how to fix this. i just want to generate excel file that has some values inside (later i will fill insert my values). For now i am just trying to create excel file that isn't corrupted.

    [HttpPost]
    public HttpResponseMessage PrintDevices()
    {

        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        using (MemoryStream output = new MemoryStream())
        {


            using (SpreadsheetDocument document = SpreadsheetDocument.Create(output, SpreadsheetDocumentType.Workbook))
            {
                WorkbookPart workbookPart = document.AddWorkbookPart();
                workbookPart.Workbook = new Workbook();

                WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
                worksheetPart.Worksheet = new Worksheet();

                Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

                Sheet sheet = new Sheet()
                {
                    Id = workbookPart.GetIdOfPart(worksheetPart),
                    SheetId = 1,
                    Name = "Testing"
                };

                sheets.Append(sheet);

                workbookPart.Workbook.Save();


                SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());

                // Constructing header
                Row row = new Row();

                row.Append(
                    ConstructCell("Id", CellValues.String),
                    ConstructCell("Name", CellValues.String),
                    ConstructCell("Birth Date", CellValues.String),
                    ConstructCell("Salary", CellValues.String));

                // Insert the header row to the Sheet Data
                sheetData.AppendChild(row);

                // Inserting each employee


                worksheetPart.Worksheet.Save();
            }
        }

         Cell ConstructCell(string value, CellValues dataType)
        {
            return new Cell()
            {
                CellValue = new CellValue(value),
                DataType = new EnumValue<CellValues>(dataType)
            };
        }

        return response;
    }
  • On what line is the exception being thrown? – Sudsy1002 Mar 20 '18 at 14:10
  • 1
    I don't see any line *writing* to the response stream. Nothing sets the content type headers either – Panagiotis Kanavos Mar 20 '18 at 14:12
  • I'd suggest you use a library like EPPlus to make Excel generation easier. Almost all of this code could be eliminated. You could even create a custom EppResult [as shown here](https://stackoverflow.com/questions/32837808/how-do-i-export-to-excel/32838151#32838151) for Web API and MVC applications to avoid buffering the file into memory – Panagiotis Kanavos Mar 20 '18 at 14:16
  • MemoryStream output is throwing exception. Line 6 – Zexxxxxxx Mar 20 '18 at 14:22
  • @PanagiotisKanavos i would love to use the EPPlus but i cannot since my project manager doesn't allow it(don't ask why). I am trying just to make this code to generate empty non corrupt excel file. – Zexxxxxxx Mar 20 '18 at 14:26

0 Answers0