日期:2014-05-18  浏览次数:20821 次

怎才能吧DataGrid里的数据导成别的格式保存啊 ???
又道题要求将数据显示在DataGrid并且将数据导成Execl保存 怎么弄啊 ?? 大侠吗 !!

------解决方案--------------------
C# code

private void button11_Click(object sender, System.EventArgs e)
  {
   Excel.Application myExcel = new Excel.Application(); 
   myExcel.Application.Workbooks.Add(true); 
   myExcel.Visible = true; 
   //Object Date[ds.Tables["CurrentTable"].Rows.Count][ds.Tables["CurrentTable"].Columns.Count]; 
   //生成标题 
   for (int i = 0; i < ds.Tables["gzdw"].Columns.Count; i++) 
   { 
    myExcel.Cells[1, i + 1] = ds.Tables["gzdw"].Columns.Caption; 
   } 
   //填充数据 
   for (int i = 0; i < ds.Tables["gzdw"].Rows.Count; i++) 
   { 
    for (int j = 0; j < ds.Tables["gzdw"].Columns.Count; j++) 
    { 
     if (ds.Tables["gzdw"].Rows[j].GetType() == typeof(System.String)) 
     { 
               myExcel.Cells[i + 3, j + 1] = "'" + ds.Tables["gzdw"].Rows[j].ToString(); 
     } 
     else 
     { 
      myExcel.Cells[i + 3, j + 1] = ds.Tables["gzdw"].Rows[j].ToString(); 
     } 
    } 
   } 
   this.Cursor = Cursors.Default; 
   MessageBox.Show("导出成功! ", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 
  }