[エクスポート] ボタンは次のとおりです。
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
var dataTable = (DataTable) Session["finalSchedulesTable"];
var dummyGv = new GridView {AllowPaging = false, DataSource = dataTable};
dummyGv.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Schedules " + DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", "-").Replace(":", "_") + ".xlsx");
Response.Charset = "";
//Response.ContentType = "application/vnd.ms-excel";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var sw = new StringWriter();
var hw = new HtmlTextWriter(sw);
for (int i = 0; i < dummyGv.Rows.Count; i++)
{
//Apply text style to each Row
dummyGv.Rows[i].Attributes.Add("class", "textmode");
}
dummyGv.RenderControl(hw);
//style to format numbers to string
const string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
上記のコードでファイル拡張子を「.xls」に変更すると、プロセスは機能しますが、ファイルを開こうとすると、次のメッセージが表示されます。
「開こうとしているファイル 'filename.xls' は、ファイル拡張子で指定されたものとは異なる形式です。ファイルを開く前に、ファイルが破損しておらず、信頼できるソースからのものであることを確認してください。開きますか?今すぐファイルしますか?」
「はい」と言うと、通常どおりファイルが開きます。
ここでの問題は、このメッセージをポップアップさせたくないということです。これを修正するにはどうすればよいですか?
私がやろうとしているのは、データを Excel にエクスポートし、ユーザーが通常 (警告メッセージなしで) ファイルを開くことを許可することだけです。