GridView를 Excel 파일로 내보내는 중일 때 파일을 열면 형식 유형과 확장명이 일치하지 않는다는 오류가 발생합니다. 전체 형식을 열면 전체 페이지가 Excel 파일, 그리드보기가 아닙니다.어떻게 GridView를 Excel로 내보낼 수 있습니까?
enter code here
protected void ExportToExcel(object sender, EventArgs e)
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
gridCustomer.AllowPaging = false;
//this.gridCustInfoBind(1);
gridCustAllInfoBind(1);
gridCustomer.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in gridCustomer.HeaderRow.Cells)
{
cell.BackColor = gridCustomer.HeaderStyle.BackColor;
}
foreach (GridViewRow row in gridCustomer.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = gridCustomer.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = gridCustomer.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
gridCustomer.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Exception Message: " + ex.Message.Replace("'", "").Replace("\"", "") + "');", true);
}
}
생성 된 파일의 내용 .html로 어떻게 .XLS로 변환하는 – Slai
을 .XLS되지 않기 때문에, 당신은 내 코드를 수정할 수 있습니다. –