91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#從數據庫導出數據[excel]

發布時間:2020-07-01 03:08:05 來源:網絡 閱讀:884 作者:bigSoul 欄目:編程語言

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using MySql.Data.MySqlClient;

using Microsoft.Office.Interop.Excel;

using Excel = Microsoft.Office.Interop.Excel;   //使用命名空間別名

using System.Reflection;

 

namespace _01

{

    class Tools

    {

        static void Main(string[]args)

        {

            if(ExportDataTableToExcel(SqlHelper.ExecuteTable("SELECT * FROM students", CommandType.Text, null),@"E:\03.xls"))

            {

                Console.WriteLine("OK!");

            }

            else{

                Console.WriteLine("Fail!");

            }

            Console.ReadKey();

        }

 

        public static boolExportDataTableToExcel(System.Data.DataTable dt, stringfilepath)

         {

 

            Excel.ApplicationoXL;          //應用

            Excel.Workbook oWB;             //工作簿

            Excel.WorksheetoSheet;         //工作表

            Excel.Range oRange;             //單元格

            try

            {

                // Start Excel and get Application object.

                oXL = new Excel.Application();              //啟動并且創建對象

                // Set some properties 

                oXL.Visible = true;

                oXL.DisplayAlerts = false;                  //設置警告信息的默認值

                // Get a new workbook.

                oWB = oXL.Workbooks.Add(Missing.Value);

                // Get the Active sheet

                oSheet = (Excel.Worksheet)oWB.ActiveSheet;

                oSheet.Name = "Data";

                introwCount = 1;

                foreach(DataRow dr in dt.Rows)

                {

                    rowCount += 1;

                    for(int i = 1; i < dt.Columns.Count +1; i++)

                    {

                        // Add the header the first timethrough

                        if (rowCount ==2)

                        {

                            oSheet.Cells[1, i] = dt.Columns[i -1].ColumnName;

                        }

                        oSheet.Cells[rowCount, i] = dr[i - 1].ToString();

                    }

                }

 

                // Resize the columns

                oRange = oSheet.Range[oSheet.Cells[1,1],

                              oSheet.Cells[rowCount, dt.Columns.Count]];

                oRange.EntireColumn.AutoFit();

 

                // Save the sheet and close

                oSheet = null;

                oRange = null;

                oWB.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal,

                    Missing.Value, Missing.Value,Missing.Value, Missing.Value,

                    Excel.XlSaveAsAccessMode.xlExclusive,

                    Missing.Value, Missing.Value,Missing.Value,

                    Missing.Value, Missing.Value);

                oWB.Close(Missing.Value,Missing.Value, Missing.Value);

                oWB = null;

                oXL.Quit();

            }

            catch

            {

                throw;

            }

            finally

            {

                // Clean up

                // NOTE: When in release mode, this does thetrick

                GC.WaitForPendingFinalizers();

                GC.Collect();

                GC.WaitForPendingFinalizers();

                GC.Collect();

            }

            returntrue;

        }

    }

}

Tips:SqlHelper未提供。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

启东市| 木兰县| 黑山县| 涞源县| 诏安县| 玉溪市| 陆川县| 贵定县| 阜新| 连南| 本溪市| 彰化市| 美姑县| 洛隆县| 南澳县| 含山县| 嘉峪关市| 株洲市| 汪清县| 雅安市| 万盛区| 温宿县| 裕民县| 连城县| 克拉玛依市| 南昌县| 荃湾区| 宜君县| 牟定县| 黄龙县| 武宣县| 绵竹市| 嘉鱼县| 乳山市| 故城县| 武安市| 通海县| 瑞金市| 平谷区| 衡山县| 图们市|