//获取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. http500:服务器内部错误案例详解(服务器代码语法错误或者逻辑错误)

    http500:服务器内部错误案例详解(服务器代码语法错误或者逻辑错误) 一.总结 服务器内部错误可能是服务器中代码运行的时候的语法错误或者逻辑错误 二.http500:服务器内部错误案例详解 只是一 ...

  2. VMWare提供三种工作模式桥接(bridge)、NAT(网络地址转换)和host-only(主机模式)

    1.桥接模式 在桥接模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机(主机和虚拟机处于对等地位),它可以访问网内任何一台机器.在桥接模式下,我们往往需要为虚拟主机配置IP地址.子网 ...

  3. Bootstrap手机网站开发案例

    Bootstrap手机网站开发案例 一.总结 一句话总结:Bootstrap手机网站开发注意事项(3点):a.引入viewpoint声明,b.通过屏幕宽动态控制元素显隐 c.图片添加自适应 1.Boo ...

  4. 【hdu 3863】No Gambling

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65568/32768 K (Java/Others) Total Submission(s) ...

  5. 学习JS的这些日子——十二月总结

    事实上非常想早就发表这篇十二月份的总结了,可是一直拖拖拉拉没有完毕.一直在想2015年都过去了,该不该再去 写这一篇2015年最后一个月的总结.还有就是2015年的年终总结能否够取代十二月的总结,后来 ...

  6. Swagger 专题

    随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.前后端分离的形态,而且前端和后端在各自的技术道路上越走越远. 前端和后端的唯一联系,变成了API接口:API文档成了前后端 ...

  7. 【NOIP2012提高组】国王游戏 贪心 + 高精度

    题目分析 题目答案不具有单调性,所以不可以二分,转而思考贪心.因为无法确定位置,所以考虑如何才能让对于每一个$1 ~ i$使得$i$的答案最大,即$1 ~ i$最后一个最优.若设对于位置$i$,$a[ ...

  8. 【烽火传递】dp + 单调队列优化

    题目描述 烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有 n 个烽火台,每个烽火台发出信 ...

  9. 英文构词法 —— circum- 前缀

    1. - circum-:表示环绕,周围,圆周: circle:圆:循环: circumference:圆周,周长,胸围: circumstance:环境: circumnavigation:环球航行 ...

  10. FPGA综合优化

    1 速度和面积 在全面优化水平将达到速度和面积RTL要利用逻辑拓扑的优势. 供FPGA由于在后端而言缺乏知识,门级优化.普通情况下更高的速度要求更高的并行性以及更大的面积,可是在某些特殊情况下并非这样 ...