public ActionResult ExportToExcel() { var package = new ExcelPackage();//是一个epplus中的类 var worksheet = package.Workbook.Worksheets.Add("Sheet1");//Workbook 是 Excel 工作簿,Worksheets 是工作簿中的工作表集合 // 添加表头 cell代表表格或单元格 worksheet.Cells[1, 1].Value = "姓名"; worksheet.Cells[1, 2].Value = "年龄"; worksheet.Cells[1, 3].Value = "性别"; // 添加数据 worksheet.Cells[2, 1].Value = "张三"; worksheet.Cells[2, 2].Value = 20; worksheet.Cells[2, 3].Value = "男"; worksheet.Cells[3, 1].Value = "李四"; worksheet.Cells[3, 2].Value = 25; worksheet.Cells[3, 3].Value = "女"; // 自适应列宽 worksheet.Cells.AutoFitColumns(); // 导出 Excel 文件 var fileStream = new MemoryStream(package.GetAsByteArray()); var fileName = "export.xlsx"; var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; return View(new FileViewModel { FileStream = fileStream, ContentType = contentType, FileName = fileName }); }@model该怎么写创建视图
时间: 2023-05-17 15:03:09 浏览: 86
您可以使用以下代码来创建视图模型:
public class FileViewModel
{
public Stream FileStream { get; set; }
public string ContentType { get; set; }
public string FileName { get; set; }
}
然后,在您的视图中,您可以使用以下代码来声明模型:
@model FileViewModel
接下来,您可以使用以下代码来访问模型中的属性:
@Model.FileName
@Model.ContentType
@Model.FileStream
请注意,这只是一个简单的示例,您可能需要根据您的需求进行更改。
相关问题
public ActionResult ExportToExcel() { var package = new ExcelPackage();//是一个epplus中的类 var worksheet = package.Workbook.Worksheets.Add("Sheet1");//Workbook 是 Excel 工作簿,Worksheets 是工作簿中的工作表集合 // 添加表头 cell代表表格或单元格 worksheet.Cells[1, 1].Value = "姓名"; worksheet.Cells[1, 2].Value = "年龄"; worksheet.Cells[1, 3].Value = "性别"; // 添加数据 worksheet.Cells[2, 1].Value = "张三"; worksheet.Cells[2, 2].Value = 20; worksheet.Cells[2, 3].Value = "男"; worksheet.Cells[3, 1].Value = "李四"; worksheet.Cells[3, 2].Value = 25; worksheet.Cells[3, 3].Value = "女"; // 自适应列宽 worksheet.Cells.AutoFitColumns(); // 导出 Excel 文件 var fileStream = new MemoryStream(package.GetAsByteArray()); var fileName = "export.xlsx"; var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; return View(new FileViewModel { FileStream = fileStream, ContentType = contentType, FileName = fileName }); }@model该怎么写
您可以这样写:
public class FileViewModel
{
public MemoryStream FileStream { get; set; }
public string ContentType { get; set; }
public string FileName { get; set; }
}
public ActionResult ExportToExcel()
{
var package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells[1, 1].Value = "姓名";
worksheet.Cells[1, 2].Value = "年龄";
worksheet.Cells[1, 3].Value = "性别";
worksheet.Cells[2, 1].Value = "张三";
worksheet.Cells[2, 2].Value = 20;
worksheet.Cells[2, 3].Value = "男";
worksheet.Cells[3, 1].Value = "李四";
worksheet.Cells[3, 2].Value = 25;
worksheet.Cells[3, 3].Value = "女";
worksheet.Cells.AutoFitColumns();
var fileStream = new MemoryStream(package.GetAsByteArray());
var fileName = "export.xlsx";
var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
return View(new FileViewModel { FileStream = fileStream, ContentType = contentType, FileName = fileName });
}
public class ExportToExcelController : Controller { // GET: ExportToExcel public ActionResult ExportToExcel() { //存储在数据表对象中 DataTable dt = new DataTable(); // 创建对象 ExcelPackage excelPackage = new ExcelPackage(); // 将表添加到对象 ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet1"); //填充数据 //worksheet.Cells["A1"].LoadFromDataTable(dt, true); // 转换字节组 byte[] fileContents = excelPackage.GetAsByteArray(); // 返回 return File(fileContents, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "filename.xlsx"); }如何添加数据
可以使用以下代码将数据添加到 DataTable 中:
dt.Columns.Add("列名1");
dt.Columns.Add("列名2");
dt.Rows.Add("数据1", "数据2");
其中,"列名1"和"列名2"是列的名称,"数据1"和"数据2"是要添加的数据。可以根据需要添加多个列和行。添加完数据后,可以使用以下代码将数据表中的数据填充到 Excel 工作表中:
worksheet.Cells["A1"].LoadFromDataTable(dt, true);
其中,"A1"是要填充数据的单元格的位置,"true"表示第一行是列名。最后,将 Excel 工作簿转换为字节数组并返回即可。
阅读全文