0

HTMLデータをExcelにエクスポートしています。エクスポート ボタンをクリックするとすぐに Excel ファイルを取得しますが、Excel ファイルを開こうとすると、ファイル形式が指定された拡張子とは異なるという警告が表示されます。どうすればこのアラートを取り除くことができますか、次のコードを使用して私を助けてください

HttpContext.Current.Response.Buffer = True
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Server.ScriptTimeout = 600
4

1 に答える 1

0

編集: .csv 形式を試してください。わたしにはできる。

protected void btnExport_Click(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        context.Response.Clear();

        string filename = "testExportToCSV";
        DataTable table1 = new DataTable("Customers");

        DataRow row1, row2, row3;

        DataColumn colName = new DataColumn("CustomerName", System.Type.GetType("System.String"));
        DataColumn colCity = new DataColumn("City", System.Type.GetType("System.String"));
        table1.Columns.Add(colName);
        table1.Columns.Add(colCity);

        row1 = table1.NewRow();
        row1["CustomerName"] = "aa";
        row1["City"] = "ss";
        table1.Rows.Add(row1);

        row2 = table1.NewRow();
        row2["CustomerName"] = "bb";
        row2["City"] = "sdd";
        table1.Rows.Add(row2);

        row3 = table1.NewRow();
        row3["CustomerName"] = "cc";
        row3["City"] = "dfgfgf";
        table1.Rows.Add(row3);

        //foreach (System.Data.DataColumn column in table1.Columns)
        //{
        for (int i = 0; i < table1.Columns.Count; i++)
        {
            context.Response.Write(table1.Columns[i].ColumnName.ToString().Replace(",", string.Empty) + ",");

        }
        context.Response.Write(Environment.NewLine);

        //}

        foreach (System.Data.DataRow row in table1.Rows)
        {
            for (int i = 0; i < table1.Columns.Count; i++)
            {
                context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
            }

            context.Response.Write(Environment.NewLine);
        }

        context.Response.ContentType = "text/csv";
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
        context.Response.End();

    }

編集1:

私はあなたの問題に取り組んでいましたが、それがあなたの問題security issueに悪影響を及ぼしていることがわかりましたsystem's security if we modify it

Key: HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security
Value: (DWORD)"ExtensionHardening" = 
                         [0 = Disable check; 
                          1 = Enable check and prompt; 
                          2 = Enable check, no prompt deny open]

Default setting if value not present is 1 (enable and prompt).

このリンクから記事全体を参照できます: http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

編集2: Steps to Modify Registry...

  1. スタートメニューをクリック
  2. 検索プログラムとファイルタイプで実行
  3. 次に、 REGEDITと入力し、[ OK] を押します。
  4. HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security

うまくいけば、それはあなたを助けるでしょう. !! ありがとう。

問題が解決した場合は、回答としてマークして、他の人も参照できるようにします。

于 2012-10-31T14:33:07.040 に答える