void Cwhu_FaxSettingDlg::Lin_ImportAutoForArr(CString m_FilePath,CStringArray &m_AutoForward) { int Number = 0; //m_DutyArr数组必须足够大// //导入 CApplication app; CWorkbook book; CWorkbooks books; CWorksheet sheet; CWorksheets sheets; CRange range; LPDISPATCH lpDisp; //定义变量// COleVariant covOptional((long) DISP_E_PARAMNOTFOUND,VT_ERROR); if (!app.CreateDispatch(_T("Excel.Application"))) { this->MessageBox(_T("无法创建Excel应用")); } books = app.get_Workbooks(); //打开Excel,其中m_FilePath为Excel表的路径名// lpDisp = books.Open(m_FilePath,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional); book.AttachDispatch(lpDisp); sheets = book.get_Worksheets(); sheet = sheets.get_Item(COleVariant((short)1)); CStringArray m_ContentArr; for (int ItemNum = 2;ItemNum<40;ItemNum++) { for (int ColumNum=1;ColumNum<11;ColumNum++) { CString m_pos = Lin_GetEnglishCharacter(ColumNum); CString m_Itempos; m_Itempos.Format(_T("%d"),ItemNum); CString m_str = m_pos+m_Itempos; range = sheet.get_Range(COleVariant(m_str),COleVariant(m_str)); //获得单元格的内容 COleVariant rValue; rValue = COleVariant(range.get_Value2()); //转换成宽字符// rValue.ChangeType(VT_BSTR); //转换格式,并输出// CString m_content = CString(rValue.bstrVal); if (m_content!=_T("")) { m_AutoForward.Add(m_content); } } } book.put_Saved(TRUE); app.Quit(); }
/*! @brief このメソッドは WM_INITDIALOG のメッセージに応答して呼び出されます。 */ BOOL CInspectUsefulToolsDlg::OnInitDialog() { CDialog::OnInitDialog(); // このダイアログのアイコンを設定します。アプリケーションのメイン ウィンドウがダイアログでない場合、 // Framework は、この設定を自動的に行います。 SetIcon(m_hIcon, TRUE); // 大きいアイコンの設定 //SetIcon(m_hIcon, FALSE); // 小さいアイコンの設定 // メニューの初期化 InitializeMenuTree(); InitializeContents(); // タイトルの設定 CString strAppName = CString((LPCTSTR)IDS_APP_NAME); this->SetWindowText(strAppName); CLSID clsid; if (CLSIDFromProgID(OLESTR("Excel.Application"), &clsid) == NOERROR){ // Excel がインストールされている LPUNKNOWN lpUnk; HRESULT hr; LPDISPATCH lpDispatch; BOOL bOpenExcel = FALSE; if (GetActiveObject(clsid, NULL, &lpUnk) == NOERROR){ hr = lpUnk->QueryInterface(IID_IDispatch, (LPVOID*)&lpDispatch); lpUnk->Release(); if (hr == NOERROR) { //すでにExcelが起動されている状態であればAttachDispatch m_inXLApp.AttachDispatch(lpDispatch,TRUE); bOpenExcel = TRUE; } } if (m_inXLApp.m_lpDispatch == NULL) { bOpenExcel = m_inXLApp.CreateDispatch(clsid); m_bOpenXL = TRUE; } if (bOpenExcel == TRUE) { // Excelファイルの初期化 CWorkbooks inWorkbooks = m_inXLApp.get_Workbooks(); COleVariant varNull; varNull.ChangeType(VT_NULL); HRSRC hrSrc = FindResource(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_MACRO_EXCEL_RCDATA), RT_RCDATA); if (hrSrc != NULL) { HGLOBAL hGlobal = LoadResource(AfxGetApp()->m_hInstance, hrSrc); if (hGlobal != NULL) { char *rcData = (char *)LockResource(hGlobal); /* リソースサイズの取得 */ DWORD dwSize = SizeofResource(AfxGetApp()->m_hInstance, hrSrc); TCHAR szFilePath[MAX_PATH]; GetTempPath(MAX_PATH, szFilePath); TCHAR szFileName[MAX_PATH]; GetTempFileName(szFilePath, _T("SubTools_"), 0, szFileName); CFile inFile; inFile.Open(szFileName, CFile::modeWrite); inFile.Write(rcData, dwSize); inFile.Close(); strTempFilePath = szFileName; m_inXLBook = inWorkbooks.Open(szFileName , varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull,varNull, varNull); m_bOpenBook = TRUE; } } } } return TRUE; }
void COpenExcelDlg::Lin_InportExcelToList(CString m_FilePath,CListCtrl &m_List) { //先删除列表内容// m_List.DeleteAllItems(); while(m_List.GetHeaderCtrl()->GetItemCount()>0) { m_List.DeleteColumn(0); } //导入 CApplication app; CWorkbook book; CWorkbooks books; CWorksheet sheet; CWorksheets sheets; CRange range; LPDISPATCH lpDisp; //定义变量// COleVariant covOptional((long) DISP_E_PARAMNOTFOUND,VT_ERROR); if (!app.CreateDispatch(_T("Excel.Application"))) { this->MessageBox(_T("无法创建Excel应用")); return; } books = app.get_Workbooks(); //打开Excel,其中m_FilePath为Excel表的路径名// lpDisp = books.Open(m_FilePath,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional); book.AttachDispatch(lpDisp); sheets = book.get_Worksheets(); sheet = sheets.get_Item(COleVariant((short)1)); CStringArray m_HeadName; for (int i=1;i<26;i++) { CString m_pos = Lin_GetEnglishCharacter(i); m_pos = m_pos + _T("1"); range = sheet.get_Range(COleVariant(m_pos),COleVariant(m_pos)); //获得单元格的内容 COleVariant rValue; rValue = COleVariant(range.get_Value2()); //转换成宽字符// rValue.ChangeType(VT_BSTR); //转换格式,并输出// CString m_content = CString(rValue.bstrVal); if (m_content!=_T("")) { m_HeadName.Add(m_content); } } Lin_InitList(m_List,m_HeadName); CStringArray m_ContentArr; for (int ItemNum = 0;ItemNum<10000;ItemNum++) { for (int j=1;j<m_List.GetHeaderCtrl()->GetItemCount();j++) { CString m_pos = Lin_GetEnglishCharacter(j); CString m_Itempos; m_Itempos.Format(_T("%d"),ItemNum+2); CString m_str = m_pos+m_Itempos; range = sheet.get_Range(COleVariant(m_str),COleVariant(m_str)); //获得单元格的内容 COleVariant rValue; rValue = COleVariant(range.get_Value2()); //转换成宽字符// rValue.ChangeType(VT_BSTR); //转换格式,并输出// CString m_content = CString(rValue.bstrVal); m_ContentArr.Add(m_content); } if (m_ContentArr.GetAt(0)!=_T("")) { Lin_InsertList(m_List,m_ContentArr); m_ContentArr.RemoveAll(); } else{ break; } } book.put_Saved(TRUE); app.Quit(); }