GemBox.Spreadsheet 保存方法没有做任何事情,也没有错误
GemBox.Spreadsheet Save method is not doing anything and no errors
我一直在尝试使用 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");
}
传入的模型仅包含两个数据表:
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);
}
传入的模型看起来不错,它的数据表中填充了正确的数据。
workbook.Save("Employees.xls")
方法会将文件保存到指定路径,如果是相对路径(就像你那里的路径),它将相对于当前工作目录保存。
所示,您需要使用 FileResult
而不是 JsonResult
下载文件
// 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);
}
我一直在尝试使用 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");
}
传入的模型仅包含两个数据表:
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);
}
传入的模型看起来不错,它的数据表中填充了正确的数据。
workbook.Save("Employees.xls")
方法会将文件保存到指定路径,如果是相对路径(就像你那里的路径),它将相对于当前工作目录保存。
FileResult
而不是 JsonResult
下载文件
// 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);
}