excel文本写入 npoi_C#中NPOI操作excel之读取和写入excel数据

usingSystem;usingSystem.Data;usingSystem.IO;usingNPOI.SS.UserModel;usingNPOI.HSSF.UserModel;usingNPOI.XSSF.UserModel;usingSystem.Collections.Generic;usingSystem.Reflection;namespaceIETMAuthor.Toolbox

{public classExcelUtility

{///

///将excel导入到datatable///

/// excel路径

/// 第一行是否是列名

/// 返回datatable

public static DataTable ExcelToDataTable(string filePath, boolisColumnName)

{

DataTable dataTable= null;

FileStream fs= null;

DataColumn column= null;

DataRow dataRow= null;

IWorkbook workbook= null;

ISheet sheet= null;

IRow row= null;

ICell cell= null;int startRow = 0;try{using (fs =File.OpenRead(filePath))

{//2003版本

if (filePath.IndexOf(".xls") > 0)

workbook= newHSSFWorkbook(fs);//2007版本

else if (filePath.IndexOf(".xlsx") > 0)

workbook= newXSSFWorkbook(fs);if (workbook != null)

{

sheet= workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet

dataTable = newDataTable();if (sheet != null)

{int rowCount = sheet.LastRowNum;//总行数

if (rowCount > 0)

{

IRow firstRow= sheet.GetRow(0);//第一行

int cellCount = firstRow.LastCellNum;//列数//构建datatable的列

if(isColumnName)

{

startRow= 1;//如果第一行是列名,则从第二行开始读取

for (int i = firstRow.FirstCellNum; i < cellCount; i++)

{

cell=firstRow.GetCell(i);if (cell != null)

{if (cell.StringCellValue != null)

{

column= newDataColumn(cell.StringCellValue);

dataTable.Columns.Add(column);

}

}

}

}else{for (int i = firstRow.FirstCellNum; i < cellCount; i++)

{

column= new DataColumn("column" + (i + 1));

dataTable.Columns.Add(column);

}

}//填充行

for (int i = startRow; i <= rowCount; i++)

{

row=sheet.GetRow(i);if (row == null) continue;

dataRow=dataTable.NewRow();for (int j = row.FirstCellNum; j < cellCount; j++)

{

cell=row.GetCell(j);if (cell == null)

{

dataRow[j]= "";

}else{switch(cell.CellType)

{case(NPOI.SS.UserModel.CellType)CellType.Blank:

dataRow[j]= "";break;case(NPOI.SS.UserModel.CellType)CellType.Numeric:short format =cell.CellStyle.DataFormat;//对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理

if (format == 14 || format == 31 || format == 57 || format == 58)

dataRow[j]=cell.DateCellValue;elsedataRow[j]=cell.NumericCellValue;break;case(NPOI.SS.UserModel.CellType)CellType.String:

dataRow[j]=cell.StringCellValue;break;

}

}

}

dataTable.Rows.Add(dataRow);

}

}

}

}

}returndataTable;

}catch(Exception)

{if (fs != null)

{

fs.Close();

}return null;

}

}///

///DataTable转List

///

/// 数据项类型

/// DataTable

/// List数据集

public static List DataTableToList(DataTable dt) where T : new()

{

List list = new List();if (dt != null && dt.Rows.Count > 0)

{foreach (DataRow dr indt.Rows)

{

T t= DataRowToModel(dr);

list.Add(t);

}

}returnlist;

}///

///DataRow转实体///

/// 数据型类

/// DataRow

/// 模式

public static T DataRowToModel(DataRow dr) where T : new()

{//T t = (T)Activator.CreateInstance(typeof(T));

T t = newT();if (dr == null) return default(T);//获得此模型的公共属性

PropertyInfo[] propertys =t.GetType().GetProperties();

DataColumnCollection Columns=dr.Table.Columns;foreach (PropertyInfo p inpropertys)

{string columnName =p.Name;if(Columns.Contains(columnName))

{object value =dr[columnName];if (value is DBNull || value ==DBNull.Value)continue;try{switch(p.PropertyType.ToString())

{case "System.String":

p.SetValue(t, Convert.ToString(value),null);break;case "System.Int32":

p.SetValue(t, Convert.ToInt32(value),null);break;case "System.Int64":

p.SetValue(t, Convert.ToInt64(value),null);break;case "System.DateTime":

p.SetValue(t, Convert.ToDateTime(value),null);break;case "System.Boolean":

p.SetValue(t, Convert.ToBoolean(value),null);break;case "System.Double":

p.SetValue(t, Convert.ToDouble(value),null);break;case "System.Decimal":

p.SetValue(t, Convert.ToDecimal(value),null);break;default:

p.SetValue(t, value,null);break;

}

}catch(Exception ex)

{continue;/*使用 default 关键字,此关键字对于引用类型会返回空,对于数值类型会返回零。对于结构,

* 此关键字将返回初始化为零或空的每个结构成员,具体取决于这些结构是值类型还是引用类型*/}

}

}returnt;

}

}enumCellType

{

Unknown= -1, Numeric = 0, String = 1, Formula = 2, Blank = 3, Boolean = 4, Error = 5}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值