国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#生成excel示例sql數據庫導出excel

2020-01-24 02:55:33
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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; }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广灵县| 陵川县| 从江县| 福安市| 博白县| 思南县| 洛宁县| 新郑市| 鄂伦春自治旗| 竹北市| 洪江市| 阳朔县| 饶平县| 孝感市| 南靖县| 汉沽区| 普洱| 蓬溪县| 于都县| 元江| 金华市| 富源县| 黄骅市| 微山县| 开平市| 土默特右旗| 雷州市| 新野县| 黑水县| 吉隆县| 砚山县| 塔城市| 米脂县| 遂宁市| 泰兴市| 伊吾县| 翼城县| 海安县| 杭州市| 涿鹿县| 新乐市|