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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值