Excel、SQL、VS6.0、特别是处理ListCtrl与Excel

一、关于CREATE TABLE tab1 (字段名 TEXT)中,字段名为“a.a.aa.a.b”时,提示:

“字段定义语法错误”

答:在字段名字外面加上“[ ]”就可以了!

二、创建一个以ListCtrl第一列为EXCEL第一行的表结构 插入一个以ListCtrlN列为EXCEL第二行的表结构

      CListCtrl& resultListCtrl = GetListCtrl();

      CString   strDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel驱动

      CString   strSQL    = _T("");

      CString   strExcelFile = _T("");

 

      //每天创建一个.xls文件

      CTime tm;

      tm = CTime::GetCurrentTime();

      strExcelFile.Format("%s%d%d%d.xls",

            ((CDCMS_SGApp*)AfxGetApp())->m_strExcelPath, tm.GetYear(), tm.GetMonth(),tm.GetDay());

 

      ((CDCMS_SGApp*)AfxGetApp())->m_strCurExcelPath = strExcelFile;

 

      TRY

      {

           //数据库字符串

            strSQL.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=/"%s/";DBQ=%s",

                strDriver, strExcelFile, strExcelFile);

           //创建数据库

            if( dbExcel.OpenEx(strSQL, CDatabase::noOdbcDialog) )

           {

                 //创建表结构的第一行 Tag Name:

                 CString strSQL2 = _T("");

                 //插入描述行SQL字符串

                 strSQL2.Format("INSERT INTO tab1 ([Tag Name:]");

                 //创建第1行位号名称字符串

              strSQL.Format("CREATE TABLE tab1 ([Tag Name:] TEXT");

                 CString strSQLTemp = _T("");

                 for(int i= 0; i<resultListCtrl.GetItemCount(); i++)

                 {

                       strSQLTemp.Format(",%s", resultListCtrl.GetItemText(i, 0));

                       strSQL2 += strSQLTemp;

                       strSQLTemp.Format(",[%s] TEXT", resultListCtrl.GetItemText(i, 0));

                       strSQL  += strSQLTemp;

                 }

                 strSQL += ")";

                 dbExcel.ExecuteSQL(strSQL); //创建表的第一行

 

                 //创建表的第二行 Description:

                 strSQL2 += ") VALUES ('Description:";

                 for(i= 0; i<resultListCtrl.GetItemCount(); i++)

                 {

                       strSQLTemp.Format("','%s", resultListCtrl.GetItemText(i, 2));

                       strSQL2 += strSQLTemp;

                 }

                 strSQL2 += "')";

                 dbExcel.ExecuteSQL(strSQL2); //创建表的第二行

           }     

           // 关闭数据库

            dbExcel.Close();

      }

      CATCH_ALL(e)

      {

            e->ReportError();

      }

      END_CATCH_ALL;

三、创建一个以ListCtrl所有列标题为第一行,列表所有数据为内容的EXCEL表格,即EXCEL表格与ListCtrl数据格式一致

      CListCtrl& resultListCtrl = GetListCtrl();

      CString   strDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel驱动

      CString   strSQL    = _T("");

      CString   strExcelFile = _T("");

 

      //每天创建一个.xls文件

      CTime tm;

      tm = CTime::GetCurrentTime();

      strExcelFile.Format("%s%d%d%d.xls",

            ((CDCMS_SGApp*)AfxGetApp())->m_strExcelPath, tm.GetYear(), tm.GetMonth(),tm.GetDay());

 

      ((CDCMS_SGApp*)AfxGetApp())->m_strCurExcelPath = strExcelFile;

 

      TRY

      {

           //数据库字符串

            strSQL.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=/"%s/";DBQ=%s",

                strDriver, strExcelFile, strExcelFile);

           //创建数据库

            if( dbExcel.OpenEx(strSQL, CDatabase::noOdbcDialog) )

           {

                 CHeaderCtrl* pHeader = resultListCtrl.GetHeaderCtrl();

                 //获得列,行的个数

                 int nColCount  = pHeader->GetItemCount();

                 int nLineCount = resultListCtrl.GetItemCount();

 

                 CString str;

                 str.Format("列:%d-行:%d", nColCount, nLineCount);

           //      AfxMessageBox(str);

 

                 int ColOrderArray[100] = {0};

                 CString col_Info[100]  = {""};

                 resultListCtrl.GetColumnOrderArray(ColOrderArray, nColCount);

 

                 //检索各列的信息,确定EXCEL列标题的内容,并将列标题的名称放于 col_Info

                 for(int i =0 ; i< nColCount; i++)

                 {

                       LVCOLUMN lvc;

                       char text[100] = {0};

                       lvc.mask       = LVCF_TEXT|LVCF_SUBITEM;

                       lvc.pszText    = text;

                       lvc.cchTextMax = 100;

                       resultListCtrl.GetColumn(ColOrderArray[i], &lvc);

                       col_Info[i]    = lvc.pszText;  //提取列的信息

                 }

 

                 //创建表结构

                 CString strSQLTemp = "(";

                 for(i =0 ; i< nColCount-1; i++)

                 {

                       strSQLTemp += "[";

                       strSQLTemp += col_Info[i];

                       strSQLTemp += "] TEXT,";

                 }

                 strSQLTemp += "[";

                 strSQLTemp += col_Info[nColCount-1];

                 strSQLTemp += "] TEXT)";

 

                 strSQL =  "CREATE TABLE tab1 ";

                 strSQL += strSQLTemp;

                 dbExcel.ExecuteSQL(strSQL);  //执行指令

 

                 //插入列表中的所有数据

                 int nItemCount = resultListCtrl.GetItemCount();

                 strSQLTemp = "(";

                 for(i =0 ; i< nColCount-1; i++)

                 {

                       strSQLTemp += col_Info[i];

                       strSQLTemp += " ,";

                      

                 }

                 strSQLTemp += col_Info[nColCount-1];

                 strSQLTemp += ")";

                  for(int itemNO =0; itemNO<nItemCount; itemNO++)

                 {   

                       strSQL =  "";

                       strSQL =  "INSERT INTO tab1 ";

                       strSQL += strSQLTemp;

                       strSQL += "VALUES ('";

                       for(i =0 ; i< nColCount-1; i++) //一行一行来(itemNO行号,i列号)

                       {

                             strSQL += resultListCtrl.GetItemText(itemNO, i);

                             strSQL += "','";

                       }

                       //因为分隔()的关系,把最后一列单独拿出来处理

                       strSQL += resultListCtrl.GetItemText(itemNO, nColCount-1);

                       strSQL += "')";

                       dbExcel.ExecuteSQL(strSQL);

                 }

           }     

           // 关闭数据库

            dbExcel.Close();

      }

      CATCH_ALL(e)

      {

            e->ReportError();

      }

      END_CATCH_ALL;

 

 

 

你可能感兴趣的:(Excel、SQL、VS6.0、特别是处理ListCtrl与Excel)