Click here to Skip to main content
15,885,933 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! When I save my list control data from excel. But when I push the open button.
I cannot open my save file.

Plz check me my codes and give advices. Thanks (^_^_)


My problem part is this.
C++
CXLEzAutomation XL(false);
      //Get file name for opening Excel file
      CString szFileName = DataRead.GetPathName();
      strThisPath.Format("%s\\%s.xls",save_path,szFileName);   
         
         if(!SetCurrentDirectory(strThisPath))
         {
         if(szFileName.IsEmpty())
            return;
               
         //Close Excel if failed to open file 
         if(!XL.OpenExcelFile(szFileName))
         {
               
           m_cList.DeleteAllItems(); 

            CString address, field, value;
          for(int i=1; ; i++)
         {
            address=XL.GetCellValue(1,i+1);  
            field=XL.GetCellValue(2,i+1);    
            value=XL.GetCellValue(3,i+1);    

            if ( address == "") 
              break;

            m_cList.InsertItem(i-1,address); 
             m_cList.SetItemText(i-1,1,field); 
            m_cList.SetItemText(i-1,2,value); 

   }

void CFormThird::OnXlSave()
{
//BOOL bRead;

   int mrowNum,   mcolNum;

int rowNum = 1;
   int colNum = 1;
   char temp [10];
   CString m_SFileName;

   CXLEzAutomation XL(FALSE);

   m_SFileName ="MODBUS DATA FILE";

   //?? ???

   XL.SetCellValue(colNum++, 1, "Address");
   XL.SetCellValue(colNum++, 1, "Field");
   XL.SetCellValue(colNum++, 1, "Value");

   rowNum= m_cList.GetItemCount();
   //   colNum = m_cList.GetItemCount();
   colNum = 3;

   //   XL.SetCellValue(mcolNum+1,mrowNum+2,m_cList.GetItemText(mcolNum,mrowNum));
   for (mrowNum=0; mrowNum<rowNum ; mrowNum++)
   {
      for(mcolNum=0; mcolNum<colNum; mcolNum++)
      {
         CString   mText;
         mText   = m_cList.GetItemText(mrowNum,mcolNum);
         XL.SetCellValue(mcolNum+1,mrowNum+2,mText);//m_cList.GetItemText(mcolNum,mrowNum));
      }
   }

   char Filter[] = "Excel File(*.xls) |*.xls| ALL FILES(*.*) |*.*|";

   CFileDialog DataWrite(FALSE, "Excel File(*.xls)", "*.xls", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, Filter, NULL);
   
   if(DataWrite.DoModal() == IDOK)
   {
      XL.SaveFileAs(DataWrite.GetPathName());
      
   }XL.ReleaseExcel();
}


void CFormThird::OnXlOpen()
{
   //Use Windows file dialog to obtain FileName 
   char Filter[] = "Excel File(*.xls) |*.xls| ALL FILES(*.*) |*.*|";

   CFileDialog   DataRead(TRUE, // TRUE for FileOpen, FALSE for FileSaveAs
      "xls", NULL,
      OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT,
      Filter,
      NULL);
   
   char save_path[MAX_PATH];
   GetCurrentDirectory(MAX_PATH,save_path);
   
   UpdateData(TRUE);
    CString strThisPath;
   GetModuleFileName( NULL, save_path, _MAX_PATH);
         
      CXLEzAutomation XL(false);
      //Get file name for opening Excel file
      CString szFileName = DataRead.GetPathName();
      strThisPath.Format("%s\\%s.xls",save_path,szFileName);   
         
         if(!SetCurrentDirectory(strThisPath))
         {
         if(szFileName.IsEmpty())
            return;
               
         //Close Excel if failed to open file 
         if(!XL.OpenExcelFile(szFileName))
         {
               
           m_cList.DeleteAllItems(); 

            CString address, field, value;
          for(int i=1; ; i++)
         {
            address=XL.GetCellValue(1,i+1);  
            field=XL.GetCellValue(2,i+1);    
            value=XL.GetCellValue(3,i+1);    

            if ( address == "") 
              break;

            m_cList.InsertItem(i-1,address); 
             m_cList.SetItemText(i-1,1,field); 
            m_cList.SetItemText(i-1,2,value); 

   }
   XL.ReleaseExcel();
}
Posted
Updated 7-Jul-11 16:52pm
v5
Comments
fjdiewornncalwe 7-Jul-11 22:26pm    
Just formatted the code a bit for readability. You should use the pre tags to wrap your code blocks so that they are "prettier"
fjdiewornncalwe 7-Jul-11 22:28pm    
What is the exception when you execute the code? What line does it throw the exception on?
Sun-Mi Kang 7-Jul-11 22:44pm    
When I execute code. I can save my file,. but i cannot browse my save excel file when I push the open button

HELP MEEEE is the sort of thing you can say, when you're paying people to help you. When you are asking random strangers to do your work for free, you need to wait.

Is the file that you create able to open in Excel ? If not, then your save code is broken. If so, then you need to step through the read code and figure out where it goes wrong. Do you get any data back ? Is there an exception thrown ? HELP MEEEEE comes across as ignorant as well as arrogant, when you refuse to tell us anything meaningful about your error. If you don't even understand what we need to know, to try to understand the problem, then the problem is almost certainly too complex for you to start with.
 
Share this answer
 
i have a same problem please help me. Thnaks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900