//--------------------------------------------------------------------------- void __fastcall TFormCreateDoc::ButtonCreateDocClick(TObject *Sender) { assert(m_plaintext.size() > 1); //Start at index one, because index zero is the copyright line std::random_shuffle(&m_plaintext[1],m_plaintext.end()); if (CheckBoxUseCode->Checked) { m_doctext = CreateDoc( m_plaintext, EditNquestions->Text.ToInt(), StrToUpper(EditCode->Text.c_str())); } else { m_doctext = CreateDoc(m_plaintext,EditNquestions->Text.ToInt()); } VectorToStringList(m_doctext,RichEdit2->Lines); ++PageControl1->ActivePageIndex; }
void OCreate::Create() { if (!File) return; if (!Location) Location << (PSZ) "<WP_DESKTOP>"; if (is(".INF")) CreateBook(); else if (is(".EXE") | is(".COM") | is(".BAT") | is(".CMD")) CreateExe(); else CreateDoc(); Title << (PSZ)NULL; File << (PSZ)NULL; }
HRESULT CZZExcel2Word::BuildDataFromExcelFile(std::wstring ExcelFile,std::wstring stringDocKey) { CExcelApplication ExcelApp; CExcelWorkbooks wbsMyBooks; CExcelWorkbook wbMyBook; CExcelWorksheets wssMysheets; CExcelWorksheet wsMysheet; //创建Excel 2000服务器(启动Excel) // IID clsid; // HRESULT hr = IIDFromString(_T("Excel.Application"), &clsid); if (!ExcelApp.CreateDispatch(_T("Excel.Application"),NULL)) { AfxMessageBox(_T("创建Excel服务失败!")); exit(1); } //利用模板文件建立新文档 wbsMyBooks = ExcelApp.get_Workbooks(); COleVariant avar((long)DISP_E_PARAMNOTFOUND,VT_ERROR); wbMyBook = wbsMyBooks.Open(ExcelFile.c_str(),avar,avar,avar,avar,avar,avar,avar,avar,avar,avar,avar,avar,avar,avar); //得到Worksheets wssMysheets = wbMyBook.get_Sheets(); COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); CExcelRange useRange; for (int iSheetIdx = 1; iSheetIdx <= wssMysheets.get_Count() ; iSheetIdx++) { //得到sheet wsMysheet=wssMysheets.get_Item(COleVariant((short)iSheetIdx)); #ifdef _DEBUG CString sheetname = wsMysheet.get_Name(); #endif // _DEBUG //首先分析表的第一行,得到数据关键字 int iKeyColumnIdx = -1; CExcelRange useRange = wsMysheet.get_UsedRange(); long iRowNum = useRange.get_Count(); useRange = useRange.get_Columns(); long iStartRow = useRange.get_Row(); int nColumn = useRange.get_Count(); long iStartCol = useRange.get_Column(); long iValueNameRow = m_iValueNameRow; //find keyName Column index for (int iColIdx = iStartCol; iColIdx <= nColumn ; iColIdx++) { useRange = wsMysheet.get_Cells(); COleVariant keyValue=useRange.get_Item(_variant_t(iValueNameRow),_variant_t(iColIdx)); useRange = keyValue.pdispVal; std::wstring itemString = GetStringFromExcelCell(useRange); if (stringDocKey == itemString ) { iKeyColumnIdx = iColIdx; break; } } if (iKeyColumnIdx < 0) { continue;//no valid data with doc key string } //根据关键字列创建doc for (int iRodIdx = 3; iRodIdx <= iRowNum ; iRodIdx++) { useRange = wsMysheet.get_Cells(); COleVariant keyValue=useRange.get_Item(_variant_t(iRodIdx),_variant_t(iKeyColumnIdx)); useRange = keyValue.pdispVal; std::wstring keyitemString = GetStringFromExcelCell(useRange); if (keyitemString.empty()) { continue; } PZZWordDoc pDoc = GetDocFromKeyString(keyitemString); if (pDoc == NULL) { pDoc = CreateDoc(keyitemString); if (pDoc == NULL) { continue; } SetExportReportSetting(pDoc); m_vecWordDoc.push_back(pDoc); } //逐个读取相关doc的数据 for (int iColIdx = iStartCol; iColIdx <= nColumn ; iColIdx++) { if (iColIdx == iKeyColumnIdx) { continue; } useRange = wsMysheet.get_Cells(); keyValue=useRange.get_Item(_variant_t(iRodIdx),_variant_t(iColIdx)); useRange = keyValue.pdispVal; std::wstring valueitemString = GetStringFromExcelCell(useRange); if (valueitemString.empty()) { //无效数据 continue; } //查找这个值对应的名字 useRange = wsMysheet.get_Cells(); keyValue=useRange.get_Item(_variant_t(iValueNameRow),_variant_t(iColIdx)); useRange = keyValue.pdispVal; std::wstring valuenameitemString = GetStringFromExcelCell(useRange); if (valuenameitemString.empty()) { //无效数据名字 continue; } pDoc->AddDataItem(valuenameitemString,valueitemString); } } if (m_ioutputOnlyoneFile ==1) { break; } wsMysheet.ReleaseDispatch(); }//sheet scan wssMysheets.ReleaseDispatch(); wbsMyBooks.Close(); wbMyBook.ReleaseDispatch(); wbsMyBooks.ReleaseDispatch(); ExcelApp.Quit(); ExcelApp.ReleaseDispatch(); return S_OK; }
// /// Creates a document based on the directory path and the specified template. The /// parameter flags, one of the document template constants, determines how the /// document template is created. If path is 0 and this is not a new document (the /// flag dtNewDoc is not set), it displays a dialog box. If path is 0 and dtNewDoc /// is not set and more than one template exists, CreateAnyDoc displays a dialog box /// and a list of templates. // TDocument* TDocManager::CreateAnyDoc(LPCTSTR path, long flags) { // Are we creating a new document? // bool newDoc = (flags & dtNewDoc) ? true : false; // Get a count and list of templates // int tplcount = GetNewTemplates(0, 0, newDoc); // Return of there are no usable templates // if (!tplcount) { TRACEX(OwlDocView, 0, _T("No usable templates found in CreateAnyDoc()")); return 0; } // Allocate memory and get templates // TDocTemplatePtr* tpllist = new TDocTemplatePtr[tplcount]; TAPointer<TDocTemplatePtr> _clnObj(tpllist); GetNewTemplates(tpllist, tplcount, newDoc); tchar filepath[_MAX_PATH]; filepath[0] = 0; // Find the index of the template to be used // int index; if (newDoc) { // If there's only one template, use it - otherwise prompt user // to select the document type // if (tplcount == 1) index = 1; else index = SelectDocType(tpllist, tplcount); } else { // Select the doctemplate using the doc template filters // !CQ might be nice to skip UI if path is already known, and maybe only // !CQ if there is just one template // if (path) ::_tcscpy(filepath, path); else filepath[0] = 0; index = SelectDocPath(tpllist, tplcount, filepath, COUNTOF(filepath), flags); WARNX(OwlDocView, index > tplcount, 0, _T("Invalid template index from SelectDocPath")); } // No index implies user cancelled or error // if (!index) return 0; // If opening a document, check that document is not already opened // if (filepath[0]) { TDocument* doc = FindDocument(filepath); if (doc != 0) { PostDocError(*doc, IDS_DUPLICATEDOC); return 0; } } // We have a template, now create the document & return it if successful // return CreateDoc(tpllist[index-1], filepath, 0, flags); }