• VC++ 访问EXCEL


    https://blog.csdn.net/baidu_37503452/article/details/78316671

    本地项目    VCExcelAccess

    DWORD dwStart = GetTickCount();
        CApplication app;
        CRange range;
        CWorkbook book;
        CWorkbooks books;
        CWorksheet sheet;
        CWorksheets sheets;
        LPDISPATCH lpdisp;
        COleVariant vresult;
        COleVariant covtrue((short)TRUE);
        COleVariant covfalse((short)FALSE);
        COleVariant covoptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    
        //create the server
        if (!app.CreateDispatch("Excel.Application"))
        {
            AfxMessageBox("Cannot start Excel server");
            return;
        }
        app.put_DisplayAlerts(false);
        app.put_Visible(FALSE);
        books.AttachDispatch(app.get_Workbooks());
    
        CString ExcelFile = "G:\colibri\ISL3.5\MSA\7-4\MSA\1.xlsx";
        CString saveFile = "G:\colibri\ISL3.5\MSA\7-4\MSA\2.xlsx";
        //open file
        lpdisp = books.Open(
            ExcelFile,
            covoptional, covfalse, covoptional, covoptional, covoptional,
            covoptional, covoptional, covoptional, covoptional, covoptional,
            covoptional, covoptional, covoptional, covoptional);
    
        //get workbook
        book.AttachDispatch(lpdisp);
    
        //get worksheets
        sheets.AttachDispatch(book.get_Worksheets());
        
        /*lpdisp = book.get_ActiveSheet();
        sheet.AttachDispatch(lpdisp);*/
    
        CString str;
        sheet = sheets.get_Item(COleVariant((short)2));  //访问第二个sheet
        range = sheet.get_Range(COleVariant("A1"), COleVariant("Z1000"));
        VARIANT valArray = range.get_Value2();
    
        for (int row = 1; row <= 1000; row++)
        {
            for (int col = 1; col <= 26; col++)
            {
                str = GetCellVal(valArray, row, col);
                SetCellVal(valArray, row, col,"test");
            }
        }
    range.put_Value2(valArray);
        VariantClear(&valArray);
    book.SaveAs(COleVariant(saveFile), covoptional,
            covoptional, covoptional,
            covoptional, covoptional, (long)0, covoptional, covoptional, covoptional,
            covoptional, covoptional);
        book.Close(covoptional, COleVariant(ExcelFile), covoptional);
        books.Close();
        app.Quit();
    
    
    CString CVCExcelAccessDlg::GetCellVal(VARIANT data , int row, int col)
    {
        VARIANT val;
        long index[2];
        index[0] = row;
        index[1] = col;
        SafeArrayGetElement(data.parray, index, &val);    //获取值
        return  Var2CString(val);
    }
    
    int CVCExcelAccessDlg::SetCellVal(VARIANT data, int row, int col,CString strData)
    {
        VARIANT val;
        long index[2];
        index[0] = row;
        index[1] = col;
        variant_t var_Datacfg = (LPSTR)(LPCTSTR)strData;
        HRESULT hRet = ::SafeArrayPutElement(data.parray, index, &var_Datacfg);
        return  hRet;
    }
  • 相关阅读:
    Linux 信号详解四(pause,alarm)
    Linux 信号详解三(sleep,raise)
    Linux 信号详解二(信号分类,信号处理,kill)
    下载windows server ISO(msdn订户下载)
    科2项目攻略
    Centos 7 LVM xfs文件系统修复
    python连接redis sentinel集群
    jquery on() bind()绑定的点击事件在js动态新添加的元素生效
    虚拟化环境下的CentOS7网络环境存在的问题
    石排科目二考场最新攻略
  • 原文地址:https://www.cnblogs.com/profession/p/12850483.html
Copyright © 2020-2023  润新知