Suprateem Bose
Suprateem Bose

Reputation: 69

Download Excel file from a MemoryStream in C#

I'm having a problem downloading a Excel file in browser using React.js.

Here is the situation:

I have written an API which will take an Excel file as input , then after modifying it using Aspose Cells I have saved it as a memorystream.Then converted it as a Byte[]. Now sending back the byte[] to the client side, then converting it as a Blob to trigger a download in browser. An excel file is downloading but when opening it its showing corrupted. can anyone help me out ?

Here is my server side api code

public async Task<Byte[]> ExportToExcel(IFormFile file)
    {
        if (file == null || file.Length == 0)
        {
            return null;
        }
        try
        {
            using (var stream = file.OpenReadStream())
            {
                Workbook workbook = new Workbook(stream);
                var worksheet = workbook.Worksheets[0];
                Cells cells = worksheet.Cells;

                FindOptions findOptions = new FindOptions();
                findOptions.CaseSensitive = false;
                findOptions.LookInType = LookInType.Values;
                findOptions.LookAtType = LookAtType.Contains;

                // Doing modifications in Excel file....

                // Save the modified workbook as Excel file
                var excelMemoryStream = new MemoryStream();
                workbook.Save(excelMemoryStream, SaveFormat.Xlsx);
                byte[] data = excelMemoryStream.ToArray();

                return data;
                

            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }

and this is the client side code of React js

const onBtExport = () => {
const csvData = gridRef.current.api.getDataAsExcel();
console.log(csvData);
instance
  .acquireTokenSilent({
    ...accessToken,
    account: accounts[0],
  })
  .then(async (response) => {
    const formData = new FormData();
    formData.append('file', csvData);
    ExportToExcel(response.accessToken, formData).then((res) => {
      if (res.data.Status === true) {
        console.log(res.data.Data);
        const blob = new Blob([res.data.Data],{ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
        console.log(blob);
        // console.log(blob);
        const a = document.createElement('a');
        a.href = URL.createObjectURL(blob);
        a.download = 'ContractSummaryReport.xlsx';
        a.click();

        URL.revokeObjectURL(a.href);
        
      } else {
        toast.error(res.data.Message);
        
      }
    });
  });

};

Upvotes: -1

Views: 890

Answers (1)

Amjad Sahi
Amjad Sahi

Reputation: 1931

I am not sure whether the issue is with Aspose.Cells or on your client-end. To isolate the issue, you may try:

  • Save the "data" to local file on the server and then check whether the local file can be opened by MS Excel without problem. If not, it means there are some invalid data in the generated Excel file via Aspose.Cells and so, we need template Excel file and runnable sample code to reproduce the issue and to fix it.

If yes, then the issue should be in the process at client side, so you may try:

  • Remove all invocations to APIs of Aspose.Cells, instead just reading the template file as byte[] and send the byte[] to client to check whether the received file can still be opened by MS Excel.

This way, you can evaluate your issue thoroughly and figure it out on your end accordingly.

You may also post your queries or discuss further in the dedicated forum.

PS. I am working as Support developer/ Evangelist at Aspose.

Upvotes: 0

Related Questions