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();
}
そのため、ユーザーがダウンロードしたファイルを開くと、同じ拡張子ではないという警告が表示されます。なぜこうなった..??
さまざまなサイトで提供されているソリューションを使用してみましたが、役に立ちませんでした...
ありがとう