0

Using System.Microsoft.Office.Excelやのようなサイトで言及されているいくつかのソリューションを試してみましたExcel = System.Microsoft.Office.Excelが、うまくいきません....次に、ファイルをダウンロードするためのリンクをユーザーに提供します。これはエクスポートのコードです `

protected void btnExcelExport_Click(object sender, EventArgs e)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using( HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            //  Create a form to contain the grid
            Table table = new Table();
            // get gridlines from gridview
            table.GridLines = GridView2.GridLines;
            if (GridView2.HeaderRow != null)
            {                       
                table.Rows.Add(GridView2.HeaderRow);
            }
            foreach (GridViewRow row in GridView2.Rows)
            {
                table.Rows.Add(row);
            }
            if (GridView2.FooterRow != null)
            {
                table.Rows.Add(GridView2.FooterRow);
            }    
            //  render the table into the htmlwriter
            table.RenderControl(htw);    
        }
        var myRootPath = Server.MapPath("~");
        var docPath = Path.GetFullPath(Path.Combine(myRootPath, "/Compare/c.xls")); 
        File.WriteAllText(docPath, sw.ToString());  
    }
    dwndlink.Visible = true;

}

これが linkbutton のコードの場合:

 protected void dwnlink(object sender, EventArgs e)
 {   
     var webRootPath = Server.MapPath("~");
     var docPath = Path.GetFullPath(Path.Combine(webRootPath, "/Compare/c.xls"));

     string name = Path.GetFileName(docPath);
     Response.AppendHeader("content-disposition", "attachment; filename=" + name);
     Response.ContentType = "Application/vnd.ms-excel";
     Response.TransmitFile(docPath);
     Response.End();
 }

そのため、ユーザーがダウンロードしたファイルを開くと、同じ拡張子ではないという警告が表示されます。なぜこうなった..??

さまざまなサイトで提供されているソリューションを使用してみましたが、役に立ちませんでした...

ありがとう

4

2 に答える 2

0

使ってみて

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
于 2012-12-06T05:42:06.753 に答える
0

Try changing Response.ContentType = "application/vnd.xls"; to Response.ContentType = "application/vnd.ms-excel"

Also

Microsoft document says :

"The current design does not allow you to open HTML content from a web site in Excel... So ASP pages that return HTML and set the MIME type to something like XLS to try to force the HTML to open in Excel instead of the web browser (as expected) will always get the security alert... If you use an HTML MIME type, then the web browser will open the content instead of Excel. So there is no good workaround for this case because of the lack of a special MIME type for HTML/MHTML that is Excel specific. You can add your own MIME type if you control both the web server and the client desktops that need access to it, but otherwise the best option is to use a different file format or alert your users of the warning and tell them to select Yes to the dialog."

Visit this site too : http://devblog.grinn.net/2008/06/file-you-are-trying-to-open-is-in.html

于 2012-12-06T05:46:25.047 に答える