(一)  導(dǎo)出excel表(將數(shù)據(jù)庫里的數(shù)據(jù)表保存為xls的文件)

          1.先得出一個表Table。
          2.在代碼中建一個excel實例。
             在建實例前先引用Microsoft.Office.Interop.Excel組件——添加引用
             Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
             excel.Workbooks.Add(true);
          3.將table時的數(shù)據(jù),循環(huán)遍歷插入到excel中。

          具體實例如下:
                  string connectionstr = "Server=192.168.1.10;database=ssh;uid=sa;pwd=1234";
                  string sqlstr = "select * from medstock";
                  SqlConnection con = new SqlConnection(connectionstr);
                  SqlCommand cmd = new SqlCommand(sqlstr,con);
                  DataSet ds = new DataSet();
                  DataTable db = new DataTable();
                  SqlDataAdapter adp = new SqlDataAdapter(cmd);//由于SqlDataAdapter自身帶有數(shù)據(jù)庫打開與關(guān)閉功能,所以不用手動打開數(shù)據(jù)庫與關(guān)閉。
                  adp.Fill(ds,"table1");
                  db = ds.Tables["table1"];

                  //建列名,根據(jù)實際情況而定,即要在excel中顯示的列名;
                  string[] str=new string[db.Columns.Count];
                  for (int i = 0; i < str.Length-2; i++)
                  {
                      str[i] = db.Columns[i+1].ColumnName;
                  }
                 //建excel實例。也就是table的容器;
                 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                 excel.Workbooks.Add(true);
                 //先在excel中顯示出table的列名;
                 for (int i = 0; i < str.Length; i++)
                 {
                      excel.Cells[1, i + 1] = str[i];
                 }
                 //再向excel中循環(huán)添加表中的每一行的每一列;
                 if (db.Rows.Count > 0)
                      {
                          for (int i = 0; i < db.Rows.Count; i++)
                          {
                              for (int j = 1; j < db.Columns.Count; j++)
                              {
                                  string str1 = db.Rows[i][j].ToString();
                                  excel.Cells[i + 2, j] = "'" + str1;
                             }
                          }
                      }
                  //設(shè)置禁止彈出保存和覆蓋的詢問提示框  
                  // excel.DisplayAlerts = false;
                  // excel.AlertBeforeOverwriting = false;
                  excel.Save();//保存excel文件         
                  excel.Quit();//確保Excel進(jìn)程關(guān)閉
                  excel.Visible = true;// 前臺可見 后臺運行
                  excel = null;

          posted on 2012-05-14 16:39 SkyDream 閱讀(1382) 評論(0)  編輯  收藏 所屬分類: C# WinForm

          <2012年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南乐县| 永修县| 绵阳市| 自贡市| 治多县| 鄂尔多斯市| 河西区| 北辰区| 新营市| 聂荣县| 阳信县| 教育| 临沧市| 弋阳县| 涿鹿县| 阳山县| 绥中县| 合川市| 同江市| 沂南县| 天峻县| 武乡县| 阳信县| 新龙县| 广河县| 白城市| 比如县| 延长县| 长宁县| 泸水县| 崇州市| 永善县| 丹巴县| 封丘县| 吴江市| 金塔县| 津市市| 大庆市| 扶余县| 湘潭市| 门源|