使用剃刀视图定义的结构创建Excel文件

时间:2021-03-20 10:05:38

I want to create an .xlsx file not .xls using my razor view string, But it doesn't work here is my code

我想使用我的剃刀视图字符串创建一个.xlsx文件而不是.xls,但它不起作用我的代码

private ContentResult GenerateExcel<T>(string viewName, T model) where T : class
    {
        var htmlViewRenderer = new HtmlViewRenderer();
        string htmlText = htmlViewRenderer.RenderViewToString(this, viewName, model);
        //  byte[] renderedBuffer;
        Response.ClearContent();
        Response.Buffer = true;

        Response.AddHeader("content-disposition", "attachment; filename=Marklist.xlsx");
        Response.ContentType = "application/ms-excel";
        Response.Charset = "";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        Response.Output.Write(htmlText.ToString());
        Response.Flush();
        Response.End();
       return  Content(htmlText,Response.ContentType);

    }

1 个解决方案

#1


0  

IText-sharp comes with nuget package it helped to create excel i use it and it don't have any issue for xls or xlsx format

IText-sharp带有nuget包,它有助于创建excel我使用它,它对xls或xlsx格式没有任何问题

#1


0  

IText-sharp comes with nuget package it helped to create excel i use it and it don't have any issue for xls or xlsx format

IText-sharp带有nuget包,它有助于创建excel我使用它,它对xls或xlsx格式没有任何问题