void COpenExcelDlg::Lin_ExportListToExcel(CListCtrl &m_List) { if (m_List.GetHeaderCtrl()->GetItemCount()>0) { CApplication app; CWorkbooks books; CWorkbook book; CWorksheets sheets; CWorksheet sheet; CRange range; CRange cols; int i = 3; CString str1, str2; COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); if( !app.CreateDispatch(_T("Excel.Application"))) { MessageBox(_T("无法创建Excel应用!")); return; } books=app.get_Workbooks(); book=books.Add(covOptional); sheets=book.get_Sheets(); sheet=sheets.get_Item(COleVariant((short)1)); //写入表头// for (int ColumNum=1;ColumNum<m_List.GetHeaderCtrl()->GetItemCount();ColumNum++) { str2 = Lin_GetEnglishCharacter(ColumNum); str2 = str2 + _T("1"); LVCOLUMN lvColumn; TCHAR strChar[256]; lvColumn.pszText=strChar; lvColumn.cchTextMax=256 ; lvColumn.mask = LVCF_TEXT; m_List.GetColumn(ColumNum,&lvColumn); CString str=lvColumn.pszText; range=sheet.get_Range(COleVariant(str2),COleVariant(str2)); range.put_Value2(COleVariant(str)); } //获取单元格的位置// for (int ColumNum =1;ColumNum<m_List.GetHeaderCtrl()->GetItemCount();ColumNum++) { for (int ItemNum = 0;ItemNum<m_List.GetItemCount();ItemNum++) { str2 = Lin_GetEnglishCharacter(ColumNum); str1.Format(_T("%d"),ItemNum+2); str2 = str2+str1; CString str = m_List.GetItemText(ItemNum,ColumNum); range=sheet.get_Range(COleVariant(str2),COleVariant(str2)); range.put_Value2(COleVariant(str)); } } cols=range.get_EntireColumn(); cols.AutoFit(); app.put_Visible(TRUE); app.put_UserControl(TRUE); }else{ AfxMessageBox(_T("列表为空")); } }
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 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 Cwhu_FaxSettingDlg::Lin_ExportForArrToExcel(CStringArray &m_AutoForward,CString m_FilePath) { CApplication app; CWorkbooks books; CWorkbook book; CWorksheets sheets; CWorksheet sheet; CRange range; CRange cols; CString str1, str2; int m_ArrPos = 0; int m_ArrCount = m_AutoForward.GetSize(); COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); if( !app.CreateDispatch(_T("Excel.Application"))) { MessageBox(_T("无法创建Excel应用!")); return; } books=app.get_Workbooks(); book=books.Add(covOptional); sheets=book.get_Sheets(); sheet=sheets.get_Item(COleVariant((short)1)); //写入表头// str2 = _T("A1"); CString str = _T("源号码"); range=sheet.get_Range(COleVariant(str2),COleVariant(str2)); range.put_Value2(COleVariant(str)); str2 = _T("B1"); str = _T("转发号码个数"); range=sheet.get_Range(COleVariant(str2),COleVariant(str2)); range.put_Value2(COleVariant(str)); for (int j=3;j<11;j++) { str2 = Lin_GetEnglishCharacter(j); str2 = str2 +_T("1"); str= _T("转发号码"); range=sheet.get_Range(COleVariant(str2),COleVariant(str2)); range.put_Value2(COleVariant(str)); } CStringArray m_LineStart; for (int i=0;i<m_ArrCount;i++) { CString m_str= m_AutoForward.GetAt(i); int CharCount = m_str.GetLength(); if (CharCount<4) //如果是号码个数// { m_str.Format(_T("%d"),i-1); m_LineStart.Add(m_str); } } int ItemNum = 2; int ColumNum = 1; for (int i=0;i<m_LineStart.GetSize();i++) { int Pos = _ttoi(m_LineStart.GetAt(i)); CString m_str = m_AutoForward.GetAt(Pos+1); int Number = _ttoi(m_str); for (int j=0;j<Number+2;j++) { str2 = Lin_GetEnglishCharacter(ColumNum); str1.Format(_T("%d"),ItemNum); str2 = str2 +str1; CString content = _T("'")+m_AutoForward.GetAt(Pos+j); range=sheet.get_Range(COleVariant(str2),COleVariant(str2)); range.put_Value2(COleVariant(content)); ColumNum++; if (ColumNum>10) { ColumNum = 3; ItemNum++; } } ItemNum++; ColumNum = 1; } cols=range.get_EntireColumn(); cols.AutoFit(); //app.put_Visible(TRUE); //app.put_UserControl(TRUE); book.SaveAs(COleVariant(m_FilePath),covOptional,covOptional,covOptional,covOptional,covOptional,0,covOptional,covOptional,covOptional,covOptional,covOptional); app.Quit(); }