问题是将数据写入特定的单元并且文件已损坏

时间:2019-01-03 01:14:54

标签: c++ mfc

这不容易1.将数据写入特定的单元格(excel文件),并且2.写入,保存,复制后excel文件损坏 (在Windows OS 7上使用Visual C ++ 6.0)

//输入行数据excel(B6〜B8,B11 ...等特定单元格)

//Setting up chrome driver
WebDriverManager.getInstance(CHROME).setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

//Redirecting to the website
driver.get("https://codepen.io/rjsmer/full/vvewWp");

Robot robot = new Robot();
robot.mouseMove(120, 300);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);

Thread.sleep(2000);
robot.mouseMove(500, 320);

Thread.sleep(2000);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

//保存文件并复制新文件

CString strOneRecord = null;
for( pos = strList.GetHeadPosition(); pos != posTail; )
{
  CString str = recordList.GetNext( pos );
  strOneRecord += str;
  strOneRecord += szSeparator; 
  strOneRecord += "\t";
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我用excel9.h解决

    CString sSub;
    BOOL    blNewXls = FALSE;
    int nCol;
    int nSheetNum;
    int nCnt;
    CString strSerialNo,strInspector;

    COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);    
    _Application objApp;
    _Workbook objBook;
    Workbooks objBooks;
    Worksheets objSheets;
    _Worksheet objSheet;
    Range objRange;
    Font ft;
    LPDISPATCH lpDisp;

    if( !objApp.CreateDispatch("Excel.Application") ){AfxMessageBox("XLS ERROR");   return FALSE;}
    objApp.SetVisible(TRUE);
    objApp.SetUserControl(TRUE);
    lpDisp = objApp.GetWorkbooks();
    objBooks.AttachDispatch(lpDisp);

   lpDisp = objBooks.Open(destFileName, VOptional, VOptional, VOptional,VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional);
   objBook.AttachDispatch(lpDisp);
   lpDisp = objBook.GetSheets();
   objSheets.AttachDispatch(lpDisp);

   lpDisp = objSheets.GetItem(COleVariant((long)1));
   objSheet.AttachDispatch(lpDisp);

    objRange = objSheet.GetRange(COleVariant("C2"), COleVariant("C2")); 
    //excel cell number
    sSub.Format("%s", row[0]);
    objRange.SetValue(COleVariant(CString(sSub)));