三图胜千言:
就是酱紫:
//打印领料表 void CKnifeDlgDlg::PrintCurUsedTabel(int order) { // TODO: Add your command handler code here char szFilters[] = "hgy Files (*.xls)|*.xls|All Files (*.*)|*.*||"; CFileDialog fileDlg (FALSE, "xls", ReadName + ".xls", OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, NULL); fileDlg.m_ofn.lpstrTitle = "保存"; if( fileDlg.DoModal() == IDOK ) { CString pathName = fileDlg.GetPathName(); std::vector<std::map<std::string,std::string>> resultArr = YHdialog::GetOpResultListTool(_knifeclasses); std::vector<std::string> title; title.push_back("用料材料"); title.push_back("厚度"); title.push_back("数量"); title.push_back("面积"); std::vector<std::string> dataCol; dataCol.push_back("PanelMatrial"); dataCol.push_back("Thickness"); dataCol.push_back("PanelNum"); dataCol.push_back("Allsize"); if(order == 0) { //反序 std::reverse(resultArr.begin(),resultArr.end()); } OnWriteDataExcel(pathName, title, dataCol, resultArr); MessageBox("保存完成"); } } //写EXCEL void CKnifeDlgDlg::OnWriteDataExcel(CString savePath, std::vector<std::string> title,std::vector<std::string> dataMap, std::vector<std::map<std::string,std::string>> dataSrc) { CString strFile = savePath; COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); CApplication app; CWorkbook book; CWorkbooks books; CWorksheet sheet; CWorksheets sheets; CRange range; CFont font; if (!app.CreateDispatch(_T("Excel.Application"))) { MessageBox(_T("Error!Creat Excel Application Server Faile!")); } books = app.get_Workbooks(); book = books.Add(covOptional); sheets = book.get_Worksheets(); sheet = sheets.get_Item(COleVariant((short)1)); //得到全部Cells range.AttachDispatch(sheet.get_Cells()); for( long title_col = 0 ; title_col < title.size() ; title_col++ ) { range.put_Item( _variant_t((long)1), //行 _variant_t((long)title_col+1), //列 _variant_t(title[title_col].c_str())); //数据 } //下面是向第二行的前十个单元格中输入1到10,十个数字 for( long data_row = 0 ; data_row < dataSrc.size() ; data_row++ ) { for( long data_col = 0 ; data_col < title.size() ; data_col++ ) { long row = data_row+2; long col = data_col+1; CString info; info.Format("%s",dataSrc[data_row][dataMap[data_col]].c_str()); range.put_Item( _variant_t(row), //行 _variant_t(col), //列 _variant_t(info)); //数据 } } //保存 book.SaveCopyAs(COleVariant(strFile)); book.put_Saved(true); app.put_Visible(false); //释放对象 range.ReleaseDispatch(); sheet.ReleaseDispatch(); sheets.ReleaseDispatch(); book.ReleaseDispatch(); books.ReleaseDispatch(); app.ReleaseDispatch(); //结尾,释放 book.ReleaseDispatch(); books.ReleaseDispatch(); app.ReleaseDispatch(); app.Quit(); }