Net中文件扩展名所指定的格式不同

Net中文件扩展名所指定的格式不同

本文介绍了您尝试打开的文件的格式与Asp.Net中文件扩展名所指定的格式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码,你尝试打开的文件的格式不同于文件扩展名c#错误中指定的格式。

  public ActionResult Export(string filterBy)
{
MemoryStream output = new MemoryStream();
StreamWriter writer = new StreamWriter(output,Encoding.UTF8);

var data = City.GetAll()。选择(o => new
{
CountryName = o.CountryName,
StateName = o.StateName,
o.City.Name,
Title = o.City.STDCode
})。ToList();
var grid = new GridView {DataSource = data};
grid.DataBind();
var htw = new HtmlTextWriter(writer);

grid.RenderControl(htw);

writer.Flush();
output.Position = 0;

return File(输出,application / vnd.ms-excel,test.xls);

}

当我尝试打开excel我得到这个错误





单击是后,文件正确打开。但我不希望这个msg出现。

解决方案

我已经使用了来解决问题。

  public static void ExportToExcel IEnumerable< dynamic> data,string sheetName)
{
XLWorkbook wb = new XLWorkbook();
var ws = wb.Worksheets.Add(sheetName);
ws.Cell(2,1).InsertTable(data);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType =application / vnd.openxmlformats-officedocument.spreadsheetml.sheet;
HttpContext.Current.Response.AddHeader(content-disposition,String.Format(@attachment; filename = {0} .xlsx,sheetName.Replace(,_)));

使用(MemoryStream memoryStream = new MemoryStream())
{
wb.SaveAs(memoryStream);
memoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
memoryStream.Close();
}

HttpContext.Current.Response.End();
}

在我的项目中使用。


the file you are trying to open is in a different format than specified by the file extension c# error when trying to open file in excel.

Here is my code

public ActionResult Export(string filterBy)
{
    MemoryStream output = new MemoryStream();
    StreamWriter writer = new StreamWriter(output, Encoding.UTF8);

    var data = City.GetAll().Select(o => new
    {
        CountryName = o.CountryName,
        StateName = o.StateName,
        o.City.Name,
        Title = o.City.STDCode
    }).ToList();
    var grid = new GridView { DataSource = data };
    grid.DataBind();
    var htw = new HtmlTextWriter(writer);

    grid.RenderControl(htw);

    writer.Flush();
    output.Position = 0;

    return File(output, "application/vnd.ms-excel", "test.xls");

}

when am trying to open excel i get this error

After clicking on Yes the file open properly. but i don't want this msg to appear.

解决方案

I have used CloseXML to solve the problem.

public static void ExportToExcel(IEnumerable<dynamic> data, string sheetName)
{
    XLWorkbook wb = new XLWorkbook();
    var ws = wb.Worksheets.Add(sheetName);
    ws.Cell(2, 1).InsertTable(data);
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    HttpContext.Current.Response.AddHeader("content-disposition", String.Format(@"attachment;filename={0}.xlsx",sheetName.Replace(" ","_")));

    using (MemoryStream memoryStream = new MemoryStream())
    {
        wb.SaveAs(memoryStream);
        memoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
        memoryStream.Close();
    }

    HttpContext.Current.Response.End();
}

Installed ClosedXML in my project using Nuget Package Manager.

这篇关于您尝试打开的文件的格式与Asp.Net中文件扩展名所指定的格式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 09:15