C# 规则的Excel 导入,和不规则的Excel 导入

  #region 规则的excel
           DataTable dt = new DataTable();
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dd.ToString() + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
            OleDbConnection conn = new OleDbConnection(strConn);
           OleDbDataAdapter myCommand = null;
            try
           {
              conn.Open();
               DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
               string tableName = schemaTable.Rows[0][2].ToString().Trim();//获取 Excel 的表名,默认值是sheet1
               string strSql = "select * from [" + tableName + "]";
             myCommand = new OleDbDataAdapter(strSql, strConn);
               myCommand.Fill(dt, "dtSource");
            }
            catch (Exception ex)
          {
               throw;
           }
           finally
           {
            conn.Close();
                conn.Dispose();
            }

            #endregion
            #region 不规则的excel
            Microsoft.Office.Interop.Excel.Application app = new ApplicationClass();
            try
            {
                string[] path = dd.ToString().Split('|');
                //让后台执行设置为不可见
                app.Visible = false;
                object missing = Missing.Value;
                //打开已有的工作簿
                Workbook wBook = app.Workbooks.Open(path[0],
                   missing, missing, missing, missing, missing, missing, missing,
                   missing, missing, missing, missing, missing, missing, missing);

                //取得一个工作表
                //如果打开了已有的工作簿,也可以这样获取工作表
                Worksheet wSheet = wBook.ActiveSheet as Worksheet;
                Range firstCell = (Range)wSheet.Cells[1, 1];  //只举例第一个单元格被合并
                string text = firstCell.Text.ToString();  //单元格文本
                bool temp = (bool)firstCell.MergeCells;  //表示本单元格是否是合并单元格
                Range mergeArea = firstCell.MergeArea;
                int count = mergeArea.Cells.Count; //合并列的个数(不分上下还是左右)//得到了总列数

                ////胡志雄写 特定的格式
                Range _YearMonth = (Range)wSheet.Cells[2, 3];
                string _yearText = _YearMonth.Text.ToString();
                if (_yearText == "")
                {
                    Msgbox.Info("请设置期间");
                    return;
                }
                string[] str = _yearText.Split('-');
                int _year = int.Parse(str[0]);
                int _month = int.Parse(str[1]);
                 FBill _fbill = _ifbill.Create(FrmLogin.UserInfo);
                _fbill.FBillerID = FrmLogin.UserInfo.FUserID;
                _fbill.FCustomID = int.Parse(path[1]);
                _fbill.FMonth = _month;
                _fbill.FYear = _year;
                _fbill.FVersion = "";
                _fbill.FTrantype = _typeid;
                _fbill.FDate = DateTime.Now;
                _fbill.FImport = 1;//表示是通过导入的
                _fbill.Entry = GetEntry(wSheet, _year, _month,count);
                _ifbill.Save(FrmLogin.UserInfo, _fbill);

                //设置禁止弹出保存和覆盖的询问提示框
                app.DisplayAlerts = false;
                app.AlertBeforeOverwriting = false;

            }
            catch (Exception er)
            {
                throw new Exception(er.Message);
            }
            finally
            {
                //确保Excel进程关闭
                app.Quit();
                app = null;
            }

转载于:https://www.cnblogs.com/alixiong/archive/2011/03/01/1968138.html

你可能感兴趣的:(C# 规则的Excel 导入,和不规则的Excel 导入)