日期:2014-05-20  浏览次数:20999 次

关于C#导入导出EXCEL表格的问题!
我用 这两个方法进行 c#的excel表格的 导入导出
为什么 我导出的文件 都导入不进来 报的错误是 格式不对
有没有办法 解决这个问题??
我的 导入导出代码如下:


 public IList<AddressList> excelImport(String path) //导入方法
  {
  IList<AddressList> allist = new List<AddressList>();
  // string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + "Extended Properties=Excel 8.0;";
  string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+";Extended Properties='Excel 8.0;HDR=NO'";
  OleDbConnection conn = new OleDbConnection(strConn);
  string strExcel = "";
  strExcel = "select * from [sheet1$]";
  OleDbCommand cmd = new OleDbCommand(strExcel, conn);
  try
  {
  conn.Open();
  OleDbDataReader reader = cmd.ExecuteReader();
   
  while (reader.Read())
  {
   
  String id = reader[0].ToString().Trim();
  string name = reader[1].ToString().Trim();
  string moblenumber = reader[2].ToString().Trim();
  if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(name) &&! string.IsNullOrEmpty(moblenumber))
  {
  AddressList al = new AddressList();
  al.Id = Convert.ToInt32(id);
  al.Name = name;
  al.MobleNumber = moblenumber;
  allist.Add(al);
  }
  }
  //AddressList al1 = new AddressList();
  //al1.Id = Convert.ToInt32(reader.GetName(0));
  //al1.Name = reader.GetName(1);
  //al1.MobleNumber = reader.GetName(2);
  //allist.Add(al1);
  //MessageBox.Show(reader.GetName(0));
  //MessageBox.Show(reader.GetName(1));
  //MessageBox.Show(reader.GetName(2));
  return allist;
  }
  catch(Exception e)
  {
  e.ToString();

  }
  finally {

  conn.Close();
  }

  return null;
   
   
  }
   

   
  //导出 
  public bool txtOut(String path,IList<string> list)
  {
  FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
  StreamWriter m_streamWriter = new StreamWriter(fs);
  m_streamWriter.Flush();
  // 使用StreamWriter来往文件中写入内容
  m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
  // 把richTextBox1中的内容写入文件
  foreach(string str in list)
  m_streamWriter.Write(str);
  //关闭此文件
  m_streamWriter.Flush();
  m_streamWriter.Close();

  return true;
   
  }
  public bool excelOut(String path,IList<AddressList> list)
  {
  Excel.Application excel = new Excel.Application();