2013-12-12 4 views
0

나는 코드를 다음과 ASP.Net에서 엑셀 문서를 생성수출 DataTable을 엑셀 형식/확장 오류

Error: excel cannot open the file .xls because the file format or file extension is not valid

어떻게 내가 해결할 수 있을까?

+0

'Response.Charset = "UTF-8"없이 시도해 보셨습니까? – Koen

+0

예, Charset없이 처음 시도한 후 Charset으로 시도한 후에 오류가 발생하고 다시 오류가 발생합니다. – UserStackk

답변

0

Excel 응용 프로그램 개체에 대한 참조를 사용하여 실제 Excel 파일을 만들거나 많은 코드 플렉스 기반 프로젝트 중 하나를 사용하여 Excel 스프레드 시트를 만들거나 DataGrid에서 CSV 파일을 가장 쉽게 만들고 반환 할 수 있습니다. 컴퓨터가 Excel에서 열 것인지 묻는 메시지를 표시합니다.

var tw = new StringWriter(); 
    var hw = new System.Web.UI.HtmlTextWriter(tw); 
    var dgGrid = new DataGrid(); 
    dgGrid.DataSource = dataTable; 
    dgGrid.DataBind(); 
    dgGrid.RenderControl(hw); 

    var attachment = "attachment; filename=" + fileName + ".xls"; 

    Response.Charset = "UTF-8"; 
    Response.ContentType = "application/vnd.ms-excel"; 
    Response.AppendHeader("content-disposition", attachment); 
    // outputResponse = tw.ToString(); 
    Response.Output.Write(tw.ToString()); 
    Response.Flush(); 
    Response.End(); 

내 데이터 테이블은 간단한 데이터 (문자와 숫자)와 2 COL의 테이블과 같이 열 :

0

그냥 내가 변경 한 마지막 줄의 예외로 코드를 시도 그것은해야한다.

The file you are trying to open'yourfilename.xls', is in a different format thahn specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?

확인을 클릭하면 파일이 잘 열립니다

당신은라는 메시지가 표시됩니다.