如何数据库表数据导出到excel中
1.首先须要有一个NPOI
2.接下来上代码
private void button1_Click(object sender, EventArgs e)
{
//1.通过Ado.net读取数据
string sql = "SELECT * FROM ASRS_F1";
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, CommandType.Text))
{
//如果读到数据
if (reader.HasRows)
{
IWorkbook wk = new HSSFWorkbook();
ISheet sheet = wk.CreateSheet("ASRS_F1"); #region 创建第一行,设置列名
//--------------------------------------------------
//创建第一行,第一行表示列名
IRow rowHead = sheet.CreateRow();
//循环查询出的每一列
for (int col = ; col < reader.FieldCount; col++)
{
rowHead.CreateCell(col).SetCellValue(reader.GetName(col));
}
//--------------------------------------------------
#endregion int rindex = ;
//下面是创建数据行
while (reader.Read())
{
//ID, Position, AName, ACode, AState, ABatch, ADateTime, BName, BCode, BState, BBatch, BDateTime, IsMoving, IsType
IRow currentRow = sheet.CreateRow(rindex);
rindex++;
int Id = reader.GetInt32();
string Position = reader.GetString();
string AName = reader.GetString();
string ACode = reader.GetString();
string AState = reader.GetString();
string ABatch =reader.IsDBNull()?null: reader.GetString();
DateTime? lockDate = reader.IsDBNull() ? null : (DateTime?)reader.GetDateTime();
string BName = reader.GetString();
string BCode = reader.GetString();
string BState = reader.GetString();
string BBatch = reader.IsDBNull()?null:reader.GetString();
DateTime? lockDates = reader.IsDBNull() ? null : (DateTime?)reader.GetDateTime();
string IsMoving = reader.GetString();
string IsType = reader.GetString(); currentRow.CreateCell().SetCellValue(Id);
currentRow.CreateCell().SetCellValue(Position);
currentRow.CreateCell().SetCellValue(AName);
currentRow.CreateCell().SetCellValue(ACode);
currentRow.CreateCell().SetCellValue(AState);
currentRow.CreateCell().SetCellValue(ABatch);
if (lockDate == null)
{
//如果是null值,那么就像excel写入一个单元格,这个单元格的类型就是Blank
currentRow.CreateCell().SetCellType(CellType.BLANK);
}
else
{ //创建一个单元格
ICell cellLockDate = currentRow.CreateCell(); //创建一个单元格样式
ICellStyle cellStyle = wk.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("m/d/yy h:mm");
//设置当前单元格应用cellStyle样式
cellLockDate.CellStyle = cellStyle; cellLockDate.SetCellValue((DateTime)lockDate);
}
currentRow.CreateCell().SetCellValue(BName);
currentRow.CreateCell().SetCellValue(BCode);
currentRow.CreateCell().SetCellValue(BState);
currentRow.CreateCell().SetCellValue(BBatch); if (lockDates == null)
{
//如果是null值,那么就像excel写入一个单元格,这个单元格的类型就是Blank
currentRow.CreateCell().SetCellType(CellType.BLANK);
}
else
{ //创建一个单元格
ICell cellLockDate = currentRow.CreateCell(); //创建一个单元格样式
ICellStyle cellStyle = wk.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("m/d/yy h:mm");
//设置当前单元格应用cellStyle样式
cellLockDate.CellStyle = cellStyle; cellLockDate.SetCellValue((DateTime)lockDates);
}
currentRow.CreateCell().SetCellValue(IsMoving);
currentRow.CreateCell().SetCellValue(IsType);
} //写入
using (FileStream fsWrite = File.OpenWrite("半成品库存表.xls"))
{
wk.Write(fsWrite);
}
label1.Text = "写入成功!" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
label1.Text = "没有查询到任何数据";
}
} }
如何数据库表数据导出到excel中的更多相关文章
- 使用POI把查询到的数据表数据导出到Excel中,一个表一个sheet.最详细!!!
一.需求 我们会遇到开发任务: 经理:小王,你来做一下把数据库里的数据导出到Excel中,一个表是一个sheet,不要一个表一个Excel. 小王:好的,经理.(内心一脸懵逼) 二.前期准备 首先我们 ...
- Python小练习_数据库表数据导出到excel
需求:只要传入一个表名,就能把所有的数据导入出来,字段名是excel的表头 1.要动态获取到标的字段 cur.descrption能获取到表的字段 fileds = [filed[0] for fil ...
- 如何将存储在MongoDB数据库中的数据导出到Excel中?
将MongoDB数据库中的数据导出到Excel中,只需以下几个步骤: (1)首先,打开MongoDB安装目录下的bin文件夹,(C:\Program Files (x86)\MongoDB\Serve ...
- 将Datagridview中的数据导出至Excel中
首先添加一个模块ImportToExcel,并添加引用 然后导入命名空间: Imports Microsoft.Office.Interop Imports System.Da ...
- Pl/sql 如何将oracle的表数据导出成excel文件?
oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件-- ; 结果视图 2)在查询结果的空白处,右键选择Copy to Excel 3) 查看导出e ...
- 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>
前台datagrid数据绑定 #region 导出到excel中 /// <summary> /// 2014-6-6 /// </summary> / ...
- Magic xpa 3.x很容易将数据导出到Excel中
Magic xpa 3.x很方便的将表中数据导出到Excel文件中,还可以自动将表中数据生成各种图表. 通过使用自带的打印数据内部事即可实现. 1.首先将打印数据任务属性设置为“是”. 2.可使用主程 ...
- 数据导出到Excel中
自己修改后的一个数据导出到Excel的方法,粘出来与大家共享. 只需要将 System.Web.HttpContext.Current.Response.Charset = ...
- Python 把数据库的数据导出到excel表
import io,xlwt def export_excel(request): """导出数据到excel表""" list_obj = ...
随机推荐
- win7下利用ftp实现华为路由器的上传和下载
win7下利用ftp实现华为路由器的上传和下载 1. Win7下ftp的安装和配置 (1)开始->控制面板->程序->程序和功能->打开或关闭Windows功能 (2)在Wi ...
- Xcode 锁终端
锁终端 输入: <1>cd /Applications/Xcode.app 回车 结果显示: Xcode.app 输入: <2>sudo chown -hR root:whee ...
- Java 教程整理:基础、项目全都有
Java 在编程语言排行榜中一直位列前排,可知 Java 语言的受欢迎程度了. 网上有很多 Java 教程,无论是基础入门还是开发小项目的教程都比比皆是,可是系统的很少,对于Java 学习者来说找到系 ...
- 解决:win10_x64 VMware Workstation and Hyper-V are not compatible. Remove the Hyper-V role from the system before running VMware Workstation
bcdedit /set hypervisorlaunchtype off A reboot of of the Windows OS is necessary 必须重启才能生效 To enab ...
- Ubuntu设置root用户登录图形界面
Ubuntu默认的是root用户不能登录图形界面的,只能以其他用户登录图形界面.这样就很麻烦,因为权限的问题,不能随意复制删除文件,用gedit编辑文件时经常不能保存,只能用vim去编辑. 解决的办法 ...
- 如何开启MySQL 5.7.12 的二进制日志
1. 打开/etc下的my.cnf文件 2. 编辑它,添加内容: log_bin=binary-log #二进制日志的文件名 server_id=1 #必须指定server_id,这是MySQL ...
- MyBatis5:MyBatis集成Spring事物管理(上篇)
前言 有些日子没写博客了,主要原因一个是工作,另一个就是健身,因为我们不仅需要努力工作,也需要有健康的身体嘛. 那有看LZ博客的网友朋友们放心,LZ博客还是会继续保持更新,只是最近两三个月LZ写博客相 ...
- 《AngularJS深度剖析与最佳实践》简介
由于年末将至,前阵子一直忙于工作的事务,不得已暂停了微信订阅号的更新,我将会在后续的时间里尽快的继续为大家推送更多的博文.毕竟一个人的力量微薄,精力有限,希望大家能理解,仍然能一如既往的关注和支持sh ...
- 使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(二)
前言 上一篇随笔Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(一)介绍maven和nexus的环境搭建,以及如何使用maven和nexus统一管理库 ...
- Linex 配置php服务器
此文是可以参考 楼主也不是系统管理员只是迫不得已所以自己才找的 大家可以参考 .... ..... 安装apache 安装mysql 安装PHP 测试服务器 php -v 查询php的版本 就这些了 ...