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;
}
}
}
}