熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#怎樣用excel.dll讀取excel文件

2022-06-13   來源: .NET編程 

  

  public DataTable ReadExcelToTable(string filepath)
        {
            string excelfilePath = ServerMapPath(Excel/Excelxls);
            ExcelApplication myExcel = new ExcelApplicationClass();
            ExcelWorkbooks myBooks = myExcelApplicationWorkbooks;
            object oMissing = SystemReflectionMissingValue;

  ExcelWorkbook myBook = myBooksOpen(excelfilePath oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing oMissing);

  //ExcelWorkbook myBook = myExcelWorkbooks[];
            int sheetint = myBookWorksheetsCount;//能得到sheet的數量

  ExcelWorksheet mySheet = (ExcelWorksheet)myBookWorksheets[];
            int rowsint = mySheetUsedRangeCellsRowsCount; //得到行數
            int columnsint = mySheetUsedRangeCellsColumnsCount;//得到列數
            SystemDataDataTable dt = new SystemDataDataTable(mytable);
            for (int i = ; i < columnsint; i++)
            {
                dtColumnsAdd(F + iToString() SystemTypeGetType(SystemString));
            }
            DataSet myDs = new DataSet();
            myDsTablesAdd(dt);
            DataRow myRow;
            myDsClear();

  for (int i = ; i < rowsint; i++) //第一行為標題不讀取
            {
                myRow = myDsTables[mytable]NewRow();
                for (int j = ; j < columnsint; j++)
                {

  ExcelRange r = (ExcelRange)mySheetCells[i j];
                    string strValue = rTextToString();
                    string aa = strValue;
                    string columnname = F + jToString();
                    myRow[columnname] = strValue;

  }
                myDsTables[mytable]RowsAdd(myRow);
            }

  return myDsTables[mytable];
        }

  


From:http://tw.wingwit.com/Article/program/net/201311/12028.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.