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中的更多相关文章

  1. 使用POI把查询到的数据表数据导出到Excel中,一个表一个sheet.最详细!!!

    一.需求 我们会遇到开发任务: 经理:小王,你来做一下把数据库里的数据导出到Excel中,一个表是一个sheet,不要一个表一个Excel. 小王:好的,经理.(内心一脸懵逼) 二.前期准备 首先我们 ...

  2. Python小练习_数据库表数据导出到excel

    需求:只要传入一个表名,就能把所有的数据导入出来,字段名是excel的表头 1.要动态获取到标的字段 cur.descrption能获取到表的字段 fileds = [filed[0] for fil ...

  3. 如何将存储在MongoDB数据库中的数据导出到Excel中?

    将MongoDB数据库中的数据导出到Excel中,只需以下几个步骤: (1)首先,打开MongoDB安装目录下的bin文件夹,(C:\Program Files (x86)\MongoDB\Serve ...

  4. 将Datagridview中的数据导出至Excel中

        首先添加一个模块ImportToExcel,并添加引用         然后导入命名空间: Imports Microsoft.Office.Interop Imports System.Da ...

  5. Pl/sql 如何将oracle的表数据导出成excel文件?

    oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件-- ; 结果视图 2)在查询结果的空白处,右键选择Copy to Excel 3) 查看导出e ...

  6. 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>

    前台datagrid数据绑定 #region 导出到excel中    /// <summary>    /// 2014-6-6    /// </summary>    / ...

  7. Magic xpa 3.x很容易将数据导出到Excel中

    Magic xpa 3.x很方便的将表中数据导出到Excel文件中,还可以自动将表中数据生成各种图表. 通过使用自带的打印数据内部事即可实现. 1.首先将打印数据任务属性设置为“是”. 2.可使用主程 ...

  8. 数据导出到Excel中

    自己修改后的一个数据导出到Excel的方法,粘出来与大家共享. 只需要将             System.Web.HttpContext.Current.Response.Charset =   ...

  9. Python 把数据库的数据导出到excel表

    import io,xlwt def export_excel(request): """导出数据到excel表""" list_obj = ...

随机推荐

  1. WEB安全隐患

    org.apache.commons.lang.StringEscapeUtils 进行输入框内容处理 [StringEscapeUtils.escapeSql(str);StringEscapeUt ...

  2. Android之网络数据存储

    一.网络保存数据介绍 可以使用网络来保存数据,在需要的时候从网络上获取数据,进而显示在App中. 用网络保存数据的方法有很多种,对于不同的网络数据采用不同的上传与获取方法. 本文利用LeanCloud ...

  3. Oracle 10g安装教程

    首先下载安装文件,打开后文件结构如图所示: 安装之前请关闭Windows防火墙并断开网络. xp系统下直接双击运行(本经验以XP系统安装为例进行讲述). 如果是在win7上安装,如图:在setup文件 ...

  4. 我的MYSQL学习心得(六) 函数

    我的MYSQL学习心得(六) 函数 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类 ...

  5. 使用Xamarin开发iOS7应用时隐藏StatusBar方法

    在iOS7之前如果需要隐藏StatusBar,比较简单,直接在AppDelegate.cs中使用如下代码就可以进行隐藏: UIApplication.SharedApplication.StatusB ...

  6. 当前端也拥有 Server 的能力

    今天看了不少文章,比较感兴趣的是 Cache API.它是浏览器 Request/Response 的缓存管理工具,其使用风格和运用场景让我瞬间联想到了 ServiceWorker 和 Fetch A ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Restful WebApi项目开发实践

    前言 踩过了一段时间的坑,现总结一下,与大家分享,愿与大家一起讨论. Restful WebApi特点 WebApi相较于Asp.Net MVC/WebForm开发的特点就是前后端完全分离,后端使用W ...

  9. ABP(现代ASP.NET样板开发框架)系列之13、ABP领域层——数据过滤器(Data filters)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之13.ABP领域层——数据过滤器(Data filters) ABP是“ASP.NET Boilerplate P ...

  10. ZeroMQ:云时代极速消息通信库

    ZeroMQ:云时代极速消息通信库(大规模|可扩展|低成本|高效率解决之道,大规模分布式|多线程应用程序|消息传递架构构建利器) [美]Pieter Hintjens(皮特.亨特金斯)著   卢涛 李 ...