zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Java第9次作业--接口及接口回调
    Java第8次作业--继承
    软件工程第三次作业——关于软件质量保障初探
    Java第7次作业--访问权限、对象使用
    Java第6次作业--static关键字、对象
    Java第5次作业--对象的创建与使用
    20194629 自动生成四则运算题第一版报告
    软件工程第一次作业
    今天开通博客啦!
    1170. Compare Strings by Frequency of the Smallest Character
  • 原文地址:https://www.cnblogs.com/acgk/p/4378495.html
Copyright © 2011-2022 走看看