C#从数据库导出数据[excel]

发布时间:2020-07-01 03:08:05 作者:bigSoul
来源:网络 阅读:884

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未提供。

推荐阅读:
  1. ZKEYS与云谷IDCSystem主机管理系统哪个好?
  2. 云计算教程学习路线视频源码课件:Linux性能监控工具-gtop

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

xlsx ce

上一篇:Oracle——redo+undo总结

下一篇:Jquery实现的tab效果可以指定默认显示第几页

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》