zoukankan      html  css  js  c++  java
  • 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 ImportClient
    {
        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:
                            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)
                {
                    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:
                            if (DateUtil.IsValidExcelDate(cell.NumericCellValue) && DateUtil.IsCellDateFormatted(cell))
                            {
                                DateTime D = cell.DateCellValue;
                                return (D.ToString().Length == 0 || D.ToString().Contains("#")) ? " " : D.ToString();
                            }
                            else
                            {
                                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)
                {
                    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;
                }
    
            }
    
        }
    }
    

      

    萌橙 你瞅啥?
  • 相关阅读:
    implement the bucket sort and some analysis
    冪運匴
    polynomial multiplication
    WinRAR(WinZip)压缩与解压实现(C#版Window平台)
    Windows 窗体计时器事件,则不引发在 Windows 服务
    VS 2008智能提示是英文的怎么改成中文的?
    C#_SQL数据访问帮助类
    C#_DllImport用法和路径问题
    Office2003/2007/2010强力卸载工具下载
    Windows_装的是2008R2系统,在电脑属性里显示内存12G(可用4G)
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/10535359.html
Copyright © 2011-2022 走看看