public static void ExportToExcel(DataTable dt, string fileName)
                  {
                      //在做這些前,將Excl添加到引用中來!!
                      Excel.Application excel = new Excel.Application();
                      //如果系統是Excl2007,添加的引用會不一樣,代碼如下。
                      //Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                      if (excel == null)
                      {
                        
                      }
                      else
                      {
                          Excel.Workbook xlBook = excel.Workbooks.Add(true);
                          Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
                          //excel.Application.Workbooks.Add(true);           
                          int cols = dt.Columns.Count;
                          int rows = dt.Rows.Count;
                          //列
                          for (int k = 0; k < cols; k++)
                          {
                              excel.Cells[1, k + 1] = dt.Columns[k].ColumnName;
                          }
                          //數據
                          for (int i = 0; i < rows - 1; i++)
                          {
                              for (int j = 0; j < cols; j++)
                              {
                                
                                      excel.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
                              }
                          }
             
                          try
                          {
                              xlBook.Saved = true;
                              xlBook.SaveCopyAs(fileName);
                          }
                          catch
                          {
                       
                          }
                    
                      }
                  }

          http://files.cnblogs.com/wangdetian168/Interop.Excel.rar

          posted on 2010-10-22 01:48 sanmao 閱讀(379) 評論(0)  編輯  收藏

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


          網站導航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 揭阳市| 祁连县| 岳普湖县| 嘉兴市| 襄樊市| 崇义县| 集安市| 香格里拉县| 贵德县| 西平县| 大宁县| 四川省| 潢川县| 健康| 长春市| 栾川县| 昭通市| 珲春市| 鄱阳县| 韶关市| 六盘水市| 黄龙县| 墨脱县| 罗平县| 花垣县| 武平县| 磐石市| 双江| 双柏县| 兴宁市| 浦江县| 伊通| 平顶山市| 方正县| 金阳县| 交城县| 应城市| 哈巴河县| 韶山市| 合水县| 长垣县|