97版苏三传奇受刑:DataGridView中的数据导入Excel .

来源:百度文库 编辑:偶看新闻 时间:2024/04/28 22:15:55
//保存的Excel public bool SaveExcel(DataGridView girdView, bool isShowExcle) { if (girdView.Rows.Count == 0) //判断数据是否等于0 return false; //创建 Excel 对象 Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Application.Workbooks.Add(true); excel.Visible = isShowExcle; //生成字段名称(列名) for (int i = 0; i < dataGridView1.ColumnCount - 1; i++) { excel.Cells[1, i + 1] = girdView.Columns[i].HeaderText; } //填充数据 for (int i = 0; i < girdView.RowCount - 1; i++) { for (int j = 0; j < girdView.ColumnCount; j++) { //判断类型是否是字符串 if (girdView[j, i].ValueType == typeof(string)) excel.Cells[i + 2, j + 1] = "'" + girdView[j, i].Value.ToString(); else excel.Cells[i + 2, j + 1] = girdView[j, i].Value; } } return true; }
完善上一种方式:
public static void ExportDataGridViewToExcel(DataGridView dataGridview1)   
  1.         {   
  2.             SaveFileDialog saveFileDialog = new SaveFileDialog();   
  3.             saveFileDialog.Filter = "Execl files (*.xls)|*.xls";   
  4.             saveFileDialog.FilterIndex = 0;   
  5.             saveFileDialog.RestoreDirectory = true;   
  6.             saveFileDialog.CreatePrompt = true;   
  7.             saveFileDialog.Title = "导出Excel文件到";   
  8.   
  9.             saveFileDialog.ShowDialog();   
  10.   
  11.             Stream myStream;   
  12.             myStream = saveFileDialog.OpenFile();   
  13.             StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));   
  14.             string str = "";   
  15.             try   
  16.             {   
  17.                 //写标题     
  18.                 for (int i = 0; i < dataGridview1.ColumnCount; i++)   
  19.                 {   
  20.                     if (i > 0)   
  21.                     {   
  22.                         str += "/t";   
  23.                     }   
  24.                     str += dataGridview1.Columns[i].HeaderText;   
  25.                 }   
  26.   
  27.                 sw.WriteLine(str);   
  28.                 //写内容    
  29.                 for (int j = 0; j < dataGridview1.Rows.Count; j++)   
  30.                 {   
  31.                     string tempStr = "";   
  32.                     for (int k = 0; k < dataGridview1.Columns.Count; k++)   
  33.                     {   
  34.                         if (k > 0)   
  35.                         {   
  36.                             tempStr += "/t";   
  37.                         }   
  38.                         if (dataGridview1.Rows[j].Cells[k].Value != null)   
  39.                         {   
  40.                             tempStr += dataGridview1.Rows[j].Cells[k].Value.ToString();   
  41.                         }   
  42.                     }   
  43.                     sw.WriteLine(tempStr);   
  44.                 }   
  45.                 sw.Close();   
  46.                 myStream.Close();   
  47.             }   
  48.             catch (Exception e)   
  49.             {   
  50.                 MessageBox.Show(e.ToString());   
  51.             }   
  52.             finally   
  53.             {   
  54.                 sw.Close();   
  55.                 myStream.Close();   
  56.             }   
  57.         }