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使用的更多相关文章

  1. java netty socket库和自定义C#socket库利用protobuf进行通信完整实例

    之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...

  2. .NET基础架构方法—DataTableToExcel通用方法

    p { display: block; margin: 3px 0 0 0; } --> .NET架构基础方法—DataTableToExcel通用方法(NPOI) 今天封装DataTaleTo ...

  3. good excel website

    http://www.codematic.net/excel-tools/free-excel-tools.htm

  4. Java操作Excel和Word

    这是一个URL它提供了Java项目所推荐的处理此项目所用的类库 http://www.oschina.net/project/tag/258/excel-tools?company=0&sor ...

  5. 指定路径批量将xls转换成csv

    PS : 用到spire库,.net控制台应用程序 其实本来没打算写这个工具的,只是最近需要用到,手头上正好没有这样的工具,那么怎么办,写呗! 其实说白了就是省事,策划想怎么玩,把表把工具丢给他,省得 ...

  6. 六、ibatis1.2.8查询性能优化,实现百万数据zip导出

    经测试发现将查询的结果100万数据(池子中共有大概14亿的数据)写入Excle文件并进行压缩导出zip文件最耗时的地方竟然在查询,因此本篇文章主要是针对如何在spring+ibatis1.2.8中优化 ...

随机推荐

  1. CentOS下RabbitMq高可用集群环境搭建

    准备工作 1,准备两台或多台安装有rabbitmq-server服务的服务器 我这里准备了两台,分别如下: 192.168.40.130 rabbitmq01192.168.40.131 rabbit ...

  2. Dynamics 365 Online-Microsoft Flow

    自December 2016 update for Dynamics 365 (online)之后的Online版本,Dynamics 365有了个新Feature:Microsoft Flow Co ...

  3. Android为TV端助力 进制互相转换

    byte转换为16进制 public static String GetByte2Str(byte b) { byte[] buff = new byte[2]; buff[0] = mHex[(b ...

  4. windows键盘按键输入错乱;

    问题:打字异常,打字乱码: 最佳解决方案:下载工具快速修复: 链接: 链接:https://pan.baidu.com/s/1GpWT-MljgQHorLNMhQ9eOg提取码:anh0 官网文档原因 ...

  5. 通过Erlang构建TCP服务应用案例(最原始方式)

    文章来源:公众号-智能化IT系统. 案例介绍 本文介绍的案例是TCP网络服务器的构建,用最原始的方式(非OTP).其功能很简单,通过网络TCP接口接收数据,按照指定的格式解析,并把数据存储至Mongo ...

  6. 从 RAID 到 Hadoop Hdfs 『大数据存储的进化史』

    我们都知道现在大数据存储用的基本都是 Hadoop Hdfs ,但在 Hadoop 诞生之前,我们都是如何存储大量数据的呢?这次我们不聊技术架构什么的,而是从技术演化的角度来看看 Hadoop Hdf ...

  7. jsp 简单下载

    <%@ page language="java" import="java.util.*" contentType="text/html;cha ...

  8. 2星|《IT真相》:日本咨询师面对美国云服务的发展,对日本IT业哀其不争

    IT真相-打通IT与商务的通路 I 作者是日本管理咨询师,对日本的IT和金融业了解比较多.书的内容是:作者看到美国的云服务发展壮大,日本IT业没能抓住机会,对日本IT业做了一些批评,比如不思进取,不了 ...

  9. iowait 过高问题的查找及解决linux

    Linux 有许多可用来查找问题的简单工具,也有许多是更高级的 I/O Wait 就是一个需要使用高级的工具来debug的问题,当然也有许多基本工具的高级用法.I/O wait的问题难以定位的原因是因 ...

  10. pdf 下载demo

    最近写了个pdf下载的demo,在这里记录一下.. 1  要下载pdf首先要有pdf 模板 ,制作pdf 模板就是 word 另存为 pdf . 2 用 Adobe Acrobat X Pro 这个软 ...