C# 操作NPOI导入导出
//把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导入导出的更多相关文章
- c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出
c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...
- NPOI导入导出Excel
.net mvc利用NPOI导入导出excel 注意:如何导出的提交方式ajax导出是失效的! 解决方案是:js处理l两个表单的提交 代码: 第一步. 在页面里面加入2个隐藏的iframe, 如下 ...
- .Net core NPOI导入导出Excel
最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...
- NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中
以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...
- 关于docker--详解安装,常规操作,导入导出等(2017-3-29)
测试环境 :CentOS 7.1 64位 目的:展示docker的常规使用(安装,常规操作,导入导出等) 其他:关于原理等请参考文章后面的延伸阅读,本文不做深入探讨,且方法不唯一 0x01 关于安装d ...
- net core WebApi——使用NPOI导入导出操作
目录 前言 NPOI 测试 小结 @ 前言 时间过得好快,在之前升级到3.0之后,就感觉好久没再动过啥东西了,之前有问到Swagger的中文汉化,虽说我觉得这种操作的意义不是太大,也是多少鼓捣了下,其 ...
- c#.net 使用NPOI导入导出标准Excel (asp.net winform csharp)
尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...
- ASP.NET- 使用NPOI导入导出标准Excel
尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...
- 使用NPOI导入导出标准Excel
尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...
随机推荐
- PartialView 加载Js
地址记录:http://stackoverflow.com/questions/21186505/including-script-specific-to-an-asp-net-mvc4-view-o ...
- explicit 只对构造函数起作用,用来抑制隐式转换。
class A { private: int a; public: A(int x) :a(x){} void display(){ cout << a << endl; } ...
- java三层模型与三层架构
http://zhidao.baidu.com/question/316273675.html?qbl=relate_question_4 持久层用来固化数据,如常说的DAO层,操作数据库将数据入库业 ...
- HTML&CSS基础学习笔记1.17-表格的头部与尾部
表格的头部和尾部 既然有标签表示表格的主体,那么自然表格的头部和尾部也有对应的标签. HTML中使用<thead>标签表示表格的头部,使用<tfoot>标签表示表格的尾部. 有 ...
- Intent系列讲解---Intent简介以及相关属性
一.Intent简介 Intent中文是"意图,意向",它是Android中四大组件通讯的纽带,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Androi ...
- 在C语言控制台程序中播放MP3音乐
游戏没有声音多单调. 这里做一个简单的范例,用 mciSendString 函数播放 MP3 格式的音乐,先看看代码吧: // 编译该范例前,请把 background.mp3 放在项目文件夹中 // ...
- PHP 中filter_var的使用
filter_var() 函数通过指定的过滤器过滤变量. 如果成功,则返回已过滤的数据,如果失败,则返回 false. 语法 :filter_var(variable, filter, options ...
- 抗忙,,建个MAVEN的私服仓库-NEXUS
公司最近需求越来越上轨道,MAVEN的私服仓库-NEXUS构架起来哟.. 参考文档URL: http://www.linuxidc.com/Linux/2011-07/39578p3.htm http ...
- 自制单片机之十八……无线通讯模块NRF24L01+
(一)基础知识篇 今天刚调试好,先看图吧! 这张是AT89C2051控制NRF24L01+做发射调试. 看看NRF24L01细节吧! 这是LCD屏显示: AT89S52做接收测试: 正在接收时的显示: ...
- VS2015 C#6.0 中的那些新特性(转载)
自动属性初始化 (Initializers for auto-properties) 以前我们是这么写的 为一个默认值加一个后台字段是不是很不爽,现在我们可以这样写 只读属性的初始化(Getter-o ...