If this occurs during Excel import, the reason is:
A text file starting with "ID" or "id_xxxx" (XXXX is a text string ). The first record of the sylk file is recorded in id_number. When Excel identifies the text starting with a text file, it interprets it as a sylk file. Excel will try to convert the file from the sylk format, but it cannot, because there is no valid sylk after the "ID" CharacterCode. Because Excel cannot convert the file, you receive an error message.
Example:
1 Public Void Import () 2 { 3 Datatable dt = Getdata (); 4 Response. Clear (); 5 Response. addheader ( " Content-Disposition " , " Attachment; filename = " + Datetime. Now. tostring ( " Yyyymmddhhmmssfff " ) + " . Xls " ); 6 Response. contentencoding = encoding. getencoding ( " Gb2312 " ); 7 Response. contenttype = " Application/MS-Excel " ; 8 Stringwriter Sw = New Stringwriter (); 9 Arraylist list = New Arraylist (); 10 String Connstr = "" ; 11 For ( Int I = 0 ; I <DT. Columns. Count; I ++ ) 12 { 13 Connstr = connstr + dt. Columns [I]. columnname +" \ T " ; 14 List. Add (Dt. Columns [I]. columnname ); 15 } 16 Connstr = connstr. substring ( 0 , Connstr. Length- 1 ); 17 Sw. writeline (connstr ); 18 For ( Int I = 0 ; I <DT. Rows. Count; I ++ ) 19 { 20 Connstr = "" ; 21 For ( Int J = 0 ; J <DT. Columns. Count; j ++ ) 22 { 23 Connstr = connstr + dt. Rows [I] [list [J]. tostring ()]. tostring () + " \ T " ; 24 } 25 Connstr = connstr. substring ( 0 , Connstr. Length- 1 ); 26 Sw. writeline (connstr ); 27 } 28 Response. Write (SW. tostring ()); 29 Response. Flush (); 30 Response. Close (); 31 } 32 Public Datatable getdata () 33 { 34 Datatable dt = New Datatable (); 35 DT. Columns. Add ( " Id_numeric " , Typeof ( String )); 36 DT. Columns. Add ( " Productname " , Typeof (String )); 37 Datarow DR = DT. newrow (); 38 Dr [ " Id_numeric " ] = " 1 " ; 39 Dr [ " Productname " ] =" Sodium Chloride Injection " ; 40 DT. Rows. Add (DR ); 41 Dr = DT. newrow (); 42 Dr [ " Id_numeric " ] = " 2 " ; 43 Dr [ " Productname " ] = " Potassium Chloride Injection " ; 44 DT. Rows. Add (DR ); 45 Return DT; 46 } 47 Protected Void Button#click ( Object Sender, eventargs E) 48 { 49 Import (); 50 }
Initial code
You can solve the problem by converting ID or id_numeric to lowercase letters:
Example:
1 Public Void Import () 2 { 3 Datatable dt = Getdata (); 4 Response. Clear (); 5 Response. addheader ( " Content-Disposition " , " Attachment; filename = " + Datetime. Now. tostring ( " Yyyymmddhhmmssfff " ) +" . Xls " ); 6 Response. contentencoding = encoding. getencoding ( " Gb2312 " ); 7 Response. contenttype = " Application/MS-Excel " ; 8 Stringwriter Sw =New Stringwriter (); 9 Arraylist list = New Arraylist (); 10 String Connstr = "" ; 11 For ( Int I = 0 ; I <DT. Columns. Count; I ++ ) 12 { 13 Connstr = connstr + dt. Columns [I]. columnname + " \ T " ; 14 List. Add (Dt. Columns [I]. columnname ); 15 } 16 Connstr = connstr. substring ( 0 , Connstr. Length- 1 ); 17 Sw. writeline (connstr ); 18 For ( Int I = 0 ; I <DT. Rows. Count; I ++ ) 19 { 20 Connstr = "" ; 21 For ( Int J =0 ; J <DT. Columns. Count; j ++ ) 22 { 23 Connstr = connstr + dt. Rows [I] [list [J]. tostring ()]. tostring () + " \ T " ; 24 } 25 Connstr = connstr. substring ( 0 , Connstr. Length- 1 ); 26 Sw. writeline (connstr ); 27 } 28 Response. Write (SW. tostring ()); 29 Response. Flush (); 30 Response. Close (); 31 } 32 Public Datatable getdata () 33 { 34 Datatable dt = New Datatable (); 35 DT. Columns. Add ( " Id_numeric " , Typeof ( String )); 36 DT. Columns. Add ( " Productname " , Typeof ( String )); 37 Datarow DR = DT. newrow (); 38 Dr [ " Id_numeric " ] = " 1 " ; 39 Dr [" Productname " ] = " Sodium Chloride Injection " ; 40 DT. Rows. Add (DR ); 41 Dr = DT. newrow (); 42 Dr [ " Id_numeric " ] = " 2 " ; 43 Dr [ " Productname " ] = " Potassium Chloride Injection " ; 44 DT. Rows. Add (DR ); 45 Return DT; 46 } 47 Protected Void Button#click ( Object Sender, eventargs E) 48 { 49 Import (); 50 }
Modified code