日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C#實(shí)現(xiàn)EXCEL表格轉(zhuǎn)DataTable

 小仙女本仙人 2021-04-17

    C#代碼實(shí)現(xiàn)把Excel文件轉(zhuǎn)化為DataTable,根據(jù)Excel的文件后綴名不同,用不同的方法來進(jìn)行實(shí)現(xiàn),下面通過根據(jù)Excel文件的兩種后綴名(*.xlsx和*.xls)分別來實(shí)現(xiàn)。獲取文件后綴名的方法是:Path.GetExtension(fileName)方法,通過引用:using System.IO;實(shí)現(xiàn)代碼如下:(其中以下代碼中出現(xiàn)的filename都是帶盤符的絕對(duì)路徑)

  • 根據(jù)Excel文件的后綴名不同調(diào)用的主方法

     private DataTable FileToDataTable(string fileName)
        {
            DataTable dt = new DataTable();
            string extendName = Path.GetExtension(fileName);//獲取文件的后綴名
            switch (extendName.ToLower())
            {
                case ".xls":
                    dt = XlsToDataTable(fileName);
                    break;
                case ".xlsx":
                    dt = XlsxToDataTable(fileName);
                    break;
                default:
                    break;
            }
            return dt;
        }

     

  • XlsToDataTable()

     private DataTable XlsToDataTable(string fileName)
        {
            DataTable dataTable = new DataTable();
            Stream stream = null;
            try
            {
                stream = File.OpenRead(fileName);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(stream);
                HSSFSheet hssfsheet = (HSSFSheet)hssfworkbook.GetSheetAt(hssfworkbook.ActiveSheetIndex);
                HSSFRow hssfrow = (HSSFRow)hssfsheet.GetRow(0);
                int lastCellNum = (int)hssfrow.LastCellNum;
                for (int i = (int)hssfrow.FirstCellNum; i < lastCellNum; i++)
                {
                    DataColumn column = new DataColumn(hssfrow.GetCell(i).StringCellValue);
                    dataTable.Columns.Add(column);
                }
                dataTable.TableName = hssfsheet.SheetName;
                int lastRowNum = hssfsheet.LastRowNum;
                //列名后,從TABLE第二行開始進(jìn)行填充數(shù)據(jù)
                for (int i = hssfsheet.FirstRowNum + 1; i < hssfsheet.LastRowNum; i++)//
                {
                    HSSFRow hssfrow2 = (HSSFRow)hssfsheet.GetRow(i);
                    DataRow dataRow = dataTable.NewRow();
                    for (int j = (int)hssfrow2.FirstCellNum; j < lastCellNum; j++)//
                    {
                        dataRow[j] = hssfrow2.GetCell(j);//
                    }
                    dataTable.Rows.Add(dataRow);
                }
                stream.Close();
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(Page, GetType(), "alertForm", "alert(' Xls to DataTable: " + ex.Message + "');", true);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return dataTable;
        }

     

  • XlsxToDataTable()
    public DataTable XlsxToDataTable(string vFilePath)
        {
            DataTable dataTable = new DataTable();
            try
            {
                SLDocument sldocument = new SLDocument(vFilePath);
                dataTable.TableName = sldocument.GetSheetNames()[0];
                SLWorksheetStatistics worksheetStatistics = sldocument.GetWorksheetStatistics();
                int startColumnIndex = worksheetStatistics.StartColumnIndex;
                int endColumnIndex = worksheetStatistics.EndColumnIndex;
                int startRowIndex = worksheetStatistics.StartRowIndex;
                int endRowIndex = worksheetStatistics.EndRowIndex;
                for (int i = startColumnIndex; i <= endColumnIndex; i++)
                {
                    SLRstType cellValueAsRstType = sldocument.GetCellValueAsRstType(1, i);
                    dataTable.Columns.Add(new DataColumn(cellValueAsRstType.GetText(), typeof(string)));
                }
                for (int j = startRowIndex + 1; j <= endRowIndex; j++)
                {
                    DataRow dataRow = dataTable.NewRow();
                    for (int i = startColumnIndex; i <= endColumnIndex; i++)
                    {
                        dataRow[i - 1] = sldocument.GetCellValueAsString(j, i);
                    }
                    dataTable.Rows.Add(dataRow);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Xlsx to DataTable: \n" + ex.Message);
            }
            return dataTable;
        }

     

     

     

     

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多