//VT_R8 int IllusionExcelFile::GetCellInt(long irow, long icolumn) { int num; COleVariant vresult; if (already_preload_ == FALSE) { CRange range; range.AttachDispatch(excel_current_range_.get_Item(COleVariant((long)irow), COleVariant((long)icolumn)).pdispVal, true); vresult = range.get_Value2(); range.ReleaseDispatch(); } else { long read_address[2]; VARIANT val; read_address[0] = irow; read_address[1] = icolumn; ole_safe_array_.GetElement(read_address, &val); vresult = val; } // num = static_cast<int>(vresult.dblVal); return num; }
double IllusionExcelFile::GetCellDouble(long irow, long icolumn) { double rtn_value = 0; COleVariant vresult; //字符串 if (already_preload_ == FALSE) { CRange range; range.AttachDispatch(excel_current_range_.get_Item(COleVariant((long)irow), COleVariant((long)icolumn)).pdispVal, true); vresult = range.get_Value2(); range.ReleaseDispatch(); } //如果数据依据预先加载了 else { long read_address[2]; VARIANT val; read_address[0] = irow; read_address[1] = icolumn; ole_safe_array_.GetElement(read_address, &val); vresult = val; } if (vresult.vt == VT_R8) { rtn_value = vresult.dblVal; } return rtn_value; }
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(); }
CString Illusion_ExcelFile::get_cell_cstring(long iline, long icolumn) { COleVariant vResult ; CString str; //字符串 if (already_preload_ == FALSE) { CRange range; _variant_t v_row(iline); _variant_t v_column(icolumn); range.AttachDispatch(excel_current_range_.get_Item(v_row, v_column).pdispVal, true); vResult = range.get_Value2(); range.ReleaseDispatch(); } //如果数据依据预先加载了 else { long read_address[2]; VARIANT val; read_address[0] = iline; read_address[1] = icolumn; ole_safe_array_.GetElement(read_address, &val); vResult = val; } if (vResult.vt == VT_BSTR) { str = vResult.bstrVal; } //整数 else if (vResult.vt == VT_INT) { str.Format(_T("%d"), vResult.pintVal); } //8字节的数字 else if (vResult.vt == VT_R8) { str.Format(_T("%.6f"), vResult.dblVal); } //时间格式 else if (vResult.vt == VT_DATE) { SYSTEMTIME st; VariantTimeToSystemTime(vResult.date, &st); CTime tm(st); str = tm.Format(_T("%Y-%m-%d")); } //单元格空的 else if (vResult.vt == VT_EMPTY) { str = ""; } return str; }
///检查一个CELL是否是时间 BOOL IllusionExcelFile::IsCellTime(long iRow, long iColumn) { CRange range; range.AttachDispatch(excel_current_range_.get_Item(COleVariant((long)iRow), COleVariant((long)iColumn)).pdispVal, true); COleVariant vResult = range.get_Value2(); if (vResult.vt == VT_DATE) { return TRUE; } range.ReleaseDispatch(); return FALSE; }
BOOL IllusionExcelFile::IsCellString(long irow, long icolumn) { CRange range; range.AttachDispatch(excel_current_range_.get_Item(COleVariant((long)irow), COleVariant((long)icolumn)).pdispVal, true); COleVariant vResult = range.get_Value2(); //VT_BSTR标示字符串 if (vResult.vt == VT_BSTR) { return TRUE; } return FALSE; }
//检查一个CELL是否是数值 BOOL CExcelFile::IsCellInt(long irow, long icolumn) { CRange range; range.AttachDispatch(excel_current_range_.get_Item (COleVariant((long)irow),COleVariant((long)icolumn)).pdispVal, true); COleVariant vResult =range.get_Value2(); //好像一般都是VT_R8 if(vResult.vt == VT_INT || vResult.vt == VT_R8) { return TRUE; } return FALSE; }
//检查一个CELL是否是数值 BOOL Illusion_ExcelFile::is_cell_number(long iline, long icolumn) { CRange range; _variant_t v_row((long)iline); _variant_t v_column((long)icolumn); range.AttachDispatch(excel_current_range_.get_Item(v_row, v_column).pdispVal, true); VARIANT vResult = range.get_Value2(); //好像一般都是VT_R8 if (vResult.vt == VT_INT || vResult.vt == VT_R8) { return TRUE; } return FALSE; }
//检查一个CELL是否是字符串 BOOL Illusion_ExcelFile::is_cell_string(long irow, long icolumn) { CRange range; _variant_t v_row((long)irow); _variant_t v_column((long)icolumn); range.AttachDispatch(excel_current_range_.get_Item(v_row, v_column).pdispVal, true); COleVariant vResult = range.get_Value2(); //VT_BSTR标示字符串 if (vResult.vt == VT_BSTR) { return TRUE; } return FALSE; }
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(); }