mini-software / MiniExcel

Fast, Low-Memory, Easy Excel .NET helper to import/export/template spreadsheet (support Linux, Mac)
https://www.nuget.org/packages/MiniExcel/
Apache License 2.0
2.66k stars 325 forks source link

空白模板导出 #633

Open Sheldon-NULL opened 1 month ago

Sheldon-NULL commented 1 month ago

Excel Type

MiniExcel Version

Version:1.31.3

Description

怎么导出一个只有表头和Sheet名称的空白模板

Sheldon-NULL commented 1 month ago
            string sFileName = $"{fileName}模板.xlsx";

            var memoryStream = new MemoryStream();
            await memoryStream.SaveAsByTemplateAsync( sFileName,t, configuration: new OpenXmlConfiguration()
            {
                //  AutoFilter = true,
                TableStyles = TableStyles.Default,
            });

            memoryStream.Seek(0, SeekOrigin.Begin);

            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = HttpUtility.UrlEncode(sFileName)
            };

我使用上面的代码尝试导出模板,t是直接new出来的对象,里面的属性值都是空的。现在遇到找不到文件的问题。我是想不保存到本地,直接把这个生成的导出模板通过流返回的。

Sheldon-NULL commented 1 month ago

直接用SaveAs会空对象引用的报错

Sheldon-NULL commented 1 month ago

我升级到Version:1.33.0 ` protected async Task DownloadImportTemplateAsync(T t, string fileName) where T : class, new() { string sFileName = $"{fileName}模板.xlsx";

        //  Response.Headers.Add("Access-Control-Expose-Headers", "content-disposition");
        var memoryStream = new MemoryStream();

        await memoryStream.SaveAsAsync(t, configuration: new OpenXmlConfiguration()
        {
            //  AutoFilter = true,
            TableStyles = TableStyles.Default,
        });

        memoryStream.Seek(0, SeekOrigin.Begin);

        return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        {
            FileDownloadName = HttpUtility.UrlEncode(sFileName)
        };
    }`

使用以上的代码尝试导出一个只有表头的空模板 ` public class SmartLockerInputDto { ///

/// 泛型主键 /// [ExcelColumn(Name = "序号")] public int Id { get; set; } /// /// 创建时间 /// [ExcelColumn(Name = "创建时间")] public DateTime? CreateTime { get; set; }

    /// <summary>
    /// 创建人
    /// </summary>
    [MaxLength(40)]
    [ExcelColumn(Name = "创建人")]
    public string? CreatedBy { get; set; }

    /// <summary>
    /// 更新时间
    /// </summary>
    [ExcelColumn(Name = "修改时间")]
    public DateTime? ModifiedTime { get; set; }

    /// <summary>
    /// 更新人
    /// </summary>
    [MaxLength(40)]
    [ExcelColumn(Name = "修改人")]
    public string? ModifiedBy { get; set; }
    /// <summary>
    /// 设备名称
    /// </summary>
    [ExcelColumn(Name = "设备名称")]
    [MaxLength(50)]
    public string? LotName { get; set; }

    /// <summary>
    /// 设备类型Id
    /// </summary>

    [ExcelColumn(Name = "设备类型序号")]
    public int? LotTypeId { get; set; }

    /// <summary>
    /// 设备类型名称 
    /// </summary>

    [MaxLength(30)]
    [ExcelColumn(Name = "设备类型名称")]
    public string? LotTypeName { get; set; }

    /// <summary>
    /// 图书馆Id
    /// </summary>
    [ExcelColumn(Name = "图书馆序号")]
    public int? LibId { get; set; }

    /// <summary>
    /// 设备描述
    /// </summary>

    [MaxLength(255)]
    [ExcelColumn(Name = "设备描述")]
    public string? LotDesc { get; set; }

    /// <summary>
    /// 设备位置地址
    /// </summary>

    [MaxLength(120)]
    [ExcelColumn(Name = "设备位置地址")]
    public string? Location { get; set; }

    /// <summary>
    /// 楼层
    /// </summary>

    [MaxLength(20)]
    [ExcelColumn(Name = "楼层")]
    public string? Floor { get; set; }

    /// <summary>
    /// 经纬度(XY坐标)
    /// </summary>
    [ExcelColumn(Name = "经纬度Lng")]
    public double? Lng { get; set; }

    /// <summary>
    /// 经纬度(XY坐标)
    /// </summary>
    [ExcelColumn(Name = "经纬度Lat")]
    public double? Lat { get; set; }

    /// <summary>
    /// 状态(1, 已启用, 0未定义, -1 停用; -2 删除)//修改成沿用的那套,1正常使用
    /// </summary>
    [ExcelColumn(Name = "设备状态")]
    public int? Status { get; set; }

    /// <summary>
    /// 运行状态( 999 PowerOn , 888 PowerOff,  3 Error(接口或者自定义错误类型枚举码)
    /// </summary>
    [ExcelColumn(Name = "运行状态")]
    public int? RunStatus { get; set; }

    /// <summary>
    /// 累计工作时间(以秒计算)
    /// </summary>
    [ExcelColumn(Name = "累计工作时间")]
    public int? TotalWorkTime { get; set; }

    /// <summary>
    /// 授权码(未来加密可以作为salt访问用)
    /// </summary>

    [MaxLength(40)]
    [ExcelColumn(Name = "授权码")]
    public string? AuthCode { get; set; }

    /// <summary>
    /// 封面图片
    /// </summary>

    [MaxLength(1024)]
    [ExcelColumn(Name = "封面地址")]
    public string? Cover { get; set; }

    /// <summary>
    /// 图片Id
    /// </summary>
    [ExcelColumn(Name = "图片序号")]
    public int? ImageId { get; set; }

    public SmartLockerInputDto()
    {

    }
}`

以上是用于导出的Dto代码 得到以下异常信息:

message:Type SmartLockerInputDto is not implemented. Please open an issue. stackMessage: 在 MiniExcelLibs.OpenXml.ExcelOpenXmlSheetWriter.d2.MoveNext() 在 MiniExcelLibs.OpenXml.ExcelOpenXmlSheetWriter.d0.MoveNext() 在 MiniExcelLibs.MiniExcel.d__1.MoveNext()