//获取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 基本读写的更多相关文章

  1. .Net Core NOPI操作word(一)

    NOPI使用方式 1.安装nuget包 即可使用 Install-Package NPOI 一.创建word文档 //创建生成word文档 string path = "D:\\test.d ...

  2. c# 使用NOPI 操作Excel

    最近项目需要导出Excel,找来找去,微软有自己的Excel组件 using Microsoft.Office.Core;using Microsoft.Office.Interop.Excel;,但 ...

  3. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  4. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  5. ArcGIS 10.0紧凑型切片读写方法

    首先介绍一下ArcGIS10.0的缓存机制: 切片方案 切片方案包括缓存的比例级别.切片尺寸和切片原点.这些属性定义缓存边界的存在位置,在某些客户端中叠加缓存时匹配这些属性十分重要.图像格式和抗锯齿等 ...

  6. socket读写返回值的处理

    在调用socket读写函数read(),write()时,都会有返回值.如果没有正确处理返回值,就可能引入一些问题 总结了以下几点 1当read()或者write()函数返回值大于0时,表示实际从缓冲 ...

  7. Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...

  8. mybatis plugins实现项目【全局】读写分离

    在之前的文章中讲述过数据库主从同步和通过注解来为部分方法切换数据源实现读写分离 注解实现读写分离: http://www.cnblogs.com/xiaochangwei/p/4961807.html ...

  9. 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库

    57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...

随机推荐

  1. ReentrantLock Condition await signal 专题

    Condition的执行方式,是当在线程T1中调用await方法后,线程T1将释放锁,并且将自己阻塞,等待唤醒, 线程T2获取到锁后,开始做事,完毕后,调用Condition的signal方法,唤醒线 ...

  2. [Vue] Update Attributes, Classes and Styles in Vue.js with v-bind

    Use v-bind:class and v-bind:style to compute html classes and inline styles from component data. Vue ...

  3. 从0到1打造直播 App(直播流程介绍整理 <mark><转>)

    注明:原创 2016-10-27 李智文 腾讯Bugly 概要 分享内容: 互联网内容载体变迁历程,文字——图片/声音——视频——VR/AR——……..从直播1.0秀场时代(YY),2.0游戏直播(斗 ...

  4. RESET MASTER 和RESET SLAVE 命令的使用方法 注意事项

    RESET MASTER 删除所有index file 中记录的所有binlog 文件,将日志索引文件清空,创建一个新的日志文件,这个命令通常仅仅用于第一次用于搭建主从关系的时的主库,注意  rese ...

  5. Photoshop怎么实现图片局部马赛克

    学好ps是一件很重要的事情,作为日常必备技能,不管是在遇到这样的同时请求帮忙或者老板发配的任务的时候,就能分分钟派上用场了. 1:安装运行photoshop,点击文件-打开,选择要ps的图片. 图片. ...

  6. Qt for Automation

    Automation, Automotive, and other industries In addition to improving the generic product offering a ...

  7. STL源代码学习--vector用法汇总

    一.容器vector 使用vector你必须包含头文件<vector>: #include<vector> 型别vector是一个定义于namespace std内的templ ...

  8. DEA上安装和使用checkstyle,findbugs,visualVM,PMD插件

    ##安装插件步骤: 1.打开settings 2.选择plugins 3.点击"Browse repositories" 4.搜索对应内插件,点击"install&quo ...

  9. vue-router设置页面标题

    通过vue-router设置页面标题 const router = new Router({ routes: [ { path: '/', name: 'EntryConfirmation', met ...

  10. 分布式缓存技术PK:选择Redis还是Memcached?

    作者:田京昆(腾讯后台研发工程师) 来源:腾云阁(https://www.qcloud.com/community/article/129) Memcached和Redis,作为近些年最常用的缓存服务 ...