ExcelTools使用
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DCZY.BISC
{
public class ExcelTool
{ public static Dictionary<string, int> SetColumncaption(IRow header)
{
Dictionary<string, int> columns = new Dictionary<string, int>();
for (int i = 0; i < header.LastCellNum; i++)
{
string strcaption = header.GetCell(i).ToString();
if (strcaption != null && strcaption.Length > 0)
{
columns.Add(strcaption, i);
}
}
return columns;
} /// <summary>
/// 获取单元格类型(xls)
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
public static string GetValueTypeForXLS(ICell cell)
{
try
{
if (cell == null)
return string.Empty;
switch (cell.CellType)
{
case CellType.BLANK: //BLANK:
return string.Empty;
case CellType.BOOLEAN: //BOOLEAN:
return cell.BooleanCellValue.ToString();
case CellType.NUMERIC: //NUMERIC:
try
{
if (cell.ColumnIndex == 3 || cell.ColumnIndex == 4)
{
return cell.DateCellValue.ToString("HH:mm");
}
}
catch
{
}
return cell.NumericCellValue.ToString();
case CellType.STRING: //STRING:
return cell.StringCellValue == null ? string.Empty : cell.StringCellValue;
case CellType.ERROR: //ERROR:
return cell.ErrorCellValue.ToString();
case CellType.FORMULA: //FORMULA:
default:
return "=" + cell.CellFormula;
}
}
catch (Exception e)
{
PLog.Log.WriteError(e.Message);
return string.Empty;
} } /// <summary>
/// 获取单元格类型(xls)
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
public static string OtherGetValueTypeForXLS(ICell cell)
{
try
{
if (cell == null)
return string.Empty;
switch (cell.CellType)
{
case CellType.BLANK: //BLANK:
return string.Empty;
case CellType.BOOLEAN: //BOOLEAN:
return cell.BooleanCellValue.ToString();
case CellType.NUMERIC: //NUMERIC:
return cell.NumericCellValue.ToString();
case CellType.STRING: //STRING:
return cell.StringCellValue == null ? string.Empty : cell.StringCellValue;
case CellType.ERROR: //ERROR:
return cell.ErrorCellValue.ToString();
case CellType.FORMULA: //FORMULA:
default:
return "=" + cell.CellFormula;
}
}
catch (Exception e)
{
PLog.Log.WriteError(e.Message);
return string.Empty;
} } }
}
调用:
using DCZY.Base.Param;
using DCZY.Bean;
using DCZY.Bean.LocationDevice;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using PLog;
using PTool;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text; namespace DCZY.BISC.OperationPost
{
public class OperationPostFromExcel
{
private string _strfilename = string.Empty;
public OperationPostFromExcel(string strfilename)
{
_strfilename = strfilename;
} public List<OperationVariablePostInfo> GetInfo()
{
StringBuilder sb = new StringBuilder();
try
{
FileStream fs = new FileStream(_strfilename, FileMode.Open, FileAccess.Read);
IWorkbook hssfworkbook = new XSSFWorkbook(fs);
ISheet sheet = hssfworkbook.GetSheet("岗位配置表 (2)");
List<OperationVariablePostInfo> devicecollection = new List<OperationVariablePostInfo>();
IRow header = sheet.GetRow(sheet.FirstRowNum);
Dictionary<string, int> colcollection = ExcelTool.SetColumncaption(header);
//数据
for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
{
OperationVariablePostInfo info = new OperationVariablePostInfo();
info.Name = ExcelTool.OtherGetValueTypeForXLS(sheet.GetRow(i).GetCell(colcollection["岗位名称"]));
if (info.Name == null)
{
info.Name = string.Empty;
}
info.Firstgwname = ExcelTool.OtherGetValueTypeForXLS(sheet.GetRow(i).GetCell(colcollection["一级岗位"]));
info.Gwname = ExcelTool.OtherGetValueTypeForXLS(sheet.GetRow(i).GetCell(colcollection["二级岗位"]));
string lookwith= ExcelTool.OtherGetValueTypeForXLS(sheet.GetRow(i).GetCell(colcollection["是否盯控"]));
if (lookwith.Length == 0)
{
info.Islookwith = false;
}
else
{
info.Islookwith = true;
}
//info.Islookwith=
info.Posttype = new ParamInfo();
info.Posttype.Name = ExcelTool.OtherGetValueTypeForXLS(sheet.GetRow(i).GetCell(colcollection["岗位类型"]));
info.Organization = new OrganizationInfo();
info.Organization.Name = ExcelTool.OtherGetValueTypeForXLS(sheet.GetRow(i).GetCell(colcollection["车站"]));
devicecollection.Add(info);
}
return devicecollection;
}
catch (Exception e)
{
Log.WriteError(e.Message);
return null;
} } }
}
ExcelTools使用的更多相关文章
- java netty socket库和自定义C#socket库利用protobuf进行通信完整实例
之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...
- .NET基础架构方法—DataTableToExcel通用方法
p { display: block; margin: 3px 0 0 0; } --> .NET架构基础方法—DataTableToExcel通用方法(NPOI) 今天封装DataTaleTo ...
- good excel website
http://www.codematic.net/excel-tools/free-excel-tools.htm
- Java操作Excel和Word
这是一个URL它提供了Java项目所推荐的处理此项目所用的类库 http://www.oschina.net/project/tag/258/excel-tools?company=0&sor ...
- 指定路径批量将xls转换成csv
PS : 用到spire库,.net控制台应用程序 其实本来没打算写这个工具的,只是最近需要用到,手头上正好没有这样的工具,那么怎么办,写呗! 其实说白了就是省事,策划想怎么玩,把表把工具丢给他,省得 ...
- 六、ibatis1.2.8查询性能优化,实现百万数据zip导出
经测试发现将查询的结果100万数据(池子中共有大概14亿的数据)写入Excle文件并进行压缩导出zip文件最耗时的地方竟然在查询,因此本篇文章主要是针对如何在spring+ibatis1.2.8中优化 ...
随机推荐
- JavaScript 为什么要有 Symbol 类型?
Symbols 是 ES6 引入了一个新的数据类型 ,它为 JS 带来了一些好处,尤其是对象属性时. 但是,它们能为我们做些字符串不能做的事情呢? 在深入探讨 Symbol 之前,让我们先看看一些 J ...
- vue点击按钮给商品按照价格进行倒叙
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 把vux中的@font-face为base64格式的字体信息解码成可用的字体文件
在最近移动端项目中用到了vux,感觉用着还习惯,当把vux使用到PC端的时候出现了IE浏览器出现,这样的错误信息: CSS3114: @font-face 未能完成 OpenType 嵌入权限检查.权 ...
- 卷烟厂生产管理系统基于ASP.NET
VS.Net 2003开发的一个非常老的生产信息管理系统运行10多年依旧稳定,这几天来南昌做维保工作,看到了曾经开发的信息系统.该系统在我眼里老到几点,可却依然在厂里大面积使用,抛开技术上的落后,从新 ...
- virtual table for class
虚函数表 说起虚函数,相信你我都可以自然而然的想到“多态”,因为多态的实现就依赖于虚函数的继承和重写(覆盖).那么,class又或者是object是如何来管理虚函数的呢?你我又会想到虚函数表. 虚函数 ...
- Idea软件中隐藏忽略.idea,.iml等文件
参考链接,https://blog.csdn.net/fanrenxiang/article/details/80533950 ignore files and folders;增加隐藏的文件类型:
- ASP.NET没有魔法——ASP.NET MVC IoC代码篇
上一篇文章主要以文字的形式介绍了IoC及其在ASP.NET MVC中的使用,本章将从以下几点介绍如何使用代码在ASP.NET MVC中实现依赖注入: ● AutoFac及安装 ● 容器的创建 ● 创建 ...
- lnmp环境里安装mssql及mssql的php扩展
小活中用到mssql,于是在自己lnmp环境中安装各mssql数据库 步骤如下: 源码编译安装 (1)下载freetds-stable-0.91源码:http://download.csdn.net/ ...
- access denied for user 'root'@'localhost'(using password:YES) FOR WINDOWS
以windows为例: mysql5.5 1. 关闭正在运行的MySQL服务. 2. 打开DOS窗口,转到mysql\bin目录. 3. 输入mysqld --skip-grant-tables 回车 ...
- python3打印当前时间和获取程序运行时间
学习使用time模块和datetime模块. 通常我们想让程序等待几秒钟,再继续向下运行,time模块的sleep()方法是一个很好的选择.但是想通过time模块打印系统的当前时间,则比较麻烦.如下: ...