ASP NET CORE MVC - 如何将数据绑定到电子表格
ASP NET CORE MVC - How to binding data to spreadsheet
如何使用列表对象中工作表上的数据创建电子表格?
我使用 devexpress asp net core 创建电子表格。
这是我的控制器:
public ActionResult Overview(IFormFile document)
{
var model = new DocumentModel();
if (document != null)
{
//SaveFile(document);
var stream = new MemoryStream();
document.CopyTo(stream);
//Office File API
var workBook = new Workbook();
workBook.LoadDocument(stream);
//preprocess the uploaded file using Spreadsheet Document API
model.DocumentId = document.FileName;
model.ContentAccessorByStream = stream;
}
return View("Overview", model);
}
如何将列表传递到电子表格?
您可以尝试将包含数据的列表导入到工作表中:
// Import the list into the worksheet.
// Data starts with the B3 cell.
workbook.Worksheets[0].Import(yourList, 2, 1);
...或使用数据绑定:
private void BindToRange(List<MyObject> dataSource, CellRange
bindingRange, Worksheet sheet) {
// Specify the binding options.
ExternalDataSourceOptions dsOptions = new ExternalDataSourceOptions();
dsOptions.ImportHeaders = true;
dsOptions.CellValueConverter = new MyObjectConverter();
dsOptions.SkipHiddenRows = true;
// Bind the data source to the worksheet range.
WorksheetDataBinding sheetDataBinding = sheet.DataBindings.BindToDataSource(dataSource, bindingRange, dsOptions);
// Adjust the column width.
sheetDataBinding.Range.AutoFitColumns();
}
请参阅以下文章以了解更多信息:
如何使用列表对象中工作表上的数据创建电子表格? 我使用 devexpress asp net core 创建电子表格。
这是我的控制器:
public ActionResult Overview(IFormFile document)
{
var model = new DocumentModel();
if (document != null)
{
//SaveFile(document);
var stream = new MemoryStream();
document.CopyTo(stream);
//Office File API
var workBook = new Workbook();
workBook.LoadDocument(stream);
//preprocess the uploaded file using Spreadsheet Document API
model.DocumentId = document.FileName;
model.ContentAccessorByStream = stream;
}
return View("Overview", model);
}
如何将列表传递到电子表格?
您可以尝试将包含数据的列表导入到工作表中:
// Import the list into the worksheet.
// Data starts with the B3 cell.
workbook.Worksheets[0].Import(yourList, 2, 1);
...或使用数据绑定:
private void BindToRange(List<MyObject> dataSource, CellRange
bindingRange, Worksheet sheet) {
// Specify the binding options.
ExternalDataSourceOptions dsOptions = new ExternalDataSourceOptions();
dsOptions.ImportHeaders = true;
dsOptions.CellValueConverter = new MyObjectConverter();
dsOptions.SkipHiddenRows = true;
// Bind the data source to the worksheet range.
WorksheetDataBinding sheetDataBinding = sheet.DataBindings.BindToDataSource(dataSource, bindingRange, dsOptions);
// Adjust the column width.
sheetDataBinding.Range.AutoFitColumns();
}
请参阅以下文章以了解更多信息: