1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
BOOL CGeneralZonePage::WriteZoneSettings()
{
if(m_Jan == 0 && m_Fev == 0 && m_Mar == 0 && m_Avr == 0 && m_Mai == 0 && m_Jun == 0 &&
m_Jul == 0 && m_Aou == 0 && m_Sep == 0 && m_Oct == 0 && m_Nov == 0 && m_Dec == 0)// les mois chauds
{
CString strMsg = "Vous n'avez indiquer aucun mois chaud pour cette zone.\n";
strMsg += "Voulez-vous continuer?";
int nRet = AfxMessageBox(strMsg, MB_YESNO);
if(nRet == IDNO)
return FALSE;
}
MCN::Automation::McnExcel::CMcnExcelApp spExcelApp;
try
{
HRESULT hr = spExcelApp.CreateInstance(EXCEL_APPLICATION);
if (FAILED(hr))
{
AfxMessageBox("Echec de chargement de Microsoft Excel", MB_ICONSTOP);
return hr;
}
CString strSettingsFile;
hr = HydroBil::API::AfxGetSafeSettingsFile(m_strZoneName, strSettingsFile);
if(hr != S_OK)
{
CString strMsg = "Impossible d'obtenir les paramètres hydrologiques de ";
strMsg += m_strZoneName;
AfxMessageBox(strMsg, MB_ICONSTOP);
return FALSE;
}
MCN::Automation::McnExcel::CMcnWorkBook spSettingsBook = spExcelApp.OpenWorkBook(strSettingsFile);
if(spSettingsBook == NULL)
{
CString strMsg = "Impossible d'obtenir les paramètres hydrologiques de ";
strMsg += m_strZoneName;
AfxMessageBox(strMsg, MB_ICONSTOP);
return FALSE;
}
MCN::Automation::McnExcel::CMcnWorkSheet spCoefficientsSheet = spSettingsBook.OpenWorkSheet("Coefficients");
if(spCoefficientsSheet == NULL)
{
spSettingsBook.Close();
CString strMsg = "Le classeur \"";
strMsg += m_strZoneName;
strMsg += "_Settings.xls\" est corrompu.\nLa feuille \"Coefficients\" est absente ou non définie";
AfxMessageBox(strMsg, MB_ICONSTOP);
return FALSE;
}
MCN::Automation::McnExcel::CMcnWorkSheet spZoneSheet = spSettingsBook.OpenWorkSheet("Zone");
if(spZoneSheet == NULL)
{
spSettingsBook.Close();
CString strMsg = "Le classeur \"";
strMsg += m_strZoneName;
strMsg += "_Settings.xls\" est corrompu.\nLa feuille \"Zone\" est absente ou non définie";
AfxMessageBox(strMsg, MB_ICONSTOP);
return FALSE;
}
MCN::Automation::McnExcel::CMcnRange spZoneRange = spZoneSheet.GetRange("F6", "F17");
spZoneRange.PutItem(1, 1, m_Jan);
spZoneRange.PutItem(2, 1, m_Fev);
spZoneRange.PutItem(3, 1, m_Mar);
spZoneRange.PutItem(4, 1, m_Avr);
spZoneRange.PutItem(5, 1, m_Mai);
spZoneRange.PutItem(6, 1, m_Jun);
spZoneRange.PutItem(7, 1, m_Jul);
spZoneRange.PutItem(8, 1, m_Aou);
spZoneRange.PutItem(9, 1, m_Sep);
spZoneRange.PutItem(10, 1, m_Oct);
spZoneRange.PutItem(11, 1, m_Nov);
spZoneRange.PutItem(12, 1, m_Dec);
// latitude
int n = m_LatitudeCB.GetCurSel();
if(n == -1)
{
spSettingsBook.Close();
return FALSE;
}
CString str;
m_LatitudeCB.GetLBText(n, str);
spZoneRange = spZoneSheet.GetRange("B6", "C6");
spZoneRange.PutItem(1, 1, str);
// latitude nord ou sud
if(m_geoSettings.m_nLatitudeType == HydroBil::SGeoSettings::NORTH)
spZoneRange.PutItem(1, 2, "N");
else
spZoneRange.PutItem(1, 2, "S");
// altitude
n = m_AltitudeCB.GetCurSel();
if(n == -1)
{
spSettingsBook.Close();
return FALSE;
}
m_AltitudeCB.GetLBText(n, str);
spZoneRange = spZoneSheet.GetRange("C8");
spZoneRange.PutValue(str);
// zone climatique
n = m_ClimatZoneCB.GetCurSel();
if(n == -1)
{
spSettingsBook.Close();
return FALSE;
}
m_ClimatZoneCB.GetLBText(n, str);
MCN::Automation::McnExcel::CMcnRange spCoeffRange = spCoefficientsSheet.GetRange("B5", "H7");
long nRowFound = -1;
long nColFound = -1;
spCoeffRange.GetLeftColumn().FindData(str, nRowFound, nColFound);
if(nRowFound == -1)
{
spSettingsBook.Close();
return FALSE;
}
double a = spCoeffRange.GetItem(nRowFound, 2);
double b = spCoeffRange.GetItem(nRowFound, 3);
double a1 = spCoeffRange.GetItem(nRowFound, 4);
double b1 = spCoeffRange.GetItem(nRowFound, 5);
double a2 = spCoeffRange.GetItem(nRowFound, 6);
double b2 = spCoeffRange.GetItem(nRowFound, 7);
spZoneRange = spZoneSheet.GetRange("H6", "N6");
spZoneRange.PutItem(1, 1, str);
spZoneRange.PutItem(1, 2, a);
spZoneRange.PutItem(1, 3, b);
spZoneRange.PutItem(1, 4, a1);
spZoneRange.PutItem(1, 5, b1);
spZoneRange.PutItem(1, 6, a2);
spZoneRange.PutItem(1, 7, b2);
spZoneRange.GetLeftColumn().AutoFitColumns();
spExcelApp.CloseAllUserExcelApp();
spSettingsBook.Save();
spSettingsBook.Close(true);
UpdateData(FALSE);
return TRUE;
}
catch(...)
{
spExcelApp.Close();
spExcelApp.Release();
AfxMessageBox(IDS_E_EXCEL_APPLICATION, MB_ICONSTOP);
return FALSE;
}
} |
Partager