Loading...

          java .net

          C# 由DataGridView導(dǎo)出到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
              {
                  //寫標(biāo)題
                  for (int i = 0; i < this.dataGridView3.ColumnCount; i++)
                  {
                      if (i > 0)
                      {
                          str += "\t";
                      }

                      str += this.dataGridView3.Columns[i].HeaderText;
                  }
                  sw.WriteLine(str);
                  //寫內(nèi)容
                  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#


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


          網(wǎng)站導(dǎo)航:
           

          公告

          希望有一天

          我能用鼠標(biāo)雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網(wǎng)絡(luò)

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 西盟| 张家界市| 张家港市| 太和县| 滕州市| 青铜峡市| 柳江县| 仙居县| 普洱| 绥宁县| 察隅县| 土默特左旗| 涟水县| 五河县| 西昌市| 邛崃市| 黄龙县| 元朗区| 博湖县| 宁陕县| 马山县| 防城港市| 岳池县| 洛宁县| 石门县| 白玉县| 张家港市| 武义县| 岚皋县| 枝江市| 东至县| 皮山县| 邵武市| 平武县| 威宁| 新巴尔虎左旗| 芷江| 临汾市| 东乌珠穆沁旗| 讷河市| 永康市|