//获取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. 【t049】&&【u001】足球

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 我们当中有很多热爱中国足球的同学,我们都知道中超(中国足球超级联赛)的规则: 一场比赛中,若获胜(即你 ...

  2. 使用Kotlin开发Android

    查看我的所有开源项目[开源实验室] 欢迎增加我的QQ群:[201055521],本博客client源代码下载[请点击] 摘要 我首先声明我并没有使用Kotlin非常长时间,我差点儿是在学习的同一时候写 ...

  3. android使用Gson来解析json

    Gson是一种对象的解析json,非常好用,介绍一个站点http://json.parser.online.fr/能够帮我们看一个字符串是不是Json 对于Json文件 { "id" ...

  4. 前端切图:调用百度地图API

    原型图 图片发自简书App <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  5. [Ramda] Change Object Properties with Ramda Lenses

    In this lesson we'll learn the basics of using lenses in Ramda and see how they enable you to focus ...

  6. Delphi2010,DelphiXE 安装控件找不到DesignIntf 解决办法

    今天安装了一个可以支持IP 地址输入的edit控件,安装后可以放到窗体上,但是编译提示找不到DesignIntf,DesignEditors 从Delphi6开始,就对DesignIntf,Desig ...

  7. JIL 编译与 AOT 编译

    JIT:Just-in-time compilation,即时编译:AOT:Ahead-of-time compilation,事前编译. JVM即时编译(JIT) 1. 动态编译与静态编译 动态编译 ...

  8. 设置npm淘宝镜像

    npm config set registry https://registry.npm.taobao.org

  9. WPF的逻辑树与视觉树(1)基本概念

    原文:WPF的逻辑树与视觉树(1)基本概念     一.摘要 逻辑树与视觉树属于WPF的基本概念,学过WPF或者Silverlight的朋友一定会对其有所耳闻,这篇文章将来探讨逻辑树与视觉树的特质以及 ...

  10. 理解c#

    首先在介绍c#的时候我们要先理解什么是.NET,.NET就是微软的用来实验XML,Web Services,SOA(面向服务的体系结构service-oriented architecture)和敏捷 ...