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();//是一个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 }); }视图里面改、该如何写
时间: 2023-05-17 21:03:30 浏览: 160
对于这个问题,你可以在视图中使用 Razor 语法来输出文件流,如下所示:
@model FileViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@Model.FileName</title>
</head>
<body>
<a href="@Url.Action("ExportToExcel")">Download Excel</a>
</body>
</html>
在这个视图中,我们使用了 @model 指令来指定视图的模型类型为 FileViewModel。然后,我们使用 Razor 语法来输出文件流,如下所示:
<a href="@Url.Action("ExportToExcel")">Download Excel</a>
这个链接会调用 ExportToExcel 方法,并返回一个文件流。在这个方法中,我们创建了一个 ExcelPackage 对象,并向其中添加了一个工作表。然后,我们向工作表中添加了表头和数据,并自适应列宽。最后,我们将 Excel 文件导出为一个文件流,并将其包装在一个 FileViewModel 对象中,以便在视图中使用。
相关问题
public interface IMomentdataEntity { double UtcTime { get; set; } double Velocity { get; set; } double Longitude { get; set; } double Latitude { get; set; } double SteeringWheelAngle { get; set; } double YawVelocity { get; set; } double VehicleRollAngle { get; set; } double LateralAcceleration { get; set; } public bool IsUP { get; set; } public bool IsDown { get; set; } public bool IsStar { get; set; } public bool IsStop { get; set; } public bool IsReSet { get; set; } }的深拷贝函数
您好,根据您提供的代码,可以编写一个 IMomentdataEntity 接口的深拷贝函数示例:
```csharp
public static T DeepCopy<T>(T obj)
{
if (obj == null) return default(T);
var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (var stream = new System.IO.MemoryStream())
{
serializer.Serialize(stream, obj);
stream.Seek(0, System.IO.SeekOrigin.Begin);
return (T)serializer.Deserialize(stream);
}
}
```
使用示例:
```csharp
IMomentdataEntity momentData = new MomentdataEntity(); // 假设 MomentdataEntity 类实现了 IMomentdataEntity 接口
// 设置 momentData 的属性值...
IMomentdataEntity momentDataCopy = DeepCopy(momentData);
```
注意,使用深拷贝函数需要保证 IMomentdataEntity 实现类是可序列化的。如果 IMomentdataEntity 实现类包含了不可序列化的属性,例如委托类型,将会导致深拷贝失败。
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 });
}
阅读全文