Blazor - ClosedXml - excel cannot open the file beacause the file format or extension is not valid

0 votes

I'm attempting to export some information from my Blazor page to an Excel workbook. However, I receive the following error message when I attempt to access the file:

excel cannot open the file because the file format or extension is not valid

When I open the file in notepad++ I get see a bunch of letters and signs but no valid data. 

enter image description here

My code:

 public byte[] SaveSearchToExcel(List<EquipmentForm> EquipmentForms)
    {
        var wb = new XLWorkbook();
  

        var ws = wb.Worksheets.Add("Search Results");
        ws.Cell(1, 1).Value = "TLV Number";
        ws.Cell(1, 2).Value = "Descirption";
        for(int row = 0; row < EquipmentForms.Count; row++)
        {
            ws.Cell(row + 1, 1).Value = EquipmentForms[row].TLVNumber;
            ws.Cell(row+1, 2).Value = EquipmentForms[row].Description;
        }



        MemoryStream XLSStream = new();
        wb.SaveAs(XLSStream);
        XLSStream.Position = 0;
        return XLSStream.ToArray();

    }

  private async void SaveToExcel(){
    var XLSStream= _saveToExcel.SaveSearchToExcel(EquipmentForms);
    
    await js.InvokeVoidAsync("BlazorDownloadFile", "export.xlsx",Convert.ToBase64String(XLSStream));

    }

function BlazorDownloadFile(filename, content) {
// thanks to Geral Barre : https://www.meziantou.net/generating-and-downloading-a-file-in-a-blazor-webassembly-application.htm 

// Create the URL
const file = new File([content], filename, { type: "application/octet-stream" });
const exportUrl = URL.createObjectURL(file);

// Create the <a> element and click on it
const a = document.createElement("a");
document.body.appendChild(a);
a.href = exportUrl;
a.download = filename;
a.target = "_self";
a.click();

// We don't need to keep the object url, let's release the memory
// On Safari it seems you need to comment this line... (please let me know if you know why)
URL.revokeObjectURL(exportUrl);

}

What I'm doing wrong here?

Jan 20, 2023 in Others by Kithuzzz
• 38,020 points
1,391 views

1 answer to this question.

0 votes

Changed the following code:

function BlazorDownloadFile(filename, bytesBase64) {
var link = document.createElement('a');
link.download = filename;
link.href = "data:application/octet-stream;base64," + bytesBase64;
document.body.appendChild(link); // Needed for Firefox
link.click();
document.body.removeChild(link);
answered Jan 20, 2023 by narikkadan
• 63,680 points

Related Questions In Others

0 votes
1 answer

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: (Open Excel File) in Python

Try this: import os import shutil dirpath = os.path.join('C:/Path/Folder', 'Folder') if ...READ MORE

answered Jan 15, 2023 in Others by narikkadan
• 63,680 points
7,765 views
0 votes
1 answer

'npm' is not recognized as internal or external command, operable program or batch file

To your existing lines of code, include ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
11,668 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,174 views
0 votes
1 answer

ImportError: openpyxl is required for loading excel format files

Forget the PsychoPy complications for the time ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,100 points
1,064 views
0 votes
1 answer

In Blue Prism how to split excel column data into TWO columns

This is how I am doing it. Dim ...READ MORE

answered Oct 15, 2018 in RPA by Priyaj
• 58,100 points
4,349 views
0 votes
1 answer

Excel Power Query: Using List.MatchAny on a column value

try this. let TableA = ...READ MORE

answered Oct 22, 2018 in Power BI by Annie97
• 2,160 points
4,224 views
0 votes
1 answer

Excel cannot open the file because the file format or file extension is not valid - PHP Maatwebsite

Try this: if (ob_get_length() == 0 ) { ...READ MORE

answered Dec 19, 2022 in Others by narikkadan
• 63,680 points
1,425 views
0 votes
1 answer

PHPExcel file cannot open file because the file format or file extension is not valid

Just change the code to this: // Save Excel ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,680 points
5,451 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP