用第三方组件:NPOI来实现

先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中添加引用。使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写。

创建一个实体类:

[Table("Customer") ]
public class Customer
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public int Gender { get; set; } }

新建一个类Customer

创建一个类去实现从List<Customer>中读取数据到Excel中

public class ExportToExcel
{
public void ExportCustomerToExcel(Stream stream, IList<Customer> customerList)
{
XSSFWorkbook workBook = new XSSFWorkbook();
ISheet workSheet = workBook.CreateSheet("Customer");
IRow currRow;
ICell currCell; workSheet.CreateFreezePane(,,,); //Excel Header
var properties = new string[] { "Id", "FirstName", "LastName", "Age", "Gender" }; ICellStyle styleHeader = GetNPOIExcelHeaderStyle(workBook); currRow = workSheet.CreateRow();
for(int i=; i < properties.Length; i++)
{
currCell = currRow.CreateCell(i);
currCell.SetCellValue(properties[i]);
currCell.CellStyle = styleHeader;
} //Excel的正文
int row = ;
int col = ;
foreach(var customer in customerList)
{
col = ;
currRow = workSheet.CreateRow(row); currRow.CreateCell(col).SetCellValue(customer.Id);
col++; currRow.CreateCell(col).SetCellValue(customer.FirstName);
col++; currRow.CreateCell(col).SetCellValue(customer.LastName);
col++; currRow.CreateCell(col).SetCellValue(customer.Age);
col++; currRow.CreateCell(col).SetCellValue(customer.Gender);
col++; row++;
} workBook.Write(stream);
} private ICellStyle GetNPOIExcelHeaderStyle(IWorkbook workbook)
{
ICellStyle styleHeader = workbook.CreateCellStyle();
IFont fontHeader = workbook.CreateFont();
fontHeader.Boldweight = (short)FontBoldWeight.Bold;
styleHeader.SetFont(fontHeader);
return styleHeader;
}
}

从List读取数据到Excel中

在main函数方法中实现从数据库读取数据,并调用ExportToExcel类中的ExportCustomerToExcel方法,将数据写入到EXCEL中

class Program
{
static void Main(string[] args)
{
string filePath = @"E:\Customer_Test.xlsx"; #region 从数据库读取数据
IList<Customer> customerList = new List<Customer>();
CodeFirstDBContext context = new CodeFirstDBContext();
var customer = context.Customer.ToList();
#endregion ExportToExcel export = new ExportToExcel();
MemoryStream ms = new MemoryStream();
export.ExportCustomerToExcel(ms, customer); using(FileStream fs = new FileStream(@"E:\Customer.xlsx",FileMode.Create,FileAccess.Write))
{
byte[] bytes = ms.ToArray();
fs.Write(bytes,,bytes.Length);
fs.Flush();
}
Console.ReadKey();
}
}

main函数

从数据库中读取数据到Excel中,已经实现

C#实现从数据库读取数据到Excel的更多相关文章

  1. Java使用poi从数据库读取数据生成Excel表格

    想要使用POI操作以xsl结尾的Excel,首先要下载poi相关的jar包,用到的jar有: poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.9.j ...

  2. 10天学会phpWeChat——第三天:从数据库读取数据到视图

    在第二天,我们创建了我们的第一个phpWeChat功能模块,但是比较简单.实际生产环境中,我们不可能有如此简单的需求.更多的情况是数据存储在MySql数据库中,我们开发功能模块的作用就是将这些数据从M ...

  3. echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端

    1.echarts的官网上的demo,都是直接写死的随机数据,没有和数据库的交互,所以就自己写了一下,ok,我们开始一步一步走一遍整个流程吧. 就以官网最简单的那个小demo来做修改吧.官网上的小de ...

  4. JMeter 参数化之利用JDBCConnectionConfiguration从数据库读取数据并关联变量

    参数化之利用DBC Connection Configuration从数据库读取数据并关联变量   by:授客 QQ:1033553122 1.   下载mysql jar包 下载mysql jar包 ...

  5. 从数据库导出数据到excel之List<List<Object>>导出

    说明:有时候数据处理为List<List<Object>>更方便 姊妹篇:从数据库导出数据到excel之List<Map<>>导出 兄弟篇:从数据库导出 ...

  6. 从数据库导出数据到excel之POI操作

    项目说明: 1:数据库中有两张表,主键关联 2:根据条件查询数据 3:处理为需要的数据封装类型,然后传到导出excel的方法中 <--框架部署就不详谈了,用的spring框架--> 补充: ...

  7. 使用python脚本从数据库导出数据到excel

    python从数据库导出数据到excel 最近需要从数据库里导出一些数据到excel,刚开始我是使用下面的命令 select * from xxx where xxx into outfile 'xx ...

  8. Android打开数据库读取数据

    打开数据库读取数据 private MyDatabaseHelper dbHelper; dbHelper=new MyDatabaseHelper(this,"List.db", ...

  9. SQL Server数据库读取数据的DateReader类及其相关类

    之前学了几天的SQL Server,现在用C#代码连接数据库了. 需要使用C#代码连接数据库,读取数据. 涉及的类有: ConfigurationManage SqlConnection SqlCom ...

随机推荐

  1. JavaScript字符串的操作

    <script> var s=new String(); var s="Hello World"; alert(s.toLowerCase(s));//转小写 aler ...

  2. [转]CharacterController与Rigidbody

    From: http://blog.csdn.net/czlilove/article/details/9139103 今天下午碰到个问题纠结了很久:人物加上了Rigidbody并使用了重力,遇到悬崖 ...

  3. 【转】iOS开发:开发证书知识点总结

    原文网址:http://www.jianshu.com/p/9c166a5e4930 1. Error: An App ID with identifier "*" is not ...

  4. nini

    using Nini.Ini; using Nini.Config; IniDocument doc = new IniDocument("Settings.ini", IniFi ...

  5. AppScan修复漏洞:启用不安全的HTTP方法

    最近对于系统使用Appscan扫描出中危漏洞“启用不安全的HTTP方法,找了很多修复方法都不能达到效果. 漏洞截图: 漏洞描述: 危险级别   中危险 影响页面   整个WEB页面. 简短描述   管 ...

  6. win7常用键

    (1)xp和win7中都可以使用Alt+Tab中进行标签切换,win7中添加了Wins+Tab可以进行3D标签切换. (2)你知道怎样一次过调整显示器亮度.音量大小,打开无线网,还能够看到本本电池电量 ...

  7. HDOJ/HDU 1556 Color the ball(树状数组)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...

  8. Windows 8.1中怎么启用Framework3.5或2.0 ( 一安装就跳到下载 Win8.1自带了Framework)

    Windows 8.1中怎么启用Framework3.5或2.0      ( 一安装就跳到下载 Win8.1自带了Framework): Win+X键 打开   开始菜单 -> 命令提示符(管 ...

  9. 让你的Xcode8 支持 iOS7

    Xcode8 发布,更新后,发现支持从iOS8开始,可是公司要求从iOS7开始,,,这和苹果相悖. 不过没关系. 跳转 www.cnblogs.com/starainDou/p/5325643.htm ...

  10. 8-6-Exercise

    HDU 1003    Max Sum 题意:给出一串数字,求出其中某段连续的数字之和最大的值,同时要输出起点的位置和终点的位置~~~ 方法一: 用sum记录某一段和的值,maxx为目前为止最大的su ...