zoukankan      html  css  js  c++  java
  • NPOI 表格处理

     /// <summary>
            /// 获取单元格类型
            /// </summary>
            /// <param name="cell">目标单元格</param>
            /// <returns></returns>
            private static object GetValueType(ICell cell)
            {
                if (cell == null)
                    return null;
                switch (cell.CellType)
                {
                    case CellType.Blank:
                        return null;
                    case CellType.Boolean:
                        return cell.BooleanCellValue;
                    case CellType.Numeric:
                        return cell.NumericCellValue;
                    case CellType.String:
                        return cell.StringCellValue;
                    case CellType.Error:
                        return cell.ErrorCellValue;
                    case CellType.Formula:
                    default:
                        return "=" + cell.CellFormula;
                }
            }
            /// <summary>
            /// Excel导入成DataTble
            /// </summary>
            /// <param name="file">导入路径(包含文件名与扩展名)</param>
            /// <returns></returns>
            public static DataTable ExcelToTable(string file ,int pistion=0)
            {
                DataTable dt = new DataTable();
                IWorkbook workbook;
                string fileExt = Path.GetExtension(file).ToLower();
                using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
                {
                    if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(fs); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(fs); } else { workbook = null; }
                    if (workbook == null) { return null; }
                    ISheet sheet = workbook.GetSheetAt(pistion);
    
                    //表头  
                    IRow header = sheet.GetRow(sheet.FirstRowNum);
                    List<int> columns = new List<int>();
                    for (int i = 0; i < header.LastCellNum; i++)
                    {
                        object obj = GetValueType(header.GetCell(i));
                        if (obj == null || obj.ToString() == string.Empty)
                        {
                            dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
                        }
                        else
                            dt.Columns.Add(new DataColumn(obj.ToString()));
                        columns.Add(i);
                    }
                    //数据  
                    for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                    {
                        DataRow dr = dt.NewRow();
                        bool hasValue = false;
                        foreach (int j in columns)
                        {
                            dr[j] = GetValueType(sheet.GetRow(i).GetCell(j));
                            if (dr[j] != null && dr[j].ToString() != string.Empty)
                            {
                                hasValue = true;
                            }
                        }
                        if (hasValue)
                        {
                            dt.Rows.Add(dr);
                        }
                    }
                }
                return dt;
            }
     private static IWorkbook workbook;
            public static int DataTableToExcel(DataTable data, string sheetName, bool isColumnWritten, string fileName)
            {
                int i = 0;
                int j = 0;
                int count = 0;
                ISheet sheet = null;
                if (File.Exists(fileName))
                    File.Delete(fileName);
                var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                if (fileName.IndexOf(".xlsx") > 0)
                    workbook = new XSSFWorkbook();
                else if (fileName.IndexOf(".xls") > 0)
                    workbook = new HSSFWorkbook();
    
                try
                {
                    if (workbook != null)
                    {
                        sheet = workbook.CreateSheet(sheetName);
                    }
                    else
                    {
                        return -1;
                    }
    
                    if (isColumnWritten == true)
                    {
                        IRow row = sheet.CreateRow(0);
                        for (j = 0; j < data.Columns.Count; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
                        }
                        count = 1;
                    }
                    else
                    {
                        count = 0;
                    }
    
                    for (i = 0; i < data.Rows.Count; ++i)
                    {
                        IRow row = sheet.CreateRow(count);
                        for (j = 0; j < data.Columns.Count; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
                        }
                        ++count;
                    }
                    workbook.Write(fs);
                    return count;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);
                    return -1;
                }
            }
                 string worksheetname = string.Empty;
                    HSSFWorkbook workbook1 = null;
                    using (FileStream stream = File.OpenRead(fazhuSheetFilePath))
                    {
    
    
                        workbook1 = new HSSFWorkbook(stream);
                    }
    
                    int position = 0;
                    for (int i = 0; i < workbook1.NumberOfSheets; i++)
                    {
                        if (workbook1.GetSheetName(i).Contains("仕様条件編"))//判断Sheet名是否为所需要的
                        {
                            worksheetname = workbook1.GetSheetName(i);//获取worksheet Name  
                            position = i;
                            break;
                        }
    
                    }
    
                   var TaggingTable = ExcelToTable(fazhuSheetFilePath, position);
  • 相关阅读:
    “图”以致用组
    水体频率小组
    2021年云开发组三等奖作品展示
    毫秒级百万数据分页存储过程[欢迎转载]
    SQL Server 数据备份存储过程[原创]
    博客园居然被中国电信提醒有病毒,有图为证
    网络文件夹例子
    小技巧:在DropDownList数据绑定前插入固定文字
    ASP.NET整合Discuz!NT3.5实例说明(含用户登录、评论等)
    Visual Studio 2008的性能改进以及十大新功能(转)
  • 原文地址:https://www.cnblogs.com/wangcl-8645/p/11961321.html
Copyright © 2011-2022 走看看