0

我一直在尝试使用 Gembox 将一些信息从我的 Web 应用程序导出到 Excel 电子表格中。

调试时,我可以逐步完成整个 SaveExcelFile 方法,但似乎没有任何反应,没有下载文件,而且似乎没有错误指出我解决问题的正确方向。

private void SaveExcelFile(ExcelDataModel excelDataModel)
    {            
        var workbook = new ExcelFile();
        ExcelWorksheet employeesWorksheet = workbook.Worksheets.Add("Employees");
        ExcelWorksheet contractsWorksheet = workbook.Worksheets.Add("Contracts");

        employeesWorksheet.Cells["A1"].Value = "Employees:";
        employeesWorksheet.InsertDataTable(excelDataModel.EmployeeDatatable,
            new InsertDataTableOptions
            {
                ColumnHeaders = true,
                StartRow = 3
            });

        contractsWorksheet.Cells["A1"].Value = "Contracts:";
        contractsWorksheet.InsertDataTable(excelDataModel.ContractDatatable,
            new InsertDataTableOptions
            {
                ColumnHeaders = true,
                StartRow = 3
            });

        workbook.Save("Employees.xls");
    }

传入的模型仅包含两个 DataTable:

    public class ExcelDataModel
{
    public DataTable EmployeeDatatable { get; set; }
    public DataTable ContractDatatable { get; set; }

}

我从以下位置调用我的 SaveExcelFile 方法:

public async Task<ActionResult> ExportEmployees()
    {
        SpreadsheetInfo.SetLicense(ConfigurationManager.AppSettings["GemBoxSpreadsheetSerialKey"]);

        var response = await **I have removed the call/url but this just calls a controller method on the api**;
        if (response.IsSuccessStatusCode)
        {
            var data = response.Content.ReadAsStringAsync().Result;
            var model = JsonConvert.DeserializeObject<ExcelDataModel>(data);

            SaveExcelFile(model);
            return Json(new { success = true, message = "Successfully exported employees" }, JsonRequestBehavior.AllowGet);
        }

        return Json(new { success = false, message = "Failed to export employees to excel" }, JsonRequestBehavior.AllowGet);
    }

传入的模型看起来不错,它的数据表中填充了正确的数据。

4

1 回答 1

0

workbook.Save("Employees.xls")方法会将文件保存到指定的路径,如果是相对路径(如您所拥有的),它将相对于当前工作目录保存。

要下载您需要使用的文件,FileResult而不是JsonResult,如ASP.NET Core 示例中所示。

// Changed "void" to "MemoryStream" and added "SaveOptions".
private MemoryStream SaveExcelFile(ExcelDataModel excelDataModel, SaveOptions options)
{
    // ...

    // Changed saving from path to stream.
    var stream = new MemoryStream();
    workbook.Save(stream, options);
    return stream;
}

public async Task<ActionResult> ExportEmployees()
{
    // ...

    if (response.IsSuccessStatusCode)
    {
        var data = response.Content.ReadAsStringAsync().Result;
        var model = JsonConvert.DeserializeObject<ExcelDataModel>(data);

        // Changed "JsonResult" to "FileResult".
        var excelOptions = SaveOptions.XlsDefault;
        using (var excelStream = SaveExcelFile(model, excelOptions))
            return File(excelStream.ToArray(), excelOptions.ContentType, "Employees.xls");
    }

    return Json(new { success = false, message = "Failed to export employees to excel" }, JsonRequestBehavior.AllowGet);
}
于 2020-11-13T06:45:14.090 回答