久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

DataGridView中的數(shù)據(jù)導(dǎo)入Excel .

 mylore 2011-11-29
//保存的Excel public bool SaveExcel(DataGridView girdView, bool isShowExcle) { if (girdView.Rows.Count == 0) //判斷數(shù)據(jù)是否等于0 return false; //創(chuàng)建 Excel 對(duì)象 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; } //填充數(shù)據(jù) 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 = "導(dǎo)出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.                 //寫標(biāo)題     
  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.                 //寫內(nèi)容    
  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.         }   
  

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多