C# 및 EXCEL

2021 단어
1. EXCEL 링크 구성
        public OleDbConnection getExcelConn(string filepath)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=2'";
            OleDbConnection OleConn = new OleDbConnection(strConn);
            return OleConn;
        }

 2.EXCEL 작업
            String filepath = path + "xls\\" + Program.username + "_" + damlname + ".xls";
            OleDbConnection conn = getExcelConn(filepath);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "INSERT INTO [" + damlname + "$] (dh,qzh,ndu,jgname,bgqx,hh,jh,ptotal) values('" + nowdh + "','" + qzh + "','" +
                ndu + "','" + jgname + "','" + qx + "','" + hh + "','" + jh + "','" + ptotal + "')";
            cmd.ExecuteNonQuery();
            conn.Close();

 3.EXCEL 질의
        public string getjgdm(string jgname)
        {
            string jgdmpath = path + "xls\\jgdmdzb.xls";
            OleDbConnection OleConn = getExcelConn(jgdmpath);
            OleConn.Open();
            String sql = "SELECT   FROM  [ $] where  ='" + jgname + "'";// Sheet , sheet2, 
            OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
            DataSet OleDsExcle = new DataSet();
            OleDaExcel.Fill(OleDsExcle, " ");
            OleConn.Close();
            System.Data.DataTable table = OleDsExcle.Tables[0];
            string jgdm = table.Rows[0][0].ToString();
            return jgdm;
        }

좋은 웹페이지 즐겨찾기