ExcelTools使用

Posted daimaxuejia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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使用的主要内容,如果未能解决你的问题,请参考以下文章

webstorm代码片段的创建

VIM 代码片段插件 ultisnips 使用教程

Android课程---Android Studio使用小技巧:提取方法代码片段

使用 Git 来管理 Xcode 中的代码片段

如何在 Javadoc 中使用 @ 和 符号格式化代码片段?

使用 Pygments 检测代码片段的编程语言