• NPOI 导入 03版本和 07版本


            #region 导入03Excel
    
            /// <summary>  
            /// 读取Excel文件到DataSet中  
            /// </summary>  
            /// <param name="filePath">文件路径</param>  
            /// <returns></returns>  
            public static DataTable ExcelToDataTable03(string filePath)
            {
                DataTable dt = new DataTable();
    
                HSSFWorkbook hssfworkbook;
                using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    hssfworkbook = new HSSFWorkbook(file);
                }
                ISheet sheet = hssfworkbook.GetSheetAt(0);
                System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
    
                IRow headerRow = sheet.GetRow(0);
                int cellCount = headerRow.LastCellNum;
    
                for (int j = 0; j < cellCount; j++)
                {
                    ICell cell = headerRow.GetCell(j);
                    dt.Columns.Add(cell.ToString());
                }
    
                for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    DataRow dataRow = dt.NewRow();
    
                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                            dataRow[j] = row.GetCell(j).ToString();
                    }
    
                    dt.Rows.Add(dataRow);
                }
                return dt;
            }
    
    
            /// <summary>
            /// 读取Excel文件到DataSet中  
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <param name="sheetName">Excel中Sheet名</param>
            /// <returns></returns>
            public static DataTable ExcelToDataTable03(string filePath, string sheetName)
            {
                DataTable dt = new DataTable();
    
                HSSFWorkbook hssfworkbook;
                using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    hssfworkbook = new HSSFWorkbook(file);
                }
                ISheet sheet = hssfworkbook.GetSheet(sheetName);
                System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
    
                IRow headerRow = sheet.GetRow(0);
                int cellCount = headerRow.LastCellNum;
    
                for (int j = 0; j < cellCount; j++)
                {
                    ICell cell = headerRow.GetCell(j);
                    dt.Columns.Add(cell.ToString());
                }
    
                for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    DataRow dataRow = dt.NewRow();
    
                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                            dataRow[j] = row.GetCell(j).ToString();
                    }
    
                    dt.Rows.Add(dataRow);
                }
                return dt;
            }
    
    
    
            /// <summary>
            /// 读取Excel文件到DataSet中  
            /// </summary>
            /// <param name="stream">文件流</param>
            /// <returns></returns>
            public static DataTable ExcelToDataTable03(Stream stream)
            {
                DataTable dt = new DataTable();
    
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(stream);
                ISheet sheet = hssfworkbook.GetSheetAt(0);
                System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
    
                IRow headerRow = sheet.GetRow(0);
                int cellCount = headerRow.LastCellNum;
    
                for (int j = 0; j < cellCount; j++)
                {
                    ICell cell = headerRow.GetCell(j);
                    dt.Columns.Add(cell.ToString());
                }
    
                for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    DataRow dataRow = dt.NewRow();
    
                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                            dataRow[j] = row.GetCell(j).ToString();
                    }
    
                    dt.Rows.Add(dataRow);
                }
                return dt;
            }
    
    
            /// <summary>
            /// 读取Excel文件到DataSet中
            /// </summary>
            /// <param name="stream">文件流</param>
            /// <param name="sheetName">Excel中Sheet名称</param>
            /// <returns></returns>
            public static DataTable ExcelToDataTable03(Stream stream, string sheetName)
            {
                DataTable dt = new DataTable();
                try
                {
                    HSSFWorkbook hssfworkbook = new HSSFWorkbook(stream);
                    ISheet sheet = hssfworkbook.GetSheet(sheetName);
                    System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
    
                    IRow headerRow = sheet.GetRow(0);
                    int cellCount = headerRow.LastCellNum;
    
                    for (int j = 0; j < cellCount; j++)
                    {
                        ICell cell = headerRow.GetCell(j);
                        dt.Columns.Add(cell.ToString());
                    }
    
                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        DataRow dataRow = dt.NewRow();
    
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            if (row.GetCell(j) != null)
                                dataRow[j] = row.GetCell(j).ToString();
                        }
    
                        dt.Rows.Add(dataRow);
                    }
                    return dt;
                }
                catch
                {
                    return dt;
                }
            }
    
            #endregion
    
    
            #region 导入Excel o7版
    
            /// <summary>
            /// 读取Excel文件到DataSet中  
            /// </summary>
            /// <param name="stream">文件流</param>
            /// <returns></returns>
            public static DataTable ExcelToDataTable07(Stream stream)
            {
                DataTable dt = new DataTable();
                try
                {
                    IWorkbook workbook = new XSSFWorkbook(stream);
                    ISheet sheet = workbook.GetSheetAt(0);
                    System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
    
                    IRow headerRow = sheet.GetRow(0);
                    int cellCount = headerRow.LastCellNum;
    
                    for (int j = 0; j < cellCount; j++)
                    {
                        ICell cell = headerRow.GetCell(j);
                        dt.Columns.Add(cell.ToString());
                    }
    
                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        DataRow dataRow = dt.NewRow();
    
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            if (row.GetCell(j) != null)
                                dataRow[j] = row.GetCell(j).ToString();
                        }
    
                        dt.Rows.Add(dataRow);
                    }
                    return dt;
                }
                catch
                {
                    return dt;
                }
            }
    
    
            /// <summary>
            /// 根据文件流导入Excel到DataTable
            /// </summary>
            /// <param name="stream"></param>
            /// <param name="sheetName"></param>
            /// <returns></returns>
            public static DataTable ExcelToDataTable07(Stream stream, string sheetName)
            {
                DataTable dt = new DataTable();
                try
                {
                    IWorkbook workbook = new XSSFWorkbook(stream);
                    ISheet sheet = workbook.GetSheet(sheetName);
                    System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
    
                    IRow headerRow = sheet.GetRow(0);
                    int cellCount = headerRow.LastCellNum;
    
                    for (int j = 0; j < cellCount; j++)
                    {
                        ICell cell = headerRow.GetCell(j);
                        dt.Columns.Add(cell.ToString());
                    }
    
                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        DataRow dataRow = dt.NewRow();
    
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            if (row.GetCell(j) != null)
                                dataRow[j] = row.GetCell(j).ToString();
                        }
    
                        dt.Rows.Add(dataRow);
                    }
                    return dt;
                }
                catch
                {
                    return dt;
                }
            }
    
            #endregion

  • 相关阅读:
    mysql安装脚本
    vim常用命令
    CentOS 6.5使用国内网易163的源
    053(七十五)
    053(七十四)
    053(七十三)
    053(七十二)
    053(七十一)
    053(七十)
    053(六十九)
  • 原文地址:https://www.cnblogs.com/acgk/p/4378495.html
Copyright © 2020-2023  润新知