1

GridView を Excel ファイルにエクスポートしていますが、ファイルを開くと、最初に形式の種類と拡張子が一致しないというエラーが表示され、開くとページ全体が Excel ファイルに取り込まれます。 、グリッド ビューだけではありません。

更新パネルを使用していません。GridView の内側と外側のボタンを試してみたところ、同じ結果が得られたので、次のようなコード ビハインドからのものである可能性があります。

        Response.Clear();

        Response.Buffer = true;
        string filename = "GridViewExport_" + DateTime.Now.ToString() + ".xls";
        Response.AddHeader("content-disposition",
        "attachment;filename=" + filename);
        Response.Charset = String.Empty;
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);


        GridView3.RenderControl(hw);
        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        HttpContext.Current.ApplicationInstance.CompleteRequest();

4

3 に答える 3