简体   繁体   中英

Corrupt Excel/spreadsheet file on export .Net Core Open XML

Corrupt Excel/spreadsheet file on export .Net Core Open XML

The exported spreadsheet file from the below code seems to be corrupted. I am using a .Net Core API to return the file with the following test code. Can anybody tell me what am I doing wrong?

Thanks in advance!

[Route("api/tools")]
public class ImportExportController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public ImportExportController(IHostingEnvironment hostingEnvironment)
    {
        this._hostingEnvironment = hostingEnvironment;
    }
    [HttpGet]
    [Route("export")]
    public async Task<IActionResult> Export()
    {
        string sWebRootFolder = this._hostingEnvironment.WebRootPath;
        string sFileName = @"demo.xls";
        string URL = string.Format("{0}://{1}/{2}", this.Request.Scheme, this.Request.Host, sFileName);
        FileInfo filePath = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
        var memory = new MemoryStream();

        SpreadsheetManager.SpreadsheetGenerator(filePath.FullName, sFileName);

        using (var stream = new FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Open))
        {
            await stream.CopyToAsync(memory);
        }

        memory.Position = 0;

        return this.File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", sFileName);
    }
}

This is the code for the SpreadsheetManager

public static class SpreadsheetManager
{
    public static void SpreadsheetGenerator(string filePath, string fileName)
    {
        if (File.Exists(filePath))
        {
            File.Delete(filePath);
        }

        SpreadsheetDocument document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook);

        using (document)
        {
           WorkbookPart workbookPart = document.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();

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

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

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

            sheets.Append(sheet);

            workbookPart.Workbook.Save();

        }
    }
}

Cross posting answer from comments on question:

OpenXML is very easy to get wrong, and neither the tooling or Office will help you when you do.

To that end, the official documentation for creating documents with OpenXML is to create the file you want (in this case with Excel) then open it with the OpenXML Productivity Tool and extract the C# code which generates that file and use that.

When you are ready to move on to more complex OpenXML documents, it will be worth reading reading through the OpenXML documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM