예제 #1
0
 /// <summary>
 /// 将类型转换为Boolean
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static Boolean ToBoolean(object obj)
 {
     if (ConvertObject.ToString(obj) == "1")
     {
         return(true);
     }
     if (ConvertObject.ToString(obj) == "0")
     {
         return(false);
     }
     return(ToBoolean(obj, false));
 }
예제 #2
0
        /// <summary>
        /// 通过Stream获取Datatable
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileType">Excel文件类型(xls或xlsx)</param>
        /// <param name="sheetName">excel工作薄sheet的名称</param>
        /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>
        /// <returns></returns>
        public DataTable ConvertStreamToDataTable(Stream stream, string fileType, string sheetName, bool isFirstRowColumn)
        {
            IWorkbook workbook = null;
            ISheet    sheet    = null;
            DataTable data     = new DataTable();
            int       startRow = 0;

            if (fileType == "xlsx")
            {
                workbook = new XSSFWorkbook(stream);//2007版本
            }
            else if (fileType == "xls")
            {
                try
                {
                    workbook = new HSSFWorkbook(stream);//2003版本
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("Office 2007"))
                    {
                        throw new Exception("xlsx");
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }
            else
            {
                throw new Exception("Excel文件类型不正确");
            }

            if (sheetName != "")
            {
                sheet = workbook.GetSheet(sheetName);
                if (sheet == null)
                {
                    //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                    sheet = workbook.GetSheetAt(0);
                }
            }
            else
            {
                sheet = workbook.GetSheetAt(0);
            }

            if (sheet != null)
            {
                IRow firstRow  = sheet.GetRow(0);
                int  cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数

                if (isFirstRowColumn)
                {
                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                    {
                        ICell cell = firstRow.GetCell(i);
                        if (cell != null)
                        {
                            string cellValue = ConvertObject.ToString(cell.StringCellValue);
                            if (cellValue != null)
                            {
                                DataColumn column = new DataColumn(cellValue);
                                data.Columns.Add(column);
                            }
                        }
                    }
                    startRow = sheet.FirstRowNum + 1;
                }
                else
                {
                    startRow = sheet.FirstRowNum;
                }

                //最后一列的标号
                int rowCount = sheet.LastRowNum;
                for (int i = startRow; i <= rowCount; ++i)
                {
                    IRow row = sheet.GetRow(i);
                    if (row == null)
                    {
                        continue;              //没有数据的行默认是null       
                    }
                    DataRow dataRow = data.NewRow();
                    for (int j = row.FirstCellNum; j < cellCount; ++j)
                    {
                        if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
                        {
                            dataRow[j] = row.GetCell(j).ToString();
                            if (row.GetCell(j).CellType == CellType.Numeric && DateUtil.IsCellDateFormatted(row.GetCell(j)))//如果类型是日期形式的,转成合理的日期字符串
                            {
                                dataRow[j] = row.GetCell(j).DateCellValue.ToString();
                            }
                        }
                    }
                    data.Rows.Add(dataRow);
                }
            }

            return(data);
        }