//获取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");


 

最新文章

  1. java学习笔记之泛型
  2. 爱上MVC3系列~Html.BeginForm与Ajax.BeginForm
  3. Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)
  4. [置顶] 2014年八大最热门IT技能
  5. ubuntu firefox 选中变成了删除
  6. 自制单片机之十三……时钟IC_DS1302
  7. Encountered a section with no Package: header
  8. spring mvc ajax请求
  9. 4.JAVA-数组、String详解
  10. Java(15) 多态
  11. 编译树莓派2代B型OpenWrt固件实现无线路由器及nodogsplash认证功能
  12. Linux基础命令---free显示内存使用
  13. android发布新版忘记keystore(jks)密码终极解决方案
  14. Scala学习教程笔记二之函数式编程、Object对象、伴生对象、继承、Trait、
  15. LINUX PID 1 和 SYSTEMD
  16. JAVA NIO 中的 zerocopy 技术提高IO性能
  17. 荣耀9少 gms core服务
  18. (转)Linux top命令的用法详细详解
  19. Skype for Business Server-呼叫质量仪表板(一)安装与配置
  20. KMP + 求最小循环节 --- HUST 1010 - The Minimum Length

热门文章

  1. js进阶正则表达式8量词(+*?{}的意义用法)(量词的对象是前面一个字符)
  2. RabbitMQ 服务
  3. 【u003】计算概率
  4. 【9107】Hanoi双塔问题(NOIP2007)
  5. [SCSS] Organize Styles with SCSS Nesting and the Parent Selector
  6. Mysql用户本机登陆不成功的解决
  7. Web开发的编码解决中文乱码
  8. Python科学计算(一)
  9. boost1.59编译安装(可以完全安装,也可定制安装--buildtype=complete,link=static)
  10. NSNull 和 nil 的判断