c#生成excel示例sql數(shù)據(jù)庫(kù)導(dǎo)出excel
更新時(shí)間:2014年01月11日 11:31:39 作者:
這篇文章主要介紹了c#操作excel的示例,里面的方法可以直接導(dǎo)出數(shù)據(jù)到excel,大家參考使用吧
復(fù)制代碼 代碼如下:
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>
/// 直接導(dǎo)出數(shù)據(jù)到excel
/// </summary>
/// <param name="connectionString">連接字符串</param>
/// <param name="sql">查詢語(yǔ)句</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();//關(guān)閉進(jìn)程
}
}
}
class objtype
{
public string name { get; set; }
public string val { get; set; }
}
}
您可能感興趣的文章:
- c#將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)Excel表數(shù)據(jù)導(dǎo)入Sql Server數(shù)據(jù)庫(kù)中的方法
- C#實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入到SQL server數(shù)據(jù)庫(kù)
- C#實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)庫(kù)數(shù)據(jù)到Excel文件
- .NET使用C#導(dǎo)入Excel文件數(shù)據(jù)到數(shù)據(jù)庫(kù)
- 使用C#實(shí)現(xiàn)Excel實(shí)時(shí)讀取并導(dǎo)入SQL數(shù)據(jù)庫(kù)
- 在VS2015中使用C#操作數(shù)據(jù)庫(kù)并實(shí)現(xiàn)Excel報(bào)表導(dǎo)出與打印的具體過(guò)程
相關(guān)文章
C# VTK 移動(dòng)旋轉(zhuǎn)交互功能實(shí)現(xiàn)
對(duì)vtk場(chǎng)景中一個(gè)或多個(gè)選中物體進(jìn)行移動(dòng)旋轉(zhuǎn),今天通過(guò)本文給大家分享C# VTK 移動(dòng)旋轉(zhuǎn)交互功能實(shí)現(xiàn),感興趣的朋友跟隨小編一起看看吧2024-06-06
C#中一個(gè)高性能異步socket封裝庫(kù)的實(shí)現(xiàn)思路分享
下面小編就為大家分享一篇C#中一個(gè)高性能異步socket封裝庫(kù)的實(shí)現(xiàn)思路,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
C#快速查找并高亮Word文檔中的文字(普通查找+正則表達(dá)式)
在日常辦公自動(dòng)化、文檔審核或數(shù)據(jù)提取場(chǎng)景中,我們經(jīng)常需要在大量的 Word 文檔中定位特定的關(guān)鍵詞或符合某種模式的文本,并將其高亮顯示以便快速查閱,本文將通過(guò)兩個(gè)具體示例,分別演示如何利用該庫(kù)進(jìn)行普通字符串查找和正則表達(dá)式模式匹配的高亮實(shí)現(xiàn)2026-03-03
在Windows 7 SP1環(huán)境下使用C#阻止窗口關(guān)閉的三種方法
本文介紹了在Windows7SP1環(huán)境下使用C#阻止窗口關(guān)閉的三種方法,包括處理FormClosing事件、攔截系統(tǒng)關(guān)閉消息和禁用關(guān)閉按鈕,每種方法都有其特點(diǎn)和適用場(chǎng)景,需要的朋友可以參考下2026-03-03
在WPF中實(shí)現(xiàn)平滑滾動(dòng)的方法詳解
這篇文章介紹了WPF實(shí)現(xiàn)平滑滾動(dòng)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C#中創(chuàng)建統(tǒng)一API接口的實(shí)現(xiàn)方案
在 C# 中創(chuàng)建統(tǒng)一 API 接口需要從架構(gòu)設(shè)計(jì)、技術(shù)選型和代碼實(shí)現(xiàn)等多個(gè)層面進(jìn)行規(guī)劃,本文給大家詳細(xì)介紹了實(shí)現(xiàn)方案和完整示例代碼,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-04-04

