我一直在尝试使用 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);
}
传入的模型看起来不错,它的数据表中填充了正确的数据。