我已经实现了下面的代码,所有的头和数据添加没有任何问题。如果他想阻止编辑用户下载的excel文件中的单个字段或列的可能性,那么就有问题了,因为没有阻止
我用于冻结列/行,但当我导出文件,我打开文件,我可以编辑任何字段worksheet.SheetView.Freeze (1,3);
[HttpGet]
public IActionResult ExportAsExcel()
{
IEnumerable<Employee> employees = this.repo.GetAll<Employee>();
List<EmployeeDTO> employeeDTO = this._mapper.Map<List<EmployeeDTO>>(employees);
using (var workbook = new XLWorkbook())
{
var woorksheet = workbook.Worksheets.Add("Sheet1");
var currentRow = 1;
woorksheet.Cell(currentRow, 1).Value = "ID";
woorksheet.Cell(currentRow, 2).Value = "name";
foreach (var empDtos in employeeDTO)
{
currentRow++;
woorksheet.Cell(currentRow, 1).Value = empDtos.EmployeeId;
woorksheet.Cell(currentRow, 2).Value = empDtos.Name;
}
using (var stream = new MemoryStream())
{
workbook.SaveAs(stream);
var content = stream.ToArray();
return File(
content,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"Employee.xlsx"
);
}
}
}
冻结列/行意味着在滚动时,冻结的区域保持在视图内。
如果你想保护文档不被编辑,我会研究一下表单保护。