데이터를 Excel 파일로 내보내는 웹 페이지가 있습니다. 내가 겪고있는 유일한 문제는 엑셀 파일을 열려고 할 때 "당신이 열려고하는 파일이 파일 확장명이 아닌 다른 형식입니다. 파일이 손상되지 않았으며 파일을 열기 전에 신뢰할 수있는 원본을 선택하십시오. " 어떻게이 메시지를 없앨 수 있습니까? 내보내기를 수행하려면 다른 기사에서 찾은 기능을 사용하고 있습니다. 코드는 다음과 같습니다.DataTable을 Excel로 내보내기 asp
private void ExporttoExcel(DataTable table)
{
//Response.Clear();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/excel";
HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=TestingReports");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1252");
// Sets font
HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
// Sets the table border, cell spacing, border color, font of the text, background, foreground, font height
HttpContext.Current.Response.Write("<Table bgColor='#ffffff' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
// Am getting my grid's column headers
int columnscount = table.Columns.Count;
// Write in new column
for (int j = 0; j < columnscount; j++)
{
HttpContext.Current.Response.Write("<Td style='font-size:15.0pt; text-align:center; width:80.0pt; border-width:1.0pt; border-color:#000000; border-style:solid; height:22.0pt;'>");
// Get column headers and make it as bold in excel columns
HttpContext.Current.Response.Write("<B>");
HttpContext.Current.Response.Write(table.Columns[j].ToString());
HttpContext.Current.Response.Write("</B>");
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
// Write in new row
foreach (DataRow row in table.Rows)
{
HttpContext.Current.Response.Write("<TR>");
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<Td style='width:80.0pt; text-align:center; border-width:0.5pt; border-color:#000000; border-style:solid; height:22.0pt;'>");
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("</Table>");
HttpContext.Current.Response.Write("</font>");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
//Response.Write("TestingReports.xls");
//Response.Flush();
//Response.End();
}
도움이 될 것입니다. 미리 감사드립니다.
중복입니다. 여기를 참고하십시오 http://stackoverflow.com/questions/3968973/how-can-i-write-data-into-an-excel-using-php –