日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

      首頁編程開發(fā)C#.NET → C# 中把DataTable中數(shù)據(jù)導(dǎo)出Excel編程實例

      C# 中把DataTable中數(shù)據(jù)導(dǎo)出Excel編程實例

      相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時間:2012/11/23 17:01:22字體大小:A-A+

      作者:西西點擊:0次評論:1次標簽: Excel

      目前有個項目,客戶需求是要把數(shù)據(jù)倒成Excel,找了點資料整理了一番。決定寫一片這樣的文章,第一給自己也是一種鞏固,第二給需要的人也是一種幫助。

      (1)首先:添加一個為程序添加一個引用

      (2)在程序中using一下

      using Excel = Microsoft.Office.Interop.Excel;using System.Reflection;

      (3)給你的程序中添加一個模板Excle(一個空的Excle文件就行)

      這里就起一個名字:temp.xls

      (4)按照三層架構(gòu)的思想,以下有2個方法寫在中間層。

      第一個方法SCexcle()有2個參數(shù),①把你需要導(dǎo)入 Excle的數(shù)據(jù)集 定義到一個DataTable中,②指向你程序里面前面定義的 temp.xls 模板excel的路徑

      1 Excel.Application app;
      2 Excel._Workbook wbook;
      3 Excel._Worksheet oSheet;
      4
      5     public string SCexcel(DataTable dt, string pathLong)
      6         {
      7            string wordPath = pathDownLoad + “temp.xls”; //定義模板的路徑
      8            //打開excel文檔
      9            app = new Excel.Application();//添加一個 Excle應(yīng)用對象
      10
      11            //打開工作簿,可見很多參數(shù),第一個就是我們模板的路徑。
      12             wbook = app.Workbooks.Open(wordPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
      13
      14
      15             oSheet = (Excel._Worksheet)wbook.Worksheets[1];//創(chuàng)建一張sheet表
      16
      17             //定義文件保存路徑
      18               string filename1 = "report" + System.DateTime.Now.Year + System.DateTime.Now.Month + savechinese + ".xls";//因為保存的不平凡,所以之精確到年和月                  。否則就保存到毫秒
      19               string filename2 = pathLong + "UpLoadFiles\\" + filename1;//保存在服務(wù)器的路徑
      20
      21               addExecl(filename2.ToString(), dt);//內(nèi)部的一個方法,把服務(wù)器路徑與之前的數(shù)據(jù)放入addExcle()中,目的是在把數(shù)據(jù)放入Excel中
      22
      23              //打開后就要關(guān)閉。O(∩_∩)O~
      24
      25              app.Workbooks.Close();
      26               //同樣不要忘記結(jié)束進程
      27              System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
      28              System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
      29
      30              GC.Collect();//強制對所有代進行即時垃圾回收
      31
      32    }

      第二個方法addExecl();我們來看看他是如何處理數(shù)據(jù)的,并且使用你自己想要的格式來定義Excel

      1        private void addExecl(string xlsPath, DataTable dt)
      2         {
      3              4           
      5             Excel.Range oRng;
      6             Excel.Range range;
      7
      8
      9             //標題
      10             int excel_cur = 1;
      11             oSheet.Cells[excel_cur, 1] = "大標題";
      12             excel_cur++;
      13
      14             //字段名          
      15             oSheet.Cells[excel_cur, 1] = "序號";
      16             oSheet.Cells[excel_cur, 2] = "字段1";
      17             oSheet.Cells[excel_cur, 3] = "字段2";
      18             oSheet.Cells[excel_cur, 4] = "字段3";
      19             oSheet.Cells[excel_cur, 5] = "字段4";
      20             oSheet.Cells[excel_cur, 6] = "字段5";
      21             oSheet.Cells[excel_cur, 7] = "字段6";
      22             oSheet.Cells[excel_cur, 8] = "字段7";
      23             oSheet.Cells[excel_cur, 9] = "字段8";
      24             oSheet.Cells[excel_cur, 10] = "字段9";
      25             excel_cur++;
      26
      27             //行數(shù)據(jù)綁定
      28             if (dt.Rows.Count > 0)
      29             {
      30                 for (int i = 0; i < dt.Rows.Count; i++)
      31                 {
      32                     oSheet.Cells[excel_cur, 1] = dt.Rows[i][0].ToString();
      33                     oSheet.Cells[excel_cur, 2] = dt.Rows[i][1].ToString();
      34                     oSheet.Cells[excel_cur, 3] = dt.Rows[i][2].ToString();
      35                     oSheet.Cells[excel_cur, 4] = dt.Rows[i][3].ToString();
      36                     oSheet.Cells[excel_cur, 5] = dt.Rows[i][4].ToString();
      37                     oSheet.Cells[excel_cur, 6] = dt.Rows[i][5].ToString();
      38                     oSheet.Cells[excel_cur, 7] = dt.Rows[i][6].ToString();
      39                     oSheet.Cells[excel_cur, 8] = dt.Rows[i][7].ToString();
      40                     oSheet.Cells[excel_cur, 9] = dt.Rows[i][8].ToString();
      41                     oSheet.Cells[excel_cur, 10] = dt.Rows[i][9].ToString();
      42                     excel_cur++;
      43
      44                 }
      45             }
      46             //格式定義
      47             range = (Excel.Range)oSheet.get_Range("A1", "J1");//選中 A1:J1 單元格
      48             range.Merge(0);//合并單元格
      49             range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//縱向居中
      50             range.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//橫向居中
      51             range.Font.Size = 18;
      52             range.Font.Name = "黑體";
      53             range.RowHeight = 24;
      54
      55             oRng = oSheet.get_Range("A2", "J" + Convert.ToString(dt.Rows.Count + 2));
      56             oRng.Borders.LineStyle = 1;
      57             oRng.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//居中
      58             oRng.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
      59             oRng.EntireColumn.AutoFit();//列寬自動 
      60
      61             app.Application.DisplayAlerts = false;
      62             oSheet.SaveAs(xlsPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,               Missing.Value);//文件保存
      63         }

      其實還有很多對Excel進行的樣式設(shè)置,見下面這個表:               

      Excel.Application eole = new Excel.Application();
      效果具體代碼
      添加新工作簿eole.Workbooks.add
      激活指定的工作簿eole.WorkSheets("工作簿名").Activate
      設(shè)置第3個工作表為激活工作表eole.Worksheets("sheet1").Activate
      打開指定工作簿  eole.Workbooks.add("E:/E_temp/ABC.xls")
      更改Excel標題欄eole.Caption="Microsoft Excel"
      給單元格賦值eole.cells(行,列).value=XM(XM為數(shù)據(jù)庫字段名)
        eole.cells(1,4).value='ASDFASDFASDFASDFADSF'
      設(shè)置指定列的寬度(單位:字符個數(shù))eole.ActiveSheet.Columns(1).ColumnWidth=5
      設(shè)置指定行的高度(單位:磅)eole.ActiveSheet.Rows(1).RowHeight=1/0.035  //設(shè)定行高為1厘米,1磅=0.035厘米
      在第7行之前插入分頁符eole.Worksheets("Sheet1").Rows(7).PageBreak=1
      在第7列之前刪除分頁符eole.ActiveSheet.Columns(7).PageBreak=0
      指定邊框線寬度(Borders參數(shù)如下)eole.ActiveSheet.Range("b3:d3").Borders(2).Weight=3
      設(shè)置四個邊框線條的類型

      eole.ActiveSheet.Range("b3:d3").Borders(1).LineStyle=1 
       (其中Borders參數(shù):1-左、2-右、3-頂、4-底、5-斜、6-斜/;LineStyle
      值:1與7-細實、2-細虛、4-點虛、9-雙細實線)
      設(shè)置頁眉eole.ActiveSheet.PageSetup.CenterHeader="報表1"
      設(shè)置頁腳eole.ActiveSheet.PageSetup.CenterFooter="第&P頁"
      設(shè)置頁眉到頂端邊距為2厘米eole.ActiveSheet.PageSetup.HeaderMargin=2/0.035
      設(shè)置頁腳到底邊距為3厘米eole.ActiveSheet.PageSetup.FooterMargin=3/0.035
      設(shè)置頂邊距為2厘米eole.ActiveSheet.PageSetup.TopMargin=2/0.035
      設(shè)置底邊距為4厘米 eole.ActiveSheet.PageSetup.BottomMargin=4/0.035
      設(shè)置左邊距為2厘米eole.ActiveSheet.PageSetup.LeftMargin=2/0.035
      設(shè)置右邊距為2厘米eole.ActiveSheet.PageSetup.RightMargin=2/0.035
      設(shè)置頁面水平居中eole.ActiveSheet.PageSetup.CenterHorizontally=.t.
      設(shè)置頁面垂直居中 eole.ActiveSheet.PageSetup.CenterVertically=.t.
      設(shè)置頁面紙張大小(1-窄行8?5?11 39-寬行14?11) eole.ActiveSheet.PageSetup.PaperSize=1
      可為下列 XlPaperSize 常量之一(某些打印機可能不支持所有的這些紙張大。;
      常量 數(shù)值 意義;
      xlPaperLetter 1 Letter (8-1/2 in. x 11 in.) 
      xlPaperA3 8 A3 (297 mm x 420 mm) 
      xlPaperA4 9 A4 (210 mm x 297 mm) 
      xlPaperA4Small 10 A4 Small (210 mm x 297 mm) 
      xlPaperA5 11 A5 (148 mm x 210 mm) 
      xlPaperB4 12 B4 (250 mm x 354 mm) 
      xlPaperB5 13 B5 (182 mm x 257 mm) 
      xlPaperFanfoldUS 39 U.S. Standard Fanfold (14-7/8 in. x 11 in.) 
      xlPaperUser 用戶自定義
      打印單元格網(wǎng)線eole.ActiveSheet.PageSetup.PrintGridlines=.t.
      拷貝整個工作簿eole.ActiveSheet.UsedRange.Copy
      拷貝指定區(qū)域eole.ActiveSheet.Range("A1:E2").Copy
      粘貼eole.Worksheets("sheet2").Activate 
      eole.ActiveSheet.Range("F1").PasteSpecial
      在第2行之前插入一行eole.ActiveSheet.Rows(2).Insert
      在第2列之前插入一列eole.ActiveSheet.Columns(2).Insert
      設(shè)置字體eole.ActiveSheet.Cells(2,1).Font.Name="黑體"
      設(shè)置字體大小eole.ActiveSheet.Cells(1,1).Font.Size=25
      設(shè)置字體為斜體eole.ActiveSheet.Cells(1,1).Font.Italic=.t.
      設(shè)置整列字體為粗體eole.ActiveSheet.Columns(1).Font.Bold=.t.
      合并單元格eole.ActiveSheet.Range("A1:B4").merge 
      撤銷合并單元格,上述操作的逆操作eole.ActiveSheet.Range("A1:B4").unmerge
      在單元格中設(shè)置公式(一般可以用來實現(xiàn)計算匯總、求平均等很多功能)eole.cells(1,4).value = "=公式"
      ** 可以使用所有VBA內(nèi)部函數(shù),如sum()等。注意:不能使用VFP的函數(shù)。
      清除單元格公式eole.ActiveSheet.Cells(1,4).ClearContents
      打印預(yù)覽工作表eole.ActiveSheet.PrintPreview
      打印輸出工作表eole.ActiveSheet.PrintOut
      工作表另為eole.ActiveWorkbook.SaveAs("c:/temp/22.xls")
      放棄存盤eole.ActiveWorkbook.saved=.t.
      關(guān)閉工作簿eole.Workbooks.close 
      退出Exceleole.quit 

      以上就是本人對Excel一些微不足道的見解。寫的不周到的地方 也請給位多多擔(dān)待。

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(1)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)