//把T_Seats中的输入导出到Excel
private void button3_Click(object sender, EventArgs e)
{
//1.读取
string sql = "select * from T_Seats";
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, CommandType.Text))
{
if (reader.HasRows)
{ //创建Workbook
IWorkbook wk = new HSSFWorkbook(); //创建Sheet
ISheet sheet = wk.CreateSheet("T_Seats"); int rowIndex = ; #region 读取并创建每一行 //读取每一条数据
while (reader.Read())
{
//CC_AutoId, CC_LoginId, CC_LoginPassword, CC_UserName, CC_ErrorTimes, CC_LockDateTime, CC_TestInt
int autoId = reader.GetInt32();
string uid = reader.GetString();
string pwd = reader.GetString();
string name = reader.GetString();
int errorTimes = reader.GetInt32();
DateTime? lockDate = reader.IsDBNull() ? null : (DateTime?)reader.GetDateTime();
int? testInt = reader.IsDBNull() ? null : (int?)reader.GetInt32(); IRow row = sheet.CreateRow(rowIndex);
rowIndex++; //像行中创建单元格
row.CreateCell().SetCellValue(autoId);
row.CreateCell().SetCellValue(uid);
row.CreateCell().SetCellValue(pwd);
row.CreateCell().SetCellValue(name);
row.CreateCell().SetCellValue(errorTimes); //对于数据库中的空值,向单元格中插入空内容
ICell cellLockDate = row.CreateCell();
if (lockDate == null)
{
//设置单元格的数据类型为Blank,表示空单元格
cellLockDate.SetCellType(CellType.BLANK);
}
else
{
cellLockDate.SetCellValue((DateTime)lockDate); //创建一个单元格格式对象
ICellStyle cellStyle = wk.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("m/d/yy h:mm");
//设置当前日期这个单元格的是CellStyle属性
cellLockDate.CellStyle = cellStyle; }
ICell cellTestInt = row.CreateCell();
if (testInt == null)
{
cellTestInt.SetCellType(CellType.BLANK);
}
else
{
cellTestInt.SetCellValue((int)testInt);
} }
#endregion //将Excel写入文件
using (FileStream fsWrite = File.OpenWrite("tseats.xls"))
{
wk.Write(fsWrite);
}
}
} MessageBox.Show("操作完毕!");
//2.写Excel
} //把Excel的内容导入到数据库表T_Seats
private void button4_Click(object sender, EventArgs e)
{
using (FileStream fsRead = File.OpenRead("tseats.xls"))
{
//1.读取Excel
IWorkbook wk = new HSSFWorkbook(fsRead);
ISheet sheet = wk.GetSheetAt(); string sql_insert = "insert into T_Seats values(@uid,@pwd,@uname,@errorTimes,@lockDate,@testint)"; //读取sheet中的每一行
for (int r = ; r <= sheet.LastRowNum; r++)
{ //读取每行
IRow row = sheet.GetRow(r);
//读取除了第一列的其他几列
string loginId = row.GetCell().StringCellValue;
string password = row.GetCell().StringCellValue;
string username = row.GetCell().StringCellValue;
int errorTimes = (int)row.GetCell().NumericCellValue;
double? lockDate = null;
ICell cellLockDate = row.GetCell();
if (cellLockDate != null && cellLockDate.CellType != CellType.BLANK)
{
lockDate = row.GetCell().NumericCellValue;
}
else
{
//lockDate = null;
} int? testInt = null;
ICell cellTestInt = row.GetCell(); if (cellTestInt != null && cellTestInt.CellType != CellType.BLANK)
{
testInt = (int)cellTestInt.NumericCellValue;
}
else
{
//testInt = null;
} SqlParameter[] pms = new SqlParameter[] {
new SqlParameter("@uid",loginId),
new SqlParameter("@pwd",password),
new SqlParameter("@uname",username),
new SqlParameter("@errorTimes",errorTimes), new SqlParameter("@lockDate",lockDate==null?DBNull.Value:(object)DateTime.FromOADate((double)lockDate)),
new SqlParameter("@testint",testInt==null?DBNull.Value:(object)testInt),
};
//执行插入操作
SqlHelper.ExecuteNonQuery(sql_insert, CommandType.Text, pms);
}
}
MessageBox.Show("ok"); //2.向表T_Seats执行insert语句
}
}

C# 操作NPOI导入导出的更多相关文章

  1. c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出

    c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...

  2. NPOI导入导出Excel

    .net mvc利用NPOI导入导出excel 注意:如何导出的提交方式ajax导出是失效的! 解决方案是:js处理l两个表单的提交  代码:  第一步. 在页面里面加入2个隐藏的iframe, 如下 ...

  3. .Net core NPOI导入导出Excel

    最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...

  4. NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中

    以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...

  5. 关于docker--详解安装,常规操作,导入导出等(2017-3-29)

    测试环境 :CentOS 7.1 64位 目的:展示docker的常规使用(安装,常规操作,导入导出等) 其他:关于原理等请参考文章后面的延伸阅读,本文不做深入探讨,且方法不唯一 0x01 关于安装d ...

  6. net core WebApi——使用NPOI导入导出操作

    目录 前言 NPOI 测试 小结 @ 前言 时间过得好快,在之前升级到3.0之后,就感觉好久没再动过啥东西了,之前有问到Swagger的中文汉化,虽说我觉得这种操作的意义不是太大,也是多少鼓捣了下,其 ...

  7. c#.net 使用NPOI导入导出标准Excel (asp.net winform csharp)

    尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...

  8. ASP.NET- 使用NPOI导入导出标准Excel

    尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...

  9. 使用NPOI导入导出标准Excel

    尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...

随机推荐

  1. mysql 主从搭建

    主要搭建步骤如下: 1.打开binlog,设置server_id     打开主库的--log-bin,并设置server_id 2.主库授权                --最好也在从库对主库授权 ...

  2. java学习笔记 (5) —— Struts2 监听器配置

    1.创建MyListener.java 实现 PreResultLisener 接口 import com.opensymphony.xwork2.ActionInvocation; import c ...

  3. windows下配置lamp环境(5)---配置MySQL5.6

    开始配置mysql 1.创建配置文件my.ini   1.进入C:\wamp\MySQL   2.把my-default.ini 另存一份:my.ini   3.开始编辑mysql的配置文件,打开my ...

  4. 使用redis缓存加索引处理数据库百万级并发

    使用redis缓存加索引处理数据库百万级并发 前言:事先说明:在实际应用中这种做法设计需要各位读者自己设计,本文只提供一种思想.准备工作:安装后本地数redis服务器,使用mysql数据库,事先插入1 ...

  5. new Handler()和new Handler(Looper.getMainLooper())的区别

    一个帖子的整理: Handler一定要在主线程实例化吗?new Handler()和new Handler(Looper.getMainLooper())的区别如果你不带参数的实例化:Handler ...

  6. Codeforces 478D Red-Green Towers

    http://codeforces.com/problemset/problem/478/D 思路:dp:f[i][j]代表当前第i层,用了j个绿色方块的方案数,用滚动数组,还有,数组清零的时候一定要 ...

  7. LeetCode_sqrt(x)

    class Solution { public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT write ...

  8. Ruby的语法糖

    发现Ruby的语法糖好多,比如函数调用,参数列表可以写括号和不写括号.代码块可以用do end 或者 {}.   还有 if,unless后置.等等. 如果看Ruby代码看多了,你会发现,它很多地方的 ...

  9. 程序错误[C/C++]

    对于初学者而言,一般意义上,程序错误可以分为两类,逻辑错误和非逻辑错误.前者是指,程序可以通过编译或链接但运行时不符合预期结果,后者是程序不能通过编译或链接. 乍一看这样的分类非常清楚.不过,当引入语 ...

  10. 【转】Windows 7/8/8.1 硬盘安装法实现 ubuntu 14.04 双系统

    原文网址:http://www.cnblogs.com/chenguangqiao/p/4219532.html 一.软件准备 1. 下载 Ubuntu 系统镜像:http://www.ubuntu. ...