c#生成excel示例sql数据库导出excel

时间:2024-01-26 16:55:07 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace ListToExcel
{
    class Program
    {
        static List<objtype> objs = new List<objtype>();
        static void Main(string[] args)
        {
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            objs.Add(new objtype() { name = "allen", val = "aaa" });
            ExportDataToExcel("", "", @"c:\a.xls", "a");
        }
        /// <summary>
        /// 直接导出数据到excel
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        /// <param name="sql">查询语句</param>
        /// <param name="fileName">文件名</param>
        /// <param name="sheetName">表名</param>
        static void ExportDataToExcel(string connectionString, string sql, string fileName, string sheetName)
        {
            Application app = new Application();
            Workbook wb = app.Workbooks.Add(Missing.Value);
            Worksheet ws = wb.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Worksheet;
            ws.Name = sheetName;
            try
            {
                int n = 0;

                for (int i = 1; i < objs.Count; i++)
                {
                      var excelRange = (Range)ws.Cells[i, 1];
                   excelRange.Value2 = objs[i].val;//Value2?
                   excelRange = null;
                }
            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
            finally
            {
                wb.Saved = true;
                wb.SaveCopyAs(fileName);//保存
                app.Quit();//关闭进程
            }
        }
    }
    class objtype
    {
        public string name { get; set; }
        public string val { get; set; }
    }
}

标签:sql数据库导出excel
0
投稿

猜你喜欢

  • MySQL性能优化的一些技巧帮助你的数据库

    2024-01-20 12:44:22
  • Python 按比例获取样本数据或执行任务的实现代码

    2023-01-10 09:48:16
  • Python库学习Tkinter制作GUI个性签名设计软件

    2021-06-23 08:17:54
  • ASP JSON类源码

    2011-04-30 16:38:00
  • Python创建模块及模块导入的方法

    2023-04-21 03:42:03
  • Python 删除连续出现的指定字符的实例

    2023-11-21 08:36:15
  • Oracle存储过程之数据库中获取数据实例

    2009-03-04 10:57:00
  • 如何用OleDbDataAdapter来对数据库进行操作?

    2010-06-12 12:56:00
  • Python 绘图和可视化详细介绍

    2021-02-16 18:17:31
  • 用Python下载一个网页保存为本地的HTML文件实例

    2023-04-15 18:41:53
  • 详解pandas获取Dataframe元素值的几种方法

    2022-12-28 07:30:01
  • 微信小程序如何处理token过期问题

    2023-07-02 05:23:54
  • golang中for range的取地址操作陷阱介绍

    2024-04-23 09:36:39
  • 绿色下划线的简洁CSS导航代码

    2007-09-17 12:51:00
  • vue阻止页面回退的实现方法(浏览器适用)

    2024-06-07 15:24:10
  • Golang嵌入资源文件实现步骤详解

    2023-06-21 08:52:36
  • 类型为search的input及相关属性

    2009-02-11 12:49:00
  • 段正淳的css笔记(4)css代码的简写

    2007-11-01 22:03:00
  • Python元素集合的列表切片

    2023-08-08 23:08:57
  • Python with语句上下文管理器两种实现方法分析

    2023-03-21 21:50:18
  • asp之家 网络编程 m.aspxhome.com