Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Wed Jan 08, 2020 9:18 am

Hello,
i try to evaluate spire.xls regarding generating an Excelfile on the server
and send it direct (without saving the file on the server)
as Excel, Pdf or Html to the browser.
The export to pdf is working, the export to Excel and Html is not working.
What did i do wrong?

Code: Select all
using System;
using Microsoft.AspNetCore.Mvc;
using Spire.Xls;
using System.IO;

namespace WebApplication2.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ExportExcelController : ControllerBase
    {
 
        // GET: api/ExportExcel/1
        [HttpGet("{id}", Name = "Get")]
        public IActionResult Get(int id)
        {
            string filename;
            MemoryStream documentStream;
            documentStream = new MemoryStream();

            //*****************create Excelworkbook **********************
             Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Range["A1"].Text = "Hello,World!";

            switch (id)
            {
                case 1: //export Excel => not working
                    workbook.SaveToStream(documentStream, FileFormat.Version2016); //
                    filename = $"test9.xlsx";
                    Response.Headers["Content-Disposition"] = $"inline = true; filename={filename}";
                    return File(documentStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                case 2: // export pdf => working
                    filename = $"test7.pdf"; // neccesary?
                    Response.Headers["Content-Disposition"] = $"inline = true; filename={filename}";// neccesary?

                    workbook.SaveToStream(documentStream, FileFormat.PDF);
                    return File(documentStream, "application /pdf");

                case 3: // export html =>  not working
                    filename = $"test7.html"; // neccesary?
                    Response.Headers["Content-Disposition"] = $"inline = true; filename={filename}";// neccesary?
                    workbook.Worksheets[0].SaveToHtml(documentStream);
                    return File(documentStream, "text/html", filename);
                default:

                    return null;
            }

        }


    }
}



idvag.vk
 
Posts: 3
Joined: Wed Jan 08, 2020 7:59 am

Wed Jan 08, 2020 10:08 am

Hi,

Thanks for your inquiry.
After an initial testing with the latest Spire.XLS Pack(Hotfix) Version:10.1.1, I didn't find the issue you mentioned. Please firstly try to use the version I mentioned. Besides, you could save the stream to file directly to check if the generated file is correct. For example:
Code: Select all
File.WriteAllBytes(@"result.xlsx", documentStream.ToArray());

If the result file is correct, I am afraid the issue is not related to our product.

If you still have the issue, please provide a sample project for showing your issue. You could upload it here or send to us(support@e-iceblue.com) via email.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Jan 09, 2020 7:49 am

hi, thank you for yor fast reply.

i updated spire.xls to the current version.
File.WriteAllBytes.. is working

I created a ms visual-studio asp.netcore Angular app via the vs template.
I only added the controller (see sourcecode in zip) .
After launching the progarmm, i simply called the controller per "..<port>/api/<controllerName>/<index>"

If i call export xlsx (index 1) , i see in the browser (in the download area left) the excel-Icon, but the chrome debugger says 'pending'.
The browser seems to wait , that the server finishes the download. (?)
The same happens, if i try to export the exile als html.
What's to do?
Attachments
ExportExcelController.zip
(934 Bytes) Downloaded 231 times

idvag.vk
 
Posts: 3
Joined: Wed Jan 08, 2020 7:59 am

Thu Jan 09, 2020 10:06 am

Hi,

Thanks for your information.
The issue is not related to our product, since the code "File.WriteAllBytes" could work.
If I used your full code, there will be an error in browser but it is not the same as you mentioned. After further investigation, I found the code "sheetms.ToArray()" could save the error on my side. Please try to use it.
Code: Select all
//use byte
return File(sheetms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

If this doesn't work, I suggest you check the return File code or the issue with browser.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Jan 09, 2020 11:09 am

hi ,
thank you very much for your suggestion.
now it works!

idvag.vk
 
Posts: 3
Joined: Wed Jan 08, 2020 7:59 am

Fri Jan 10, 2020 1:33 am

Hi,

Glad to hear the issue is solved.
Any question, please feel free to contact us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.XLS

cron