1. 程式人生 > >c# 32位機和64位機 讀取Excel內容到DataSet

c# 32位機和64位機 讀取Excel內容到DataSet

----------------------32位機

//註釋說明

//ExclePath  為Excel路徑     批號  是指Excel檔案中某一列必填項

public static DataSet GetDataTableForExcel(String ExclePath)
        {
            string strCon = String.Empty;
            strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExclePath + "; Extended Properties='Excel 8.0;IMEX=1';";
            OleDbConnection olecon = new OleDbConnection(strCon);
            olecon.Open();
            DataTable dt = olecon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string tableName = dt.Rows[0][2].ToString().Trim();
            OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [" + tableName + "] Where 批號 is not null", strCon);// Where 條件根據實際情況進行更改
            DataSet myds = new DataSet();
            try
            {
                myda.Fill(myds);
            }
            catch { myds = null;}
            olecon.Close();
            return myds;
        }

-----------------------------64位機

/// <summary>
        /// 把Excel裡的資料轉換為DataTable,應用引用的com元件:Microsoft.Office.Interop.Excel.dll 讀取EXCEL檔案
        /// </summary>
        /// <param name="filenameurl">物理路徑</param>
        /// <param name="sheetIndex">sheet名稱的索引</param>
        /// <param name="splitstr">如果是已存在列,則自定義新增的字串</param>
        /// <returns></returns>
        public static DataTable ExecleToDataSet(string filenameurl, int sheetIndex, string splitstr)
        {
            //
            Microsoft.Office.Interop.Excel.Workbook wb = null;
            Microsoft.Office.Interop.Excel.Worksheet ws =null;
            bool isEqual = false;//不相等
            ArrayList columnArr = new ArrayList();//列欄位表
            DataSet myDs = new DataSet();
            DataTable xlsTable = myDs.Tables.Add("show");
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//lauch excel application
            if (excel != null)
            {
                excel.Visible = false;
                excel.UserControl = true;
                // 以只讀的形式開啟EXCEL檔案
                wb = excel.Workbooks.Open(filenameurl, missing, true, missing, missing, missing,
                 missing, missing, missing, true, missing, missing, missing, missing, missing);
                //取得第一個工作薄
                ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(sheetIndex);
                //取得總記錄行數(包括標題列)
                int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行數
                int columnsint = ws.UsedRange.Cells.Columns.Count;//得到列數
                DataRow dr;
                for (int i = 1; i <= columnsint; i++)
                {
                    //判斷是否有列相同
                    if (i >= 2)
                    {
                        int r = 0;
                        for (int k = 1; k <= i - 1; k++)//列從第一列到第i-1列遍歷進行比較
                        {
                            if (((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, i]).Text.ToString() == ((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, k]).Text.ToString())
                            {
                                //如果該列的值等於前面列中某一列的值
                                xlsTable.Columns.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, i]).Text.ToString() + splitstr + (r + 1).ToString(), typeof(string));
                                columnArr.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, i]).Text.ToString() + splitstr + (r + 1).ToString());
                                isEqual = true;
                                r++;
                                break;
                            }
                            else
                            {
                                isEqual = false;
                                continue;
                            }
                        }
                        if (!isEqual)
                        {
                            xlsTable.Columns.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, i]).Text.ToString(), typeof(string));
                            columnArr.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, i]).Text.ToString());
                        }
                    }
                    else
                    {
                        xlsTable.Columns.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, i]).Text.ToString(), typeof(string));
                        columnArr.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, i]).Text.ToString());
                    }
                }
                for (int i = 2; i <= rowsint; i++)
                {
                    dr = xlsTable.NewRow();
                    for (int j = 1; j <= columnsint; j++)
                    {
                        dr[columnArr[j - 1].ToString()] = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[i, j]).Text.ToString();
                    }
                    xlsTable.Rows.Add(dr);
                }
            }
            excel.Quit();
            excel = null;
            Dispose(ws,wb);
            return xlsTable;
        }