我嘗試了幾個解決方案,如Using System.Microsoft.Office.Excel
和Excel = System.Microsoft.Office.Excel
,但它的功能工作.... 在這裏,我試圖讓數據在一個表並以.xls格式下載到服務器中指定位置的文件中,然後爲用戶提供下載文件的鏈接。 這是出口`從asp.net中的gridview導出爲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();
}
所以當用戶打開下載的文件,給出了相同的擴展名的警告,它不是。 這是爲什麼發生.. ??
我試圖在各種網站提供的解決方案的工作,但都無濟於事......
感謝
試過了,但同樣的提示彈出... :( – Kulkarni