C#实现从数据库读取数据到Excel
用第三方组件: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的更多相关文章
- Java使用poi从数据库读取数据生成Excel表格
想要使用POI操作以xsl结尾的Excel,首先要下载poi相关的jar包,用到的jar有: poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.9.j ...
- 10天学会phpWeChat——第三天:从数据库读取数据到视图
在第二天,我们创建了我们的第一个phpWeChat功能模块,但是比较简单.实际生产环境中,我们不可能有如此简单的需求.更多的情况是数据存储在MySql数据库中,我们开发功能模块的作用就是将这些数据从M ...
- echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端
1.echarts的官网上的demo,都是直接写死的随机数据,没有和数据库的交互,所以就自己写了一下,ok,我们开始一步一步走一遍整个流程吧. 就以官网最简单的那个小demo来做修改吧.官网上的小de ...
- JMeter 参数化之利用JDBCConnectionConfiguration从数据库读取数据并关联变量
参数化之利用DBC Connection Configuration从数据库读取数据并关联变量 by:授客 QQ:1033553122 1. 下载mysql jar包 下载mysql jar包 ...
- 从数据库导出数据到excel之List<List<Object>>导出
说明:有时候数据处理为List<List<Object>>更方便 姊妹篇:从数据库导出数据到excel之List<Map<>>导出 兄弟篇:从数据库导出 ...
- 从数据库导出数据到excel之POI操作
项目说明: 1:数据库中有两张表,主键关联 2:根据条件查询数据 3:处理为需要的数据封装类型,然后传到导出excel的方法中 <--框架部署就不详谈了,用的spring框架--> 补充: ...
- 使用python脚本从数据库导出数据到excel
python从数据库导出数据到excel 最近需要从数据库里导出一些数据到excel,刚开始我是使用下面的命令 select * from xxx where xxx into outfile 'xx ...
- Android打开数据库读取数据
打开数据库读取数据 private MyDatabaseHelper dbHelper; dbHelper=new MyDatabaseHelper(this,"List.db", ...
- SQL Server数据库读取数据的DateReader类及其相关类
之前学了几天的SQL Server,现在用C#代码连接数据库了. 需要使用C#代码连接数据库,读取数据. 涉及的类有: ConfigurationManage SqlConnection SqlCom ...
随机推荐
- bzoj3669
不难想到从小到大穷举a,判断在携带不超过a个B型精灵的情况下最少携带多少个B型精灵这是一个经典的问题,显然要求出最小生成树,树上1到N路径上最大的边即是答案所以我们要动态维护一个最小生成树可以用lin ...
- bzoj1202
很久以前写的,忘补解题报告了首先似乎dfs就可以了吧?但还有更高大上的做法其实这东西就是告诉sum[y]-sum[x-1]=z然后给出一堆看成不成立可以用并查集,维护每个点到father点的差即可 . ...
- Beta Round #9 (酱油杯noi考后欢乐赛)随机数生成器
题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...
- lfs遇到的一些问题--后续阶段
1.安装GPM-1.20.7,make install出错: prog/display-buttons.c:39:57: 致命错误:gpm.h:没有那个文件或目录 #include <gpm.h ...
- JavaScript高级程序设计44.pdf
unload事件 与load事件对应的是unload事件,这个事件在文档被完全卸载后触发,只要用户从一个页面切换到另一个页面,就会发生unload事件,最多的情况是清除引用,避免内存泄漏 与load事 ...
- Apache Commons 工具集使用简介
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成的东西,我只是做了一个汇总整理. 一.Comm ...
- yii快速入门与参考
Yii 权威指南 http://www.yiichina.com/guide/ http://www.yiiframework.com/doc/guide/1.1/zh_cn/ Ⅰ.基本概念一.入口文 ...
- JAVA 面向对象-2-继承(Inheritance)
i.继承(Inheritance) 1.继承的概念 继承:在面向对象编程的过程中,通过扩展一个已有的类,并继承该类的属性和行为,来创建一个新的类. 继承是面向对象编程最重要的特征之一. 继承的优点:1 ...
- 笔记本开了WIFI之后只能上QQ,上不了网页的解决方法
前几天拉了宽带之后,开了WIFI,发现WIFI能上网,但是电脑就上不了网页. 把本地连接的DNS指定一下,(电信)指定为202.102.192.68
- text code
https://github.com/Itseez/opencv/blob/master/modules/objdetect/src/erfilter.cpp