void CMyExcel::PrintOut(short CopySum) { if(CopySum<=0) CopySum=1; COleVariant covTrue((short)TRUE); COleVariant covFalse((short)FALSE); COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR); MySheet.PrintOut(vtMissing,vtMissing,(_variant_t)(short)CopySum,vtMissing ,vtMissing,vtMissing,covTrue,vtMissing); }
void ole_close_excel(const std::wstring &xlsx, CApplication &excel_application, CWorkbooks &books, CWorkbook &book, CWorksheets &sheets) { COleVariant covTrue((short)TRUE), covFalse((short)FALSE); COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); book.Close(COleVariant(short(FALSE)),COleVariant(xlsx.c_str()), covOptional); books.Close(); // 释放资源 sheets.ReleaseDispatch(); book.ReleaseDispatch(); books.ReleaseDispatch(); excel_application.Quit(); excel_application.ReleaseDispatch(); }
CMyExcel::~CMyExcel() { COleVariant covFalse((short)FALSE); COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR); MyRange.ReleaseDispatch(); MySheet.ReleaseDispatch(); MySheets.ReleaseDispatch(); MyBook.Close(covFalse,_variant_t(strFilePath),covOptional); MyBook.ReleaseDispatch(); MyBooks.Close(); MyBooks.ReleaseDispatch(); MyApp.Quit(); MyApp.ReleaseDispatch(); CoUninitialize(); }
void CModbusPollView::OnSetupExcelloging() { COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //创建Excel 2000服务器(启动Excel) if (!m_ExcelApp.CreateDispatch(_T("Excel.Application"),NULL)) { AfxMessageBox(_T("Create Excel Fail!")); exit(1); } m_ExcelApp.SetVisible(TRUE); m_wbsMyBooks.AttachDispatch(m_ExcelApp.GetWorkbooks(),true); m_wbMyBook.AttachDispatch(m_wbsMyBooks.Add(covOptional)); //得到Worksheets m_wssMysheets.AttachDispatch(m_wbMyBook.GetWorksheets(),true); //得到sheet1 m_wsMysheet.AttachDispatch(m_wssMysheets.GetItem(_variant_t("Sheet1")),true); //得到全部Cells,此时,rgMyRge是cells的集合 m_rgMyRge.AttachDispatch(m_wsMysheet.GetCells(),true); m_rgMyRge.SetValue(_variant_t("")); m_curexcelrow=1; CString str; str=_T("Time"); m_rgMyRge.SetItem(_variant_t((long)m_curexcelrow),_variant_t((long)m_curexcelrow),_variant_t(str)); for (int i=0;i<m_Quantity;i++) { str.Format(_T("%d"),m_address+i); m_rgMyRge.SetItem(_variant_t((long)m_curexcelrow),_variant_t((long)i+2),_variant_t(str)); } m_logExcel=FALSE; }
void CDlgHistory::OnBnClickedButtonExport() { // TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); GetDlgItem(IDC_BUTTON_EXPORT)->EnableWindow(FALSE); COleVariant covTrue((short)TRUE),covFalse((short)FALSE),covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR); book.PrintPreview(_variant_t(false)); CFileDialog FileDialog(FALSE,"xlsx",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("Microsoft Excel 2007(*.xlsx)|*.xlsx|所有文件(*.*)"),this); if(FileDialog.DoModal()!=IDOK) { GetDlgItem(IDC_BUTTON_EXPORT)->EnableWindow(TRUE); return; } CString cStrFile=FileDialog.GetPathName(); //选择保存路径名称 if(::PathFileExists(cStrFile)) DeleteFile(cStrFile); CString AntID,SensorID,StrStartTime,StrStopTime,tmpData,passWord,StartTime,StopTime,message,strRecordTime,rowCount,strRow,strLimit; StrStartTime.Format("%f",m_DataBegin); StrStopTime.Format("%f",m_DataEnding); CTime m_Startdate; m_CtlDateTimeStartTime.GetTime(m_Startdate); StartTime.Format("%d%02d%02d",m_Startdate.GetYear(), m_Startdate.GetMonth(),m_Startdate.GetDay()); _RecordsetPtr m_pRecordsetch; m_pRecordsetch.CreateInstance(__uuidof(Recordset)); books = app.get_Workbooks(); book = books.Add(covOptional); sheets = book.get_Worksheets(); sheets.Add(vtMissing,vtMissing,vtMissing,vtMissing); int nCount; char cCell; for(int i=1;i<5;i++) { tmpData.Format("%d",i); AntID = "天线"+tmpData; sheet = sheets.get_Item(COleVariant((short)i)); //得到第一个工作表 sheet.put_Name(AntID); for(int j=1;j<13;j++) { nCount = 3; tmpData.Format("%d",j); SensorID = "传感器"+tmpData; cCell = 'A' + 2*(j-1); rowCount.Format(_T("%c"),cCell); rowCount+="1"; range = sheet.get_Range(COleVariant(rowCount),COleVariant(rowCount)); range.put_Value2(COleVariant(SensorID)); rowCount.Format(_T("%c"),cCell); rowCount+="2"; range = sheet.get_Range(COleVariant(rowCount),COleVariant(rowCount)); range.put_Value2(COleVariant("时间")); rowCount.Format(_T("%c"),cCell+1); rowCount+="2"; range = sheet.get_Range(COleVariant(rowCount),COleVariant(rowCount)); range.put_Value2(COleVariant("温度")); _bstr_t bstrSQLdata ="SELECT Temperature,RecordTime FROM DB_Data where ReaderID='"+strReader+"' and AntID='"+AntID+"' and SensorID='"+SensorID+"' and RecordTime<"+StrStopTime+" and RecordTime>"+StrStartTime+" and (Notes='正常' or Notes='超限') order by DataID;"; m_pRecordsetch->Open(bstrSQLdata,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); while(!m_pRecordsetch->adoEOF) { CString temper=(LPCSTR)(_bstr_t)m_pRecordsetch->GetCollect("Temperature"); CString time=(LPCSTR)(_bstr_t)m_pRecordsetch->GetCollect("RecordTime"); COleDateTime t=atof(time); strRecordTime=t.Format("%Y-%m-%d %H:%M:%S"); rowCount.Format(_T("%c"),cCell); strRow.Format(_T("%d"),nCount); rowCount+=strRow; range = sheet.get_Range(COleVariant(rowCount),COleVariant(rowCount)); range.put_NumberFormat(COleVariant("hh:mm:ss")); range.put_Value2(COleVariant(strRecordTime)); //设置时间 rowCount.Format(_T("%c"),cCell+1); rowCount+=strRow; range = sheet.get_Range(COleVariant(rowCount),COleVariant(rowCount)); range.put_Value2(COleVariant(temper)); //设置温度 time.ReleaseBuffer(); temper.ReleaseBuffer(); m_pRecordsetch->MoveNext(); nCount++; } m_pRecordsetch->Close(); } range = sheet.get_Range(COleVariant("A3"),COleVariant("A3")); COleVariant vResult =range.get_Value2(); if(vResult.vt==VT_EMPTY) { passWord="******"; } else { SYSTEMTIME st; VariantTimeToSystemTime(vResult.date, &st); passWord.Format("%d%02d%02d",st.wYear,st.wMonth,st.wDay); passWord+="hshiot"; } sheet.Protect(_variant_t(passWord),vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing); } book.SaveCopyAs(COleVariant(cStrFile)); //保存到cStrFile文件 book.put_Saved(true); books.Close(); book.ReleaseDispatch(); books.ReleaseDispatch(); app.ReleaseDispatch(); app.Quit(); message = "已导出数据,文件位置 :"+cStrFile; AfxMessageBox(message); GetDlgItem(IDC_BUTTON_EXPORT)->EnableWindow(TRUE); }
void CLogAnalyzerView::OnGeneratereportExcel() { #ifdef EXCEL_REPORT try { if (m_logger->GetFileName().empty()) return; CApplication app; CWorkbook exlBook; CWorkbooks exlBooks; CWorksheet exlSheet; CWorksheets exlSheets; CChart chart; CRange range; CChartObjects chartobjects; LPDISPATCH lpDisp; COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); // Start Excel and get the Application object. if(!app.CreateDispatch("Excel.Application")) { ERROR_MESSAGEBOX("Couldn't start Excel and get an application 0bject"); return; } app.put_Visible(TRUE); // Get Workbooks collection. lpDisp = app.get_Workbooks(); ASSERT(lpDisp); // Attach the IDispatch pointer exlBooks.AttachDispatch(lpDisp); exlBook = exlBooks.Add(covOptional); // Get sheets. lpDisp = exlBook.get_Sheets(); ASSERT(lpDisp); exlSheets.AttachDispatch(lpDisp); lpDisp = exlSheets.get_Item( COleVariant((short)(1)) ); ASSERT(lpDisp); // Attach the lpDisp pointer to a Worksheet object. exlSheet.AttachDispatch(lpDisp); const std::map<tstring, Poco::SharedPtr<CComponent>>& componentMap = m_logger->GetComponents(); char charPos[32]; int nSize = 0; if (componentMap.size() > 0) { std::map<tstring, Poco::SharedPtr<CComponent>>::const_iterator ite = componentMap.begin(); int nIndex = 3; while (ite != componentMap.end()) { if (ite->second->GetTraceLogs().size() > 0) { sprintf(charPos, "A%d", nIndex); lpDisp = exlSheet.get_Range(COleVariant(charPos), COleVariant(charPos)); ASSERT(lpDisp); range.AttachDispatch(lpDisp); // Attach the IDispatch pointer range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(ite->second->GetComponentName().c_str())); range.put_Value2(COleVariant(ite->second->GetComponentName().c_str())); int nTrace = 0, nNotification = 0, nError = 0, nInformation = 0, nDebug = 0, nCritical = 0, nWarning = 0, nFatal = 0; if (nSize == 0) { nTrace = 10; nNotification = 10; nError = 5, nInformation = 50, nDebug = 2, nCritical = 2, nWarning = 9, nFatal = 1; } else if (nSize == 1) { nTrace = 10; nNotification = 25; nError = 4, nInformation = 36, nDebug = 5, nCritical = 0, nWarning = 20, nFatal = 1; } else if (nSize == 2) { nTrace = 0; nNotification = 5; nError = 4, nInformation = 100, nDebug = 5, nCritical = 5, nWarning = 15, nFatal = 0; } else if (nSize == 3) { nTrace = 0; nNotification = 5; nError = 4, nInformation = 82, nDebug = 36, nCritical = 1, nWarning = 50, nFatal = 0; } //ite->second->getLogTypeCount(nTrace, nNotification, nError, nInformation, nDebug, nCritical, nWarning, nFatal); //Trace sprintf(charPos, "B%d", nIndex); lpDisp = exlSheet.get_Range(COleVariant(charPos), COleVariant(charPos)); range.AttachDispatch(lpDisp); sprintf(charPos, "%d", nTrace); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(charPos)); range.put_Value2(COleVariant(charPos)); //Debug sprintf(charPos, "C%d", nIndex); lpDisp = exlSheet.get_Range(COleVariant(charPos), COleVariant(charPos)); range.AttachDispatch(lpDisp); sprintf(charPos, "%d", nDebug); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(charPos)); range.put_Value2(COleVariant(charPos)); //Information sprintf(charPos, "D%d", nIndex); lpDisp = exlSheet.get_Range(COleVariant(charPos), COleVariant(charPos)); range.AttachDispatch(lpDisp); sprintf(charPos, "%d", nInformation); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(charPos)); range.put_Value2(COleVariant(charPos)); //Warning sprintf(charPos, "E%d", nIndex); lpDisp = exlSheet.get_Range(COleVariant(charPos), COleVariant(charPos)); range.AttachDispatch(lpDisp); sprintf(charPos, "%d", nWarning); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(charPos)); range.put_Value2(COleVariant(charPos)); //Error sprintf(charPos, "F%d", nIndex); lpDisp = exlSheet.get_Range(COleVariant(charPos), COleVariant(charPos)); range.AttachDispatch(lpDisp); sprintf(charPos, "%d", nError); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(charPos)); range.put_Value2(COleVariant(charPos)); //Critical sprintf(charPos, "G%d", nIndex); lpDisp = exlSheet.get_Range(COleVariant(charPos), COleVariant(charPos)); range.AttachDispatch(lpDisp); sprintf(charPos, "%d", nCritical); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(charPos)); range.put_Value2(COleVariant(charPos)); //Notification sprintf(charPos, "H%d", nIndex); lpDisp = exlSheet.get_Range(COleVariant(charPos), COleVariant(charPos)); range.AttachDispatch(lpDisp); sprintf(charPos, "%d", nNotification); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(charPos)); range.put_Value2(COleVariant(charPos)); //Fatal sprintf(charPos, "I%d", nIndex); lpDisp = exlSheet.get_Range(COleVariant(charPos), COleVariant(charPos)); range.AttachDispatch(lpDisp); sprintf(charPos, "%d", nFatal); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant(charPos)); range.put_Value2(COleVariant(charPos)); ++nIndex; ++nSize; } ++ite; } } lpDisp = exlSheet.get_Range(COleVariant("B2"), COleVariant("B2")); ASSERT(lpDisp); range.AttachDispatch(lpDisp); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant("TRACE")); range.put_Value2(COleVariant("TRACE")); lpDisp = exlSheet.get_Range(COleVariant("C2"), COleVariant("C2")); ASSERT(lpDisp); range.AttachDispatch(lpDisp); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant("DEBUG")); range.put_Value2(COleVariant("DEBUG")); lpDisp = exlSheet.get_Range(COleVariant("D2"), COleVariant("D2")); ASSERT(lpDisp); range.AttachDispatch(lpDisp); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant("INFORMATION")); range.put_Value2(COleVariant("INFORMATION")); lpDisp = exlSheet.get_Range(COleVariant("E2"), COleVariant("E2")); ASSERT(lpDisp); range.AttachDispatch(lpDisp); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant("WARNING")); range.put_Value2(COleVariant("WARNING")); lpDisp = exlSheet.get_Range(COleVariant("F2"), COleVariant("F2")); ASSERT(lpDisp); range.AttachDispatch(lpDisp); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant("ERROR")); range.put_Value2(COleVariant("ERROR")); lpDisp = exlSheet.get_Range(COleVariant("G2"), COleVariant("G2")); ASSERT(lpDisp); range.AttachDispatch(lpDisp); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant("CRITICAL")); range.put_Value2(COleVariant("CRITICAL")); lpDisp = exlSheet.get_Range(COleVariant("H2"), COleVariant("H2")); ASSERT(lpDisp); range.AttachDispatch(lpDisp); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant("NOTIFICATION")); range.put_Value2(COleVariant("NOTIFICATION")); lpDisp = exlSheet.get_Range(COleVariant("I2"), COleVariant("I2")); ASSERT(lpDisp); range.AttachDispatch(lpDisp); range.put_Value(COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR), COleVariant("FATAL")); range.put_Value2(COleVariant("FATAL")); // The cells are populated. To start the chart, // declare some long variables and site the chart. long left, top, width, height; left = 200; top = 200; width = 450; height = 350; lpDisp = exlSheet.ChartObjects(covOptional); ASSERT(lpDisp); chartobjects.AttachDispatch(lpDisp); // Attach the lpDisp pointer // for ChartObjects to the chartobjects // object. CChartObject chartobject = chartobjects.Add(left, top, width, height); //defines the rectangle, // adds a new chart at that rectangle and // assigns its object reference to a // ChartObject variable named chartobject chart.AttachDispatch(chartobject.get_Chart()); sprintf(charPos, "I%d", nSize + 2); lpDisp = exlSheet.get_Range(COleVariant("A2"), COleVariant(charPos)); // The range containing the data to be charted. ASSERT(lpDisp); range.AttachDispatch(lpDisp); VARIANT var; // ChartWizard needs a Variant for the Source range. var.vt = VT_DISPATCH; // .vt is the usable member of the tagVARIANT var.pdispVal = lpDisp; // Assign IDispatch pointer chart.ChartWizard(var, // Source. COleVariant((short)52), // Gallery: stacked column. covOptional, // Format, use default. COleVariant((short)1), // PlotBy: xlRows. COleVariant((short)1), // CategoryLabels. COleVariant((short)1), // SeriesLabels. COleVariant((short)TRUE), // HasLegend. COleVariant("Trace Report"), // Title. COleVariant("Components"), // CategoryTitle. COleVariant("Trace Count"), // ValueTitles. covOptional // ExtraTitle. ); range.ReleaseDispatch(); exlSheet.ReleaseDispatch(); exlSheets.ReleaseDispatch(); exlBook.ReleaseDispatch(); exlBooks.ReleaseDispatch(); chart.ReleaseDispatch(); chartobject.ReleaseDispatch(); app.ReleaseDispatch(); } catch(COleException *e) { char buf[1024]; sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc); ::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK); } catch(COleDispatchException *e) { char buf[1024]; sprintf(buf, "COleDispatchException. SCODE: %08lx, Description: \"%s\".", (long)e->m_wCode, (LPSTR)e->m_strDescription.GetBuffer(1024)); ::MessageBox(NULL, buf, "COleDispatchException", MB_SETFOREGROUND | MB_OK); } catch(...) { ::MessageBox(NULL, "General Exception caught.", "Catch-All", MB_SETFOREGROUND | MB_OK); } #endif }
void CSendRedPacketRecord::ExportSendRedPacketToexel() { // TODO: 在此添加控件通知处理程序代码 string conditon; conditon= "where 1=1 order by send_time desc"; uistruct::REDPACKETSENDLIST SendRedPacketList; int nItem = theApp.m_SqliteDeal.GetRedPacketSendRecordList(conditon ,&SendRedPacketList ) ; if (SendRedPacketList.size() == 0) { UiFun::MessageBoxEx(UiFun::UI_LoadString("TRAD_MODULE" , "TRAD_NOT_RECORD_OUT" ,theApp.gsLanguage) , UiFun::UI_LoadString("COMM_MODULE" , "COMM_TIP" ,theApp.gsLanguage) ,MFB_OK|MFB_TIP ); return; } CFileDialog dlg(FALSE,NULL,UiFun::UI_LoadString("MENU" , "MENU_SENDRED" ,theApp.gsLanguage),OFN_HIDEREADONLY|OFN_FILEMUSTEXIST ,UiFun::UI_LoadString("COMM_MODULE" , "COMM_FILE" ,theApp.gsLanguage)+" (*.xls)|*.xls||"); if (IDOK != dlg.DoModal()) { return; } CString strFile = dlg.GetPathName(); if (!((CDacrsUIDlg*)(theApp.m_pMainWnd))->GetFileName(strFile,_T(".xls"))) { return; } struct LISTCol { string name ; UINT size ; } listheadr[7] = { {UiFun::UI_LoadString("P2P_MODULE" , "MORTTARD_SENDGIFT" ,theApp.gsLanguage) , 70}, {UiFun::UI_LoadString("MORTTARD_MODULE" , "P2P_PROMOTER" ,theApp.gsLanguage), 30}, {UiFun::UI_LoadString("MORTTARD_MODULE" , "MORTTARD_TYPE" ,theApp.gsLanguage) , 10}, {UiFun::UI_LoadString("MORTTARD_MODULE" , "MORTTARD_GRABTIME" ,theApp.gsLanguage) , 20}, {UiFun::UI_LoadString("MORTTARD_MODULE" , "MORTTARD_TOTALMONEY" ,theApp.gsLanguage) ,30}, {UiFun::UI_LoadString("MORTTARD_MODULE" , "MORTTARD_COUNT" ,theApp.gsLanguage) ,10}, {UiFun::UI_LoadString("RECEIVE_MODULE" , "RECEIVE_ACTIVATION_STATE" ,theApp.gsLanguage) , 10} }; COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); CApplication app; CWorkbooks books; CWorkbook book; CWorksheets sheets; CWorksheet sheet; CRange range; CFont0 font; if (!app.CreateDispatch(_T("Excel.Application"))) { UiFun::MessageBoxEx(UiFun::UI_LoadString("TRAD_MODULE" , "TRAD_NOT_OFFICE" ,theApp.gsLanguage) , UiFun::UI_LoadString("COMM_MODULE" , "COMM_TIP" ,theApp.gsLanguage),MFB_OK|MFB_TIP ); return; } //Get a new workbook. books = app.get_Workbooks(); book = books.Add(covOptional); sheets = book.get_Worksheets(); sheet = sheets.get_Item(COleVariant((short)1)); ////////////////////////////////////CListCtrl控件report风格////////////////////////////////////////////////////////// //CHeaderCtrl *pmyHeaderCtrl; //pmyHeaderCtrl = m_listCtrl.GetHeaderCtrl();//此句取得CListCtrl控件的列表頭 int iRow,iCol; int m_cols = 7; int m_rows = SendRedPacketList.size(); HDITEM hdi; TCHAR lpBuffer[256]; bool fFound = false; hdi.mask = HDI_TEXT; hdi.pszText = lpBuffer; hdi.cchTextMax = 256; CString colname; CString strTemp; for(iCol=0; iCol <m_cols; iCol++)//将列表的标题头写入EXCEL { UiFun::GetCellName(1 ,iCol + 1, colname); range = sheet.get_Range(COleVariant(colname),COleVariant(colname)); //pmyHeaderCtrl-> GetItem(iCol, &hdi); range.put_Value2(COleVariant(listheadr[iCol].name.c_str())); int nWidth = listheadr[iCol].size; //m_listCtrl.GetColumnWidth(iCol)/6; //得到第iCol+1列 range.AttachDispatch(range.get_Item(_variant_t((long)(iCol+1)),vtMissing).pdispVal,true); //设置列宽 range.put_ColumnWidth(_variant_t((long)nWidth)); } range = sheet.get_Range(COleVariant( _T("A1 ")), COleVariant(colname)); range.put_RowHeight(_variant_t((long)50));//设置行的高度 font = range.get_Font(); font.put_Bold(covTrue); range.put_VerticalAlignment(COleVariant((short)-4108));//xlVAlignCenter = -4108 COleSafeArray saRet; DWORD numElements[]={m_rows,m_cols}; //5x2 element array saRet.Create(VT_BSTR, 2, numElements); range = sheet.get_Range(COleVariant( _T("A2 ")),covOptional); range = range.get_Resize(COleVariant((short)m_rows),COleVariant((short)m_cols)); long index[2]; range = sheet.get_Range(COleVariant( _T("A2 ")),covOptional); range = range.get_Resize(COleVariant((short)m_rows),COleVariant((short)m_cols)); int iLine = 0; iRow = 1; iCol = 1; vector<uistruct::REDPACKETSEND_t>::const_iterator pitem = SendRedPacketList.begin(); for(;pitem != SendRedPacketList.end();pitem++,iRow++) { map<int,string> item; GetExportCol(item,*pitem); for ( iCol = 1; iCol <= m_cols; iCol++) { index[0]=iRow-1; index[1]=iCol-1; string strTemp = item[iCol-1]; CString szTemp = strTemp.c_str(); BSTR bstr = szTemp.AllocSysString(); saRet.PutElement(index,bstr); SysFreeString(bstr); } } range.put_Value2(COleVariant(saRet)); saRet.Detach(); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// book.SaveCopyAs(COleVariant(strFile)); // cellinterior.ReleaseDispatch(); book.put_Saved(true); book.ReleaseDispatch(); books.ReleaseDispatch(); app.Quit(); app.ReleaseDispatch(); }
void CBatchSNDlg::OnBnClickedCalibrationPointReport() { CString Product_Head_File_Name; CString strFilter; CString strFilename; CString strTemp; CString RegisterName; CString RegisterID; CString logstr; _variant_t temp_variant; _Application app; Workbooks books; _Workbook book; Worksheets sheets; _Worksheet sheet; Range range; Range rgMyRge1, rgMyRge2; COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); int CurrentRow; if (!app.CreateDispatch(_T("Excel.Application"),NULL)) { AfxMessageBox(_T("Create Excel false!")); return; } //遍历所有行 strFilename=g_strExePth+_T("Calibration Sensor Report.xls"); books.AttachDispatch(app.GetWorkbooks()); book.AttachDispatch(books.Add(_variant_t(strFilename))); sheets.AttachDispatch(book.GetWorksheets()); sheet.AttachDispatch(sheets.GetItem(_variant_t("Sheet1"))); range.AttachDispatch(sheet.GetCells()); CString ContentData; CTime time = CTime::GetCurrentTime(); ContentData.Format(_T("Calibration Date")); range.SetItem(_variant_t((long)(1)),_variant_t((long)(1)),_variant_t(ContentData)); ContentData.Format(_T("%d-%d-%d"),time.GetYear(),time.GetMonth(),time.GetDay()); range.SetItem(_variant_t((long)(1)),_variant_t((long)(3)),_variant_t(ContentData)); range.AttachDispatch(sheet.GetCells()); for (int i=0;i<(int)m_modbus_IDs.size();i++) { if (!m_modbus_IDs.at(i).Result) { continue; } CurrentRow = 3 + 7*i; unsigned short databuffer[30]; Read_Multi(m_modbus_IDs.at(i).ModbusID,&databuffer[0],2000,30); ContentData = _T("Serial Number"); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(1)),_variant_t(ContentData)); ContentData.Format(_T("%d"),databuffer[2027 - 2000]); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(2)),_variant_t(ContentData)); ContentData = _T("Points"); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(3)),_variant_t(ContentData)); ContentData.Format(_T("%d"),databuffer[2003 - 2000]); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(4)),_variant_t(ContentData)); ++CurrentRow; ContentData = _T("Temperature"); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(1)),_variant_t(ContentData)); ContentData.Format(_T("%0.1f"),((float)((short)databuffer[2000 - 2000]))/10); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(2)),_variant_t(ContentData)); ContentData = _T("Humidity"); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(3)),_variant_t(ContentData)); ContentData.Format(_T("%0.1f"),((float)((short)databuffer[2001 - 2000]))/10); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(4)),_variant_t(ContentData)); ++CurrentRow; ContentData = _T("RH"); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(1)),_variant_t(ContentData)); for (int j = 0;j<10;j++) { ContentData.Format(_T("%0.1f"),((float)((short)databuffer[2004 - 2000 + 2*j]))/10); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(2+j)),_variant_t(ContentData)); } ++CurrentRow; ContentData = _T("Frequency"); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(1)),_variant_t(ContentData)); for (int j = 0;j<10;j++) { ContentData.Format(_T("%d"),databuffer[2005 - 2000 + 2*j]); range.SetItem(_variant_t((long)(CurrentRow)),_variant_t((long)(2+j)),_variant_t(ContentData)); } } AfxMessageBox(_T("Export Completely")); app.SetVisible(true); range.ReleaseDispatch(); sheet.ReleaseDispatch(); sheets.ReleaseDispatch(); book.ReleaseDispatch(); books.ReleaseDispatch(); app.ReleaseDispatch(); }