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 閱讀(1154) 評論(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ò)

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 武安市| 富蕴县| 靖边县| 金山区| 陵川县| 商河县| 华亭县| 内乡县| 莒南县| 垫江县| 洞口县| 余干县| 东至县| 巴林左旗| 棋牌| 孝感市| 鄂托克旗| 育儿| 开阳县| 江西省| 开鲁县| 宁乡县| 英超| 临安市| 青河县| 永定县| 张家川| 尉氏县| 白银市| 海盐县| 云浮市| 灵寿县| 秦皇岛市| 六盘水市| 朝阳市| 黄骅市| 西和县| 毕节市| 福贡县| 万盛区| 肥城市|