在做excel导入时候出现如上图情况,原因是:
SYLK 文件时一个文本文件,开头的为“ID”或“ID_XXXX”(其中XXXX是文本字符串)。
SYLK 文件的第一个的记录是在 ID_Number 记录的。
Excel 将识别该文本在文本文件开头时, 它会将该文件解释为 SYLK 文件。
Excel 将尝试从该 SYLK 格式转换该文件,但不能这样做,因为"ID"字符后不有任何有效的 SYLK 代码。
因为 Excel 不能转换该文件,您收到错误消息。
例子如下:
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"]="氯化钠注射液"; 40 dt.Rows.Add(dr); 41 dr = dt.NewRow(); 42 dr["ID_Numeric"] = "2"; 43 dr["productname"] = "氯化钾注射液"; 44 dt.Rows.Add(dr); 45 return dt; 46 } 47 protected void Button1_Click(object sender, EventArgs e) 48 { 49 Import(); 50 }
将ID或者ID_Numeric转换成小写字母就可以解决问题:
例子如下:
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"]="氯化钠注射液"; 40 dt.Rows.Add(dr); 41 dr = dt.NewRow(); 42 dr["Id_Numeric"] = "2"; 43 dr["productname"] = "氯化钾注射液"; 44 dt.Rows.Add(dr); 45 return dt; 46 } 47 protected void Button1_Click(object sender, EventArgs e) 48 { 49 Import(); 50 }