Loading...

          java .net

          C# 由DataGridView導出到Excel

          private void buttonToExcel_Click(object sender, EventArgs e)
          {

              SaveFileDialog saveFileDialog = new SaveFileDialog();
              saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
              saveFileDialog.FilterIndex = 0;
              saveFileDialog.RestoreDirectory = true;
              saveFileDialog.CreatePrompt = true;
              saveFileDialog.Title = "Export Excel File To";
              saveFileDialog.ShowDialog();
              Stream myStream;
              myStream = saveFileDialog.OpenFile();
              StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
              string str = "";
              try
              {
                  //寫標題
                  for (int i = 0; i < this.dataGridView3.ColumnCount; i++)
                  {
                      if (i > 0)
                      {
                          str += "\t";
                      }

                      str += this.dataGridView3.Columns[i].HeaderText;
                  }
                  sw.WriteLine(str);
                  //寫內容
                  for (int j = 0; j < this.dataGridView3.Rows.Count; j++)
                  {
                      string tempStr = "";
                      for (int k = 0; k < this.dataGridView3.Columns.Count; k++)
                      {
                          if (k > 0)
                          {
                              tempStr += "\t";
                          }
                          tempStr += this.dataGridView3.Rows[j].Cells[k].Value + "";
                      }
                      sw.WriteLine(tempStr);
                  }

                  sw.Close();
                  myStream.Close();
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }

              finally
              {
                  sw.Close();
                  myStream.Close();
              }    
              MessageBox.Show("OK");
          }

          posted on 2009-06-03 12:23 閱讀(1160) 評論(0)  編輯  收藏 所屬分類: C#


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          公告

          希望有一天

          我能用鼠標雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導航

          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乌海市| 德钦县| 台东县| 滁州市| 洪雅县| 台南市| 昌邑市| 乌鲁木齐县| 隆安县| 梁平县| 玉林市| 织金县| 柳州市| 昆山市| 阳原县| 尉氏县| 钟祥市| 湘乡市| 平湖市| 阿城市| 南开区| 盐亭县| 荆州市| 永安市| 聂荣县| 友谊县| 越西县| 云霄县| 武安市| 读书| 嘉禾县| 永登县| 兴文县| 霍林郭勒市| 色达县| 游戏| 瑞安市| 福建省| 八宿县| 乌恰县| 观塘区|