NOPI 基本读写
//获取cell的数据,并设置为对应的数据类型
public object GetCellValue(ICell cell)
{
object value = null;
try
{
if (cell.CellType != CellType.Blank)
{
switch (cell.CellType)
{
case CellType.Numeric:
// Date comes here
if (DateUtil.IsCellDateFormatted(cell))
{
value = cell.DateCellValue;
}
else
{
// Numeric type
value = cell.NumericCellValue;
}
break;
case CellType.Boolean:
// Boolean type
value = cell.BooleanCellValue;
break;
case CellType.Formula:
value = cell.CellFormula;
break;
default:
// String type
value = cell.StringCellValue;
break;
}
}
}
catch (Exception)
{
value = "";
} return value;
}
//npoi读excel方法
public void ReadFromExcelFile(string filePath)
{
IWorkbook wk = null;
string extension = System.IO.Path.GetExtension(filePath);
try
{
FileStream fs = File.OpenRead(filePath);
if (extension.Equals(".xls"))
{
//把xls文件中的数据写入wk中
wk = new HSSFWorkbook(fs);
}
else
{
//把xlsx文件中的数据写入wk中
wk = new XSSFWorkbook(fs);
} fs.Close();
//读取当前表数据
ISheet sheet = wk.GetSheetAt(); IRow row;
//LastRowNum 是当前表的总行数-1(注意)
//LastCellNum 是当前行的总列数 for (int i = ; i <= sheet.LastRowNum; i++)
{
row = sheet.GetRow(i); //读取当前行数据
if (row != null && i!=)
{
DateTime value1 = DateTime.Parse( row.GetCell().ToString());
string value2 = row.GetCell().ToString();
string value3 = row.GetCell().ToString();
string value4 = row.GetCell().ToString();
string value5 = row.GetCell().ToString();
string value6 = row.GetCell().ToString();
string value7 = row.GetCell().ToString();
string value8 = row.GetCell().ToString();
Health.Model.PatientSchedule temp1 = new Health.Model.PatientSchedule();
temp1.BeginDate = value1;
temp1.BeginTime = value2;
temp1.EndTime = value3;
temp1.Content = value4;
temp1.Center = value5;
temp1.DicDept = value6;
temp1.Staves = value7;
temp1.PlanTips = value8;
temp1.ID = ;
temp1.InHosID = Datagrid1_selectedItem.InHosID;
temp1.PatientID = Datagrid1_selectedItem.PatID;
temp1.RegDatetime = DateTime.Now;
temp1.RegUser = Health.Config.UserProfiles.UserName;
temp1.State = ;
Datagrid2_ItemsSource.Add(temp1); }
}
} catch (Exception e)
{
//只在Debug模式下才输出
Console.WriteLine(e.Message);
}
}
//根据数据类型设置不同类型的cell
public static void SetCellValue(ICell cell, object obj)
{
if (obj.GetType() == typeof(int))
{
cell.SetCellValue((int)obj);
}
else if (obj.GetType() == typeof(double))
{
cell.SetCellValue((double)obj);
}
else if (obj.GetType() == typeof(IRichTextString))
{
cell.SetCellValue((IRichTextString)obj);
}
else if (obj.GetType() == typeof(string))
{
cell.SetCellValue(obj.ToString());
}
else if (obj.GetType() == typeof(DateTime))
{
cell.SetCellValue((DateTime)obj);
}
else if (obj.GetType() == typeof(bool))
{
cell.SetCellValue((bool)obj);
}
else
{
cell.SetCellValue(obj.ToString());
}
}
//npoi写excel方法
public void WriteToExcel(string filePath)
{
//创建工作薄
IWorkbook wb;
string extension = System.IO.Path.GetExtension(filePath);
//根据指定的文件格式创建对应的类
if (extension.Equals(".xls"))
{
wb = new HSSFWorkbook();
}
else
{
wb = new XSSFWorkbook();
} //创建一个表单
ISheet sheet = wb.CreateSheet("行程"); //行数和列数
int rowCount = Datagrid2_ItemsSource.Count(); IRow row; for (int i = ; i < rowCount+; i++)
{
row = sheet.CreateRow(i);//创建第i行
var cell1 = row.CreateCell();
var cell2 = row.CreateCell();
var cell3 = row.CreateCell();
var cell4 = row.CreateCell();
var cell5 = row.CreateCell();
var cell6 = row.CreateCell();
var cell7 = row.CreateCell();
var cell8 = row.CreateCell();
if (i==)
{
SetCellValue(cell1, "日期");
SetCellValue(cell2, "开始时间");
SetCellValue(cell3, "结束时间");
SetCellValue(cell4, "日程内容(流程)");
SetCellValue(cell5, "执行中心");
SetCellValue(cell6, "执行科室");
SetCellValue(cell7, "相关人员");
SetCellValue(cell8, "备注");
}
else
{
SetCellValue(cell1, Datagrid2_ItemsSource[i - ].BeginDate.ToShortDateString());
SetCellValue(cell2, Datagrid2_ItemsSource[i - ].BeginTime);
SetCellValue(cell3, Datagrid2_ItemsSource[i - ].EndTime);
SetCellValue(cell4, Datagrid2_ItemsSource[i - ].Content);
SetCellValue(cell5, Datagrid2_ItemsSource[i - ].Center);
SetCellValue(cell6, Datagrid2_ItemsSource[i - ].DicDept);
SetCellValue(cell7, Datagrid2_ItemsSource[i - ].Staves);
SetCellValue(cell8, Datagrid2_ItemsSource[i - ].PlanTips);
} } try
{
FileStream fs = File.OpenWrite(filePath);
wb.Write(fs);//向打开的这个Excel文件中写入表单并保存。
fs.Close();
}
catch (Exception e)
{
Health.Toolkit.MessageBox.Show("保存失败:"+e.Message);
}
} 时期格式设置
ICellStyle style0 = wb.CreateCellStyle();
IDataFormat dataformat = wb.CreateDataFormat();
style0.DataFormat = dataformat.GetFormat("yyyy年MM月dd日 HH:mm");
NOPI 基本读写的更多相关文章
- .Net Core NOPI操作word(一)
NOPI使用方式 1.安装nuget包 即可使用 Install-Package NPOI 一.创建word文档 //创建生成word文档 string path = "D:\\test.d ...
- c# 使用NOPI 操作Excel
最近项目需要导出Excel,找来找去,微软有自己的Excel组件 using Microsoft.Office.Core;using Microsoft.Office.Interop.Excel;,但 ...
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- 【造轮子】打造一个简单的万能Excel读写工具
大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...
- ArcGIS 10.0紧凑型切片读写方法
首先介绍一下ArcGIS10.0的缓存机制: 切片方案 切片方案包括缓存的比例级别.切片尺寸和切片原点.这些属性定义缓存边界的存在位置,在某些客户端中叠加缓存时匹配这些属性十分重要.图像格式和抗锯齿等 ...
- socket读写返回值的处理
在调用socket读写函数read(),write()时,都会有返回值.如果没有正确处理返回值,就可能引入一些问题 总结了以下几点 1当read()或者write()函数返回值大于0时,表示实际从缓冲 ...
- Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...
- mybatis plugins实现项目【全局】读写分离
在之前的文章中讲述过数据库主从同步和通过注解来为部分方法切换数据源实现读写分离 注解实现读写分离: http://www.cnblogs.com/xiaochangwei/p/4961807.html ...
- 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...
随机推荐
- 解析C#内存管理
C#内存管理解析 前言:对于很多的C#程序员来说,经常会很少去关注其内存的释放,他们认为C#带有强大的垃圾回收机制,所有不愿意去考虑这方面的事情,其实不尽然,很多时候我们都需要考虑C#内存的管理问题, ...
- Google VR技术大揭秘
VR 虚拟现实(Virtual Reality)技术是一种能够创建和体验虚拟世界的计算机仿真系统.它利用计算机生成一种模拟环境.是一种多源信息融合的.交互式的三维动态视景和实体行为的系统仿真, 使用户 ...
- 一个好汉一个帮:前端UI改造
今天是周六,继续工作中. 只是,不是自己亲自参与搞代码,让一起好的同事帮我美化界面. 虽说前端,我也可以搞定, but,but呀,所有的工作都让我来搞,实在是太累太烦了. 前端,样式,目前做很多是模仿 ...
- [SVG] Add an SVG as an Embedded Background Image
Learn how to set an elements background image to embedded SVG. This method has an added benefit of n ...
- 学习鸟哥的Linux私房菜笔记(11)——系统监视1
一.了解系统状况 uname:显示系统信息 hostname:显示主机名 last:列出最近的用户登录 lastlog:列出每一个用户的最近登录情况 free:显示内存使用状况 还可以使用vmstat ...
- selenium firefox 提取qq空间相册链接
环境: selenium-java 3.9.1 firefox 57.0 geckodriver 0.19.1 1.大概的思路就是模拟用户点击行为,关于滚动条的问题,我是模拟下拉箭头,否则只能每个相册 ...
- 【43.26%】【codeforces 732C】Sanatorium
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Android Studio 报错Guest isn't online after 7 seconds 解决方案
最近使用真机模拟之后,再使用虚拟机就频繁出现这个问题; 解决步骤如下: 1.打开Android虚拟设备管理器, 2.查看Actoins栏下拉图标, 3.选择冷启动模式即可, 4.重启AVD正常;
- GridLayout网格布局
网格布局特点: l 使容器中的各组件呈M行×N列的网格状分布. l 网格每列宽度相同,等于容器的宽度除以网格的列数. l 网格每行高度相同,等于容器的高度除以网格的行数. l 各组件的排列方式 ...
- Android--数据持久化存储概述
Android数据持久化存储共有四种方式,分别是文件存储.SharedPreferences.Sqlite数据库和ContentProvider.在本篇幅中只介绍前面三种存储方式,因为ContentP ...