亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c# 根據(jù)NPOI 讀取一個(gè)excel 文件的多個(gè)Sheet

 更新時(shí)間:2020年12月11日 08:47:02   作者:風(fēng)格不同  
這篇文章主要介紹了c# 根據(jù)NPOI 讀取一個(gè)excel 文件的多個(gè)Sheet,幫助大家更好的利用c#處理excel表格,感興趣的朋友可以了解下

大家都知道NPOI組件可以在你本地沒(méi)有安裝office的情況下來(lái) 讀取,創(chuàng)建excel文件。但是大家一般都是只默認(rèn)讀取一個(gè)excel文件的第一個(gè)sheet。那么如果要讀取一個(gè)excel 的所有sheet 要怎么做呢?

下面就來(lái)告訴大家如何操作NPOI 讀取excel 的所有sheet。

首先我們先講解操作excel 單獨(dú)創(chuàng)建的一個(gè)類(lèi),我命名為 EXECLHELP

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
 
  public class ExcelHelper : IDisposable
        {
            private string fileName = null; //文件名
            private IWorkbook workbook = null;
            private FileStream fs = null;
            private bool disposed;
 
            public ExcelHelper(string fileName)
            {
                this.fileName = fileName;
                disposed = false;
            }
 
            /// <summary>
            /// 將DataTable數(shù)據(jù)導(dǎo)入到excel中
            /// </summary>
            /// <param name="data">要導(dǎo)入的數(shù)據(jù)</param>
            /// <param name="isColumnWritten">DataTable的列名是否要導(dǎo)入</param>
            /// <param name="sheetName">要導(dǎo)入的excel的sheet的名稱(chēng)</param>
            /// <returns>導(dǎo)入數(shù)據(jù)行數(shù)(包含列名那一行)</returns>
            public int DataTableToExcel(DataTable data, string sheetName, bool isColumnWritten)
            {
                int i = 0;
                int j = 0;
                int count = 0;
                ISheet sheet = null;
 
                fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                    workbook = new XSSFWorkbook();
                else if (fileName.IndexOf(".xls") > 0) // 2003版本
                    workbook = new HSSFWorkbook();
 
                try
                {
                    if (workbook != null)
                    {
                        sheet = workbook.CreateSheet(sheetName);
                    }
                    else
                    {
                        return -1;
                    }
 
                    if (isColumnWritten == true) //寫(xiě)入DataTable的列名
                    {
                        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); //寫(xiě)入到excel
                    return count;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);
                    return -1;
                }
            }
 
            /// <summary>
            /// 將excel中的數(shù)據(jù)導(dǎo)入到DataTable中
            /// </summary>
            /// <param name="sheetName">excel工作薄sheet的名稱(chēng)</param>
            /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>
            /// <returns>返回的DataTable</returns>
            ///
 
 
                public Dictionary<int,string> ReturnSheetList()
            {
                Dictionary<int, string> t = new Dictionary<int, string>();
                ISheet sheet = null;
                DataTable data = new DataTable();
                int startRow = 0;
                    try
                    {
                        fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                        if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                            workbook = new XSSFWorkbook(fs);
                        else if (fileName.IndexOf(".xls") > 0) // 2003版本
                            workbook = new HSSFWorkbook(fs);
                        int count = workbook.NumberOfSheets; //獲取所有SheetName
                        for(int i=0;i<count;i++)
                        {
                            sheet = workbook.GetSheetAt(i);
                            if (sheet.LastRowNum > 0)
                            {
                                t.Add(i, workbook.GetSheetAt(i).SheetName);
                            }
                        }
                        return t;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                  
 
            }<br>        ///index excel的第幾個(gè)sheet
            public DataTable ExcelToDataTable(int index)
            {
                ISheet sheet = null;
                DataTable data = new DataTable();
                int startRow = 0;
                try
                {
                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                        workbook = new XSSFWorkbook(fs);
                    else if (fileName.IndexOf(".xls") > 0) // 2003版本
                        workbook = new HSSFWorkbook(fs);
                    //int coutnts = workbook.NumberOfSheets;
 
                    sheet = workbook.GetSheetAt(index);
                    //string names= sheet.SheetName;
                    if (sheet != null)
                    {
                        IRow firstRow = sheet.GetRow(0);
                        int cellCount = firstRow.LastCellNum; //一行最后一個(gè)cell的編號(hào) 即總的列數(shù)
 
 
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                            ICell cell = firstRow.GetCell(i);
                            CellType c = cell.CellType;
                            if (cell != null)
                            {
                                string cellValue = cell.StringCellValue;
                                if (cellValue != null)
                                {
                                    DataColumn column = new DataColumn(cellValue);
                                    data.Columns.Add(column);
                                }
                            }
                        }
                        startRow = sheet.FirstRowNum + 1;
 
 
                        //最后一列的標(biāo)號(hào)
                        int rowCount = sheet.LastRowNum;
                        for (int i = startRow; i <= rowCount; ++i)
                        {
                            IRow row = sheet.GetRow(i);
                            if (row == null) continue; //沒(méi)有數(shù)據(jù)的行默認(rèn)是null       
 
                            DataRow dataRow = data.NewRow();
                            for (int j = row.FirstCellNum; j < cellCount; ++j)
                            {
                                if (row.GetCell(j) != null) //同理,沒(méi)有數(shù)據(jù)的單元格都默認(rèn)是null
                                    dataRow[j] = row.GetCell(j).ToString(); 
                            }
                            data.Rows.Add(dataRow);
                        }
                    }
 
                    return data;
                }
                catch (Exception ex)
                {
                    return null;
                    throw new Exception(ex.Message);
 
                }
            }
 
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
 
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        if (fs != null)
                            fs.Close();
                    }
 
                    fs = null;
                    disposed = true;
                }
            }
        }<br><br>

DataTableToExcel 這個(gè)方法是講數(shù)據(jù)導(dǎo)出為excel,參數(shù)在代碼里面都寫(xiě)了注釋?zhuān)梢灾苯犹子谩?ExcelToDataTable 這個(gè)方法主要是將excel數(shù)據(jù)導(dǎo)入到 databtable里面 同理參數(shù)也在注釋里面。 主要講一下 ReturnSheetList 方法 在讀取之前我們是需要判斷導(dǎo)入的excel版本是高版本還是低版本,這是因?yàn)閚poi 提供高低版本的操作類(lèi)是不一樣的 大于03小于07版本提供的是HSSFWorkbook,小于07版本提供的是 XSSFWorkbook。 然后 workbook.NumberOfSheets 這個(gè)主要是獲取一個(gè)excel文件中有多少個(gè)sheet,我們根據(jù)循環(huán)遍歷讀取sheet 然后將sheetname 的名字和對(duì)應(yīng)的index傳到一個(gè)數(shù)據(jù)字典中保存。 于是這個(gè)數(shù)據(jù)字典就存在了你導(dǎo)入的excel 文件所有的 有內(nèi)容的sheet和對(duì)應(yīng)的index。 搭配上ExcelToDataTable 使用 就可以達(dá)到切換讀取一個(gè)excel 的不同sheet 的目的。

以上就是c# 根據(jù)NPOI 讀取一個(gè)excel 文件的多個(gè)Sheet的詳細(xì)內(nèi)容,更多關(guān)于c# 讀取excel 的sheet的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#實(shí)現(xiàn)Markdown內(nèi)容轉(zhuǎn)為Word或PDF

    C#實(shí)現(xiàn)Markdown內(nèi)容轉(zhuǎn)為Word或PDF

    Markdown作為一種輕量級(jí)的標(biāo)記語(yǔ)言,因其簡(jiǎn)潔易讀、語(yǔ)法清晰的特點(diǎn)被廣大程序員和技術(shù)文檔編寫(xiě)者所青睞,本文主要介紹了如何使用C#實(shí)現(xiàn)Markdown內(nèi)容轉(zhuǎn)為Word或PDF,需要的可以參考下
    2024-03-03
  • C#使用is、as關(guān)鍵字以及顯式強(qiáng)轉(zhuǎn)實(shí)現(xiàn)引用類(lèi)型轉(zhuǎn)換

    C#使用is、as關(guān)鍵字以及顯式強(qiáng)轉(zhuǎn)實(shí)現(xiàn)引用類(lèi)型轉(zhuǎn)換

    這篇文章介紹了C#使用is、as關(guān)鍵字以及顯式強(qiáng)轉(zhuǎn)實(shí)現(xiàn)引用類(lèi)型轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • C#計(jì)算汽車(chē)行駛方向的方法分析

    C#計(jì)算汽車(chē)行駛方向的方法分析

    這篇文章主要介紹了C#計(jì)算汽車(chē)行駛方向的方法,結(jié)合實(shí)例形式分析了C#數(shù)值計(jì)算的原理與相關(guān)技巧,需要的朋友可以參考下
    2016-09-09
  • C#中Convert.ToDecimal()報(bào)錯(cuò)問(wèn)題的解決

    C#中Convert.ToDecimal()報(bào)錯(cuò)問(wèn)題的解決

    這篇文章主要給大家介紹了關(guān)于C#中Convert.ToDecimal()報(bào)錯(cuò)問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 用 C# Winform做出全透明的磨砂玻璃窗體效果代碼

    用 C# Winform做出全透明的磨砂玻璃窗體效果代碼

    就是一個(gè)簡(jiǎn)單的例子, 調(diào)用系統(tǒng)的 dwm 去重繪窗口. 只能在 Vista 和 7 之后才可以, 并且要確保已經(jīng)開(kāi)啟主題服務(wù)等等, 總之不是非常實(shí)用, 好玩而已
    2011-05-05
  • C#圖表算法之最短路徑

    C#圖表算法之最短路徑

    本文詳細(xì)講解了C#圖表算法之最短路徑,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • 深入理解C#序列化與反序列化的詳解

    深入理解C#序列化與反序列化的詳解

    本篇文章是對(duì)C#中序列化與反序列化進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 全面解讀C#編程中的析構(gòu)函數(shù)用法

    全面解讀C#編程中的析構(gòu)函數(shù)用法

    這篇文章主要介紹了C#編程中的析構(gòu)函數(shù)用法,文中最后還整理了析構(gòu)函數(shù)與Dispose()方法的區(qū)別,需要的朋友可以參考下
    2016-01-01
  • C#實(shí)現(xiàn)多文件壓縮與解壓功能

    C#實(shí)現(xiàn)多文件壓縮與解壓功能

    這篇文章主要為大家詳細(xì)介紹了如何利用C#語(yǔ)言實(shí)現(xiàn)多文件壓縮與解壓功能,即選擇多個(gè)文件壓縮成ZIP文件和解壓ZIP文件,需要的可以參考一下
    2022-08-08
  • Qt之調(diào)用C#的動(dòng)態(tài)庫(kù)的解決方法

    Qt之調(diào)用C#的動(dòng)態(tài)庫(kù)的解決方法

    這篇文章給大家介紹了Qt之調(diào)用C#的動(dòng)態(tài)庫(kù)的解決方法,環(huán)境使用的是VS2019+Qt5.12,感興趣的朋友一起看看吧
    2021-10-10

最新評(píng)論