我从列表控件中打开Excel文件时遇到问题. [英] I have problem to open excel file from list control.

查看:89
本文介绍了我从列表控件中打开Excel文件时遇到问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!当我从excel保存列表控件数据时.但是当我按下打开按钮时.
我无法打开保存文件.

请检查我的密码并提供建议.谢谢(^ _ ^ _)


我的问题是这个.

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.

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();
}

推荐答案

帮助员工是您可以说的一种话,当您付钱给您帮助的时候.当您要求随机的陌生人免费工作时,您需要等待.

您创建的文件可以在Excel中打开吗?如果没有,则您的保存代码将被破坏.如果是这样,那么您需要逐步阅读代码并找出错误所在.你有回任何数据吗?是否抛出异常?当您拒绝告诉我们有关您的错误的任何有意义信息时,HELP MEEEEE既无知又傲慢.如果您甚至不了解我们需要了解的内容,然后尝试理解问题,那么对于您来说,这个问题几乎肯定太复杂了.
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.


我有一个相同的问题问题请帮帮我.感想
i have a same problem please help me. Thnaks


这篇关于我从列表控件中打开Excel文件时遇到问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆