错误:将gridview下载到Excel电子表格时,文件扩展名采用不同的格式 [英] ERROR: File extension in a different format when downloading gridview to excel spreadsheet

查看:70
本文介绍了错误:将gridview下载到Excel电子表格时,文件扩展名采用不同的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么

我想让用户能够使用针对SQL Server的C#asp.net网页进行搜索并检查gridview中的数据。然后,如果数据令人满意,他们可以点击导出并将数据复制到Excel电子表格。



我的代码:

What am I trying to do
I want to give the user the ability to do a search using a C# asp.net webpage against a SQL Server and examine the data in a gridview. Then if the data is satisfactory they can click on export and copy the data to an excel spreadsheet.

My Code:

Response.ClearContent();
    Response.ContentType = "application/excel";
    Response.AddHeader("content-disposition", "attachment; filename=\"FileName.xls\"");
    Response.Charset = "";
    System.IO.StringWriter OEStringWriter = new StringWriter();
    System.Web.UI.HtmlTextWriter OEHTMLTextWriter = new HtmlTextWriter(OEStringWriter);
    grdMyOEs.RenderControl(OEHTMLTextWriter);
    Response.Write(OEStringWriter.ToString());
    Response.Flush();
    Response.End();





注意:

1.上面的grdMyOE有大约30行从SQL Server填充的数据。

2.我在我的代码中覆盖了VerifyRenderingInServerForm。

3.花了12个小时查看每个网上的示例/示例代码。

4.我打算复制我在代码中尝试的所有突变以使其工作,但它可能会超载codeproject.com



麻烦的警告:

您要打开的文件,'FileName.xls'的格式与指定的格式不同通过文件扩展名...



我尝试了所有常用的excel文件扩展名无济于事。如果我将文件名更改为FileName.html,我可以打开该文件,但是,它是网页格式而不是excel文件。



Notes:
1. grdMyOEs above has about 30 rows of data populated from a SQL Server.
2. I am overriding VerifyRenderingInServerForm in my code.
3. Spent 12 hours looking over every example/sample code on the web.
4. I was going to copy in all of the mutations I tried within the code to get it to work but it would probably overload codeproject.com

Troublesome warning:
The file you are trying to open, 'FileName.xls' is in a different format than specified by the file extension...

I have tried all of the usual excel file extensions to no avail. If I change the file name to FileName.html I can open the file, however, it is in the format of a web page and not an excel file.

推荐答案

当我不得不给这只猫上皮时,这就是我采用的方法:



When I've had to skin this cat, this is the approach that I took:

//returns a memorystream with an OpenXML.SpreadsheetDocument created on it
var stream = ExcelUtility.GetExcel(temp);
var filename = "MyFile.xlsx" // Usually a concat'd term

var cd = new System.Net.Mime.ContentDisposition
{
    FileName = filename,
    Inline = false,
};
Response.Clear();
Response.AppendHeader("Content-Disposition", cd.ToString());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.Flush();
Response.End();





我怀疑有两种可能性。



首先,没有适当的内容处理,浏览器不知道如何处理文件。



其次,文件可能有问题,因为它是文本流而不是byte []。您可能最好建立一个利用DocumentFormat.OpenXML来解析网格的函数,而不是尝试将其作为文本流发送。



I suspect 2 possibilities.

First, without a proper Content-Disposition the browser isn't sure what to do with the file.

Second, the file may have issues since it is a text stream rather than a byte[]. You'd likely be best off building a function that leverages DocumentFormat.OpenXML to parse the grid, rather than trying to send it as a text stream.


这篇关于错误:将gridview下载到Excel电子表格时,文件扩展名采用不同的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆