国产探花免费观看_亚洲丰满少妇自慰呻吟_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; }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成武县| 双峰县| 大安市| 晋州市| 华宁县| 灵山县| 神木县| 乌兰察布市| 渝中区| 太仆寺旗| 禄丰县| 富川| 望都县| 随州市| 屏南县| 桐庐县| 婺源县| 馆陶县| 柳河县| 垣曲县| 牙克石市| 东宁县| 边坝县| 金溪县| 高雄县| 潮州市| 康乐县| 邵武市| 喜德县| 凉山| 安岳县| 石景山区| 肥东县| 全椒县| 抚顺县| 永泰县| 栖霞市| 嘉鱼县| 广西| 遵化市| 延长县|