Example #1
0
void __fastcall TForm1::actSortClassificationsExecute(TObject *Sender)
{
	if(!MakeBackup()) {
		Log->Lines->Add("Ошибка: Не удалось создать резервную копию файла");
		return;
	}

	Variant app = Variant::CreateObject("Excel.Application");
	app.OlePropertySet("Visible", true);
	Variant excel = app.OlePropertyGet("Workbooks").OleFunction("Open", WideString(m_strFileName.c_str()));
	Variant vSheets = excel.OlePropertyGet("Worksheets");

	Variant vSheet = vSheets.OlePropertyGet("Item",m_nPageClassification);
	UnicodeString strPageName = vSheet.OlePropertyGet("Name");
	if (strPageName.UpperCase() != UnicodeString("классификации").UpperCase()) {
	   app.OleProcedure("Quit");
	   Log->Lines->Add("Ошибка: Не верное имя страницы");
	   MessageBox (Handle, UnicodeString(L"Не верное имя страницы").c_str(), L"prompt", MB_OK);
	   return;
	};
	std::vector<exlClass> classes;
	ReadClassifications(vSheet, classes);
	WriteClassifications(vSheet, classes);


	Log->Lines->Add("Сохраняю файл...");
	try {
		app.OlePropertySet("DisplayAlerts",false);
		excel.OleProcedure("SaveAs", WideString(m_strFileName.c_str()));
		Log->Lines->Add("Классификации отсортированы!");
	} catch (...) {
		Log->Lines->Add("Ошибка: Пожалуйста закройте все открытые копии файла и повторите операцию");
	}
	app.OleProcedure("Quit");
}
Example #2
0
void __fastcall TForm1::actViewClassificationsExecute(TObject *Sender)
{
	Variant app = Variant::CreateObject("Excel.Application");
	Variant excel = app.OlePropertyGet("Workbooks").OleFunction("Open", WideString(m_strFileName.c_str()));
	Variant vSheets = excel.OlePropertyGet("Worksheets");
	Variant vSheet = vSheets.OlePropertyGet("Item",m_nPageClassification);
	UnicodeString strPageName = vSheet.OlePropertyGet("Name");
	if (strPageName.UpperCase() != UnicodeString("классификации").UpperCase()) {
	   app.OleProcedure("Quit");
	   MessageBox (Handle, UnicodeString(L"Не верное имя страницы").c_str(), L"prompt", MB_OK);
	   return;
	};

	std::vector<exlClass> classes;
	ReadClassifications(vSheet, classes);
	app.OleProcedure("Quit");

	/*RichEdit1->Lines->Clear();
	RichEdit1->Lines->Add("Уровень TreeView1->Selected->Level: "+IntToStr(TreeView1->Selected->Level));
	int n = TreeView1->Selected->AbsoluteIndex;
	RichEdit1->Lines->Add("Асболютный номер TreeView1->Selected->AbsoluteIndex: "+IntToStr(n));
	RichEdit1->Lines->Add("Текст из выбранного узла: "+TreeView1->Selected->Text);*/
	FormSelectClass->ComboBox1->Items->Clear();
	FormClassEditor->TreeView1->Items->Clear();
	std::map<UnicodeString, TTreeNode *> nodeclasses;
	for (unsigned int i = 0; i < classes.size(); i++) {
		if (nodeclasses.count(classes[i].Class) == 0) {
			TTreeNode *parentNode = FormClassEditor->TreeView1->Items->Add(NULL, classes[i].Class);
			nodeclasses[classes[i].Class] = parentNode;
			parentNode->ImageIndex = 0;
			parentNode->SelectedIndex = 0;
			FormSelectClass->ComboBox1->Items->Add(classes[i].Class);
		}
		TTreeNode *pParentNode = nodeclasses[classes[i].Class];
		TTreeNode *pChildNode = FormClassEditor->TreeView1->Items->AddChild(pParentNode, classes[i].Name);
		pChildNode->ImageIndex = 1;
		pChildNode->Data = new exlClass(classes[i]);
		pChildNode->SelectedIndex = 1;
	}


//	TTreeNode *Node1 = FormClassEditor->TreeView1->Items->Add(NULL, "Root");
//	Node1->ImageIndex = 0;
//	FormClassEditor->TreeView1->Items->AddChild(Node1, "Root1");



/*	int n = TreeView1->Selected->AbsoluteIndex;
	TTreeNode *Node1 = TreeView1->Items->Item[n];
	TreeView1->Items->AddChild(Node1,"ChildNode");*/
//	Node1->Selected=true;
    ProgressBar1->Position = 0;
	FormClassEditor->ShowModal();
}
//---------------------------------------------------------------------------
void OfficeSaveAsDoc(AnsiString Office, Variant Document, AnsiString FileName)
{
 //сохранить документ как
  try
    {
     if (Office=="OO")
      {
        Variant SaveParam;
        int Bounds[2] = {0,0};
        Variant ServiceManager = Variant::CreateObject("com.sun.star.ServiceManager");
        Variant MyStruct = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
        MyStruct.OlePropertySet("name","FilterName");
        MyStruct.OlePropertySet("value","MS Word 97");
        SaveParam = VarArrayCreate(Bounds, 1, varVariant);
        SaveParam.PutElement(MyStruct, 0);
        Document.OleFunction ("storeAsURL", convertToURL(FileName), SaveParam);
        ServiceManager = Unassigned;
      }
     if (Office=="MSO")
      {
        Document.OleProcedure("SaveAs", FileName);
      }
    }
  catch (Exception &E)
    {
     ShowMessage("Ошибка при сохранении документа как " + E.Message);
    }
}
//---------------------------------------------------------------------------
void OfficeSaveDoc(AnsiString Office, Variant Document)
{
// сохранить документ
  try
    {
     if (Office=="OO")
      {
       Document.OleFunction("store");
      }
     if (Office=="MSO")
      {
       Document.OleProcedure("Save" );
      }
    }
  catch (Exception &E)
    {
     ShowMessage("Ошибка при сохранении документа " + E.Message);
    }
}
//---------------------------------------------------------------------------
void OfficeQuit(AnsiString Office, Variant app, Variant Document)
{
 // закрыть документ
 try
    {
     if (Office=="OO")
      {
        Document.OleFunction("close", true);                            //закрытие документа
        Variant Desktop;                                                //закрытие офиса
        Desktop = app.OleFunction("createInstance", "com.sun.star.frame.Desktop");
        Desktop.OleFunction("terminate");                               //не закроет QuickStarter
        Desktop = Unassigned;
      }
     if (Office=="MSO")
      {
        app.OleProcedure("Quit");
      }
    }
  catch (Exception &E)
    {
     ShowMessage("Ошибка при закрытии Офиса " + E.Message);
    }
}
Example #6
0
void __fastcall TForm1::actRedesignClassificationsExecute(TObject *Sender)
{
	lblStatus->Caption = "";
	if(!MakeBackup()) {
		Log->Lines->Add("Не удалось создать резервную копию файла");
		lblStatus->Caption = "Ошибка: Не удалось создать резервную копию файла";
		return;
	}
	m_strRecomendations = "";

	lblStatus->Caption = "Открываю файл на чтение...";
	Variant app = Variant::CreateObject("Excel.Application");
    app.OlePropertySet("Visible", true);
	Variant excel = app.OlePropertyGet("Workbooks").OleFunction("Open", WideString(m_strFileName.c_str()));
	Variant vSheets = excel.OlePropertyGet("Worksheets");
	Variant vSheet = vSheets.OlePropertyGet("Item",m_nPageClassification);
	UnicodeString strPageName = vSheet.OlePropertyGet("Name");
	if (strPageName.UpperCase() != UnicodeString("классификации").UpperCase()) {
		app.OleProcedure("Quit");
		MessageBox (Handle, UnicodeString(L"Не верное имя страницы").c_str(), L"prompt", MB_OK);
		lblStatus->Caption = "Ошибка: Не верное имя страницы";
		return;
	};

	std::vector<exlClass> classes;
	ReadClassifications(vSheet, classes);

	std::vector<exlMonth> months;
	for (unsigned int i = 0; i < m_vMonth.size(); i++) {
		int nMonthPage = m_vMonth[i].Number;
		Variant vSheetMonth = vSheets.OlePropertyGet("Item",nMonthPage);
		ReadMonth(vSheetMonth, months);
	}
//	Log->Lines->Add("Обновляю линки." + IntToStr((int)months.size()));

	int nAll = classes.size() * months.size();
	Log->Lines->Add("Всего записей в месяцах: " + IntToStr((int)months.size()));
	std::vector<exlClass> newclasses;
	int nRemovedClasses = 0;
	ProgressBar1->Max = nAll;
	ProgressBar1->Min = 0;
	ProgressBar1->Position = 0;

	lblStatus->Caption = "Поиск наименований которые отсутвуют в месяцах...";
	Log->Lines->Add("Произвожу поиск наименований которые отсутвуют в месяцах...");
	for (unsigned int iC = 0; iC < classes.size(); iC++) {
		exlClass cl = classes[iC];
		cl.Monthes = "";
		UnicodeString name = classes[iC].Name.UpperCase();
		int nCount = 0;
		for (unsigned int iM = 0; iM < months.size(); iM++) {
			ProgressBar1->Position++;
			Application->ProcessMessages();
			UnicodeString name2 = months[iM].Name.UpperCase();
			if (name == name2) {
			   nCount++;
			   if (cl.Monthes.Pos(months[iM].Month) < 1) {
				  cl.Monthes += months[iM].Month + ";";
			   }
			}
		}
		if (nCount > 0) {
			newclasses.push_back(cl);
		} else {
			nRemovedClasses++;
			Log->Lines->Add("\tНаименование '" + cl.Name + "' - нигде не встречается и будет удалено из классификаций");
		}
	}
	Log->Lines->Add(" ** ");

	nAll = newclasses.size() * months.size();
	ProgressBar1->Max = nAll;
	ProgressBar1->Min = 0;
	ProgressBar1->Position = 0;
	int nAddClasses = 0;
	lblStatus->Caption = "Поиск наименований которые отсутвуют в списке классификаций...";
	Log->Lines->Add("Произвожу поиск наименований которые отсутвуют в списке классификаций...");
	for (unsigned int iM = 0; iM < months.size(); iM++) {
		UnicodeString name = months[iM].Name.UpperCase();
		int nCount = 0;
		for (unsigned int iC = 0; iC < newclasses.size(); iC++) {
			ProgressBar1->Position++;
			Application->ProcessMessages();
			UnicodeString name2 = newclasses[iC].Name.UpperCase();
			if (name == name2) {
			   nCount++;
			}
		}
		if (nCount == 0) {
			Log->Lines->Add("\tНаименование '" + months[iM].Name + "' - будет добавлено в классификации");
			exlClass cl;
			cl.Name = months[iM].Name;
			cl.Class = m_sUnknownClass;
			cl.Monthes += months[iM].Month + ";";
			newclasses.push_back(cl);
			nAddClasses++;

			nAll = newclasses.size() * months.size();
			ProgressBar1->Max = nAll;
		}
	}
	Log->Lines->Add("Будет удалено классификаций: " + IntToStr(nRemovedClasses));
    Log->Lines->Add("Будет добавлено классификаций: " + IntToStr(nAddClasses));
	Log->Lines->Add("Всего классификаций: " + IntToStr((int)(newclasses.size())));


	WriteClassifications(vSheet, newclasses);

	lblStatus->Caption = "Сохраняю файл...";
	Log->Lines->Add("Сохраняю файл...");
	try {
		app.OlePropertySet("DisplayAlerts",false);
		excel.OleProcedure("SaveAs", WideString(m_strFileName.c_str()));
		Log->Lines->Add("Данные сохранены!");
	} catch (...) {
		lblStatus->Caption = "Ошибка: Пожалуйста закройте все открытые копии файла и повторите операцию";
		Log->Lines->Add("Ошибка: Пожалуйста закройте все открытые копии файла и повторите операцию");
	}
    ProgressBar1->Position = 0;
	app.OleProcedure("Quit");
	lblStatus->Caption = "";
}
Example #7
0
void __fastcall TForm1::actCalcClassificationExecute(TObject *Sender)
{
	if(!MakeBackup()) {
		Log->Lines->Add("Не удалось создать резервную копию файла");
		return;
	}

	Variant app = Variant::CreateObject("Excel.Application");
	app.OlePropertySet("Visible", true);
	Variant excel = app.OlePropertyGet("Workbooks").OleFunction("Open", WideString(m_strFileName.c_str()));
	Variant vSheets = excel.OlePropertyGet("Worksheets");
	Variant vSheet = vSheets.OlePropertyGet("Item",m_nPageClassification);
	UnicodeString strPageName = vSheet.OlePropertyGet("Name");
	if (strPageName.UpperCase() != UnicodeString("классификации").UpperCase()) {
	   app.OleProcedure("Quit");
	   MessageBox (Handle, UnicodeString(L"Не верное имя страницы").c_str(), L"prompt", MB_OK);
	   return;
	};

	std::vector<exlClass> classes;
	ReadClassifications(vSheet, classes);

	std::vector<exlMonth> months;
	Variant vSheetMonth;
	double fSumSum = 0;
	for (unsigned int i = 0; i < m_vMonth.size(); i++) {
		if (cmbMonth->Text == m_vMonth[i].Name) {
			int nMonthPage = m_vMonth[i].Number;
			vSheetMonth = vSheets.OlePropertyGet("Item", nMonthPage);
			ReadMonth(vSheetMonth, months);
			ReadMonthSum(vSheetMonth, fSumSum);
		}
	}

	Log->Lines->Add("Произвожу расчет...");
    ProgressBar1->Max = months.size();
	ProgressBar1->Min = 0;
	double nSumHand = 0;

	std::vector<exlSumClass> vSumClasses;
	for (unsigned int i = 0; i < months.size(); i++) {
		ProgressBar1->Position = i;
		int nFound = 0;
		nSumHand += months[i].Price;
		for (unsigned int iC = 0; iC < classes.size(); iC++) {
			if (nFound == 0 && classes[iC].Name.UpperCase() == months[i].Name.UpperCase()) {
				months[i].Class = classes[iC].Class;
				months[i].LinkToClassification = createHyperLinkToClassification(classes, iC);
				nFound++;
			}
		}
		if (nFound == 0) {
			months[i].Class = m_sUnknownClass;
		}

		nFound = 0;
		for (unsigned int iC = 0; iC < vSumClasses.size(); iC++) {
			if (vSumClasses[iC].Name.UpperCase() == months[i].Class.UpperCase()) {
				vSumClasses[iC].Sum += months[i].Price;
				nFound++;
			}
		}
		if (nFound == 0) {
		   exlSumClass sm;
		   sm.Name = months[i].Class;
		   sm.Sum = months[i].Price;
		   vSumClasses.push_back(sm);
		}
	}

	/*for (unsigned int i = 0; i < vSumClasses.size(); i++) {
		Log->Lines->Add(vSumClasses[i].Name + " = " + FloatToStr(vSumClasses[i].Sum));
	}*/

	Log->Lines->Add("Готово.");

	Log->Lines->Add("Сортирую классификации...");
	ProgressBar1->Max = 100;
	ProgressBar1->Min = 0;

	{
		int nPermutation = 1;
		while (nPermutation > 0) {
			nPermutation = 0;
			for (unsigned int iC = 0; iC < vSumClasses.size()-1; iC++) {
				ProgressBar1->Position = (ProgressBar1->Position+1) % ProgressBar1->Max;
				Application->ProcessMessages();
				if (vSumClasses[iC].Name.UpperCase() > vSumClasses[iC+1].Name.UpperCase()) {
					exlSumClass buf = vSumClasses[iC];
					vSumClasses[iC] = vSumClasses[iC+1];
					vSumClasses[iC+1] = buf;
					nPermutation++;
				}
			}
		}
		ProgressBar1->Position = 0;
    }
	Log->Lines->Add("Готово.");

	Log->Lines->Add("Очистка старых данных");
	ProgressBar1->Max = 100;
	ProgressBar1->Min = 0;

	// clear sum classes, 11,12
	{
		bool b = true;
		int nRow = 1;
		while (b) {
			b = false;
			nRow++;
			ProgressBar1->Position = (ProgressBar1->Position+1) % ProgressBar1->Max;
			Application->ProcessMessages();

			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,11).OleProcedure("ClearFormats");
			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OleProcedure("ClearFormats");

			UnicodeString sValue11 = vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,11).OlePropertyGet("Value");
			UnicodeString sValue12 = vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OlePropertyGet("Value");

			clearCell(vSheetMonth, nRow, 11);
			clearCell(vSheetMonth, nRow, 12);

			if (sValue12.Trim().Length() > 0 || sValue11.Trim().Length() > 0) {
				b = true;
			}
		}
		ProgressBar1->Position = 0;
	}

	// clear 14,15,16,17
	{
		bool b = true;
		int nRow = 1;
		while (b) {
			b = false;
			nRow++;
			ProgressBar1->Position = (ProgressBar1->Position+1) % ProgressBar1->Max;
			Application->ProcessMessages();

			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,14).OleProcedure("ClearFormats");
			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,15).OleProcedure("ClearFormats");
			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,16).OleProcedure("ClearFormats");
			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,17).OleProcedure("ClearFormats");

			UnicodeString sValue14 = vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,14).OlePropertyGet("Value");
			UnicodeString sValue15 = vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,15).OlePropertyGet("Value");
			UnicodeString sValue16 = vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,16).OlePropertyGet("Value");
			UnicodeString sValue17 = vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,17).OlePropertyGet("Value");

			clearCell(vSheetMonth, nRow, 14);
			clearCell(vSheetMonth, nRow, 15);
			clearCell(vSheetMonth, nRow, 16);
			clearCell(vSheetMonth, nRow, 17);

			if (
				sValue14.Trim().Length() > 0
				|| sValue15.Trim().Length() > 0
				|| sValue16.Trim().Length() > 0
				|| sValue17.Trim().Length() > 0
			) {
				b = true;
			}
		}
		ProgressBar1->Position = 0;
	}


	Log->Lines->Add("Запись новых данных");
	{
		double nSum = 0;
		int nRow = 2;
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,11).OlePropertySet("Value", WideString("Класс"));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OlePropertySet("Value", WideString("Сумма"));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,11).OlePropertyGet("Font").OlePropertySet("Bold", true);
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OlePropertyGet("Font").OlePropertySet("Bold", true);
		vSheetMonth.OlePropertyGet("Columns", WideString("K")).OlePropertySet("ColumnWidth", 20);
		vSheetMonth.OlePropertyGet("Columns", WideString("L")).OlePropertySet("ColumnWidth", 15);
		setBorders(vSheetMonth, nRow, 11);
		setBorders(vSheetMonth, nRow, 12);
		setColor(vSheetMonth, nRow, 11, RGBToInt(240, 230, 140));
		setColor(vSheetMonth, nRow, 12, RGBToInt(240, 230, 140));

		ProgressBar1->Max = vSumClasses.size();
		ProgressBar1->Min = 0;
		for (unsigned int i = 0; i < vSumClasses.size(); i++) {
			nRow++;
			ProgressBar1->Position = i;
			nSum += vSumClasses[i].Sum;
			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,11).OlePropertySet("Value", WideString(vSumClasses[i].Name));
			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OlePropertySet("Value", WideString(vSumClasses[i].Sum));

			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OlePropertySet("NumberFormat", WideString(m_strNumberFormat));

			setBorders(vSheetMonth, nRow, 11);
			setBorders(vSheetMonth, nRow, 12);
		}
		nRow++;
		ProgressBar1->Position = 0;

		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,11).OlePropertySet("Value", WideString("Итого:"));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OlePropertySet("Value", WideString(nSum));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OlePropertySet("NumberFormat", WideString(m_strNumberFormat));

		nRow++;

		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,11).OlePropertySet("Value", WideString("Сумма сумм по дням:"));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OlePropertySet("Value", WideString(fSumSum));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,12).OlePropertySet("NumberFormat", WideString(m_strNumberFormat));
	}

	{
		double nSum = 0;
		int nRow = 2;
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,14).OlePropertySet("Value", WideString("Класс"));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,15).OlePropertySet("Value", WideString("Наименование"));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,16).OlePropertySet("Value", WideString("Цена"));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,17).OlePropertySet("Value", WideString("Ссылка"));

		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,14).OlePropertyGet("Font").OlePropertySet("Bold", true);
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,15).OlePropertyGet("Font").OlePropertySet("Bold", true);
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,16).OlePropertyGet("Font").OlePropertySet("Bold", true);
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,17).OlePropertyGet("Font").OlePropertySet("Bold", true);

		vSheetMonth.OlePropertyGet("Columns", WideString("N")).OlePropertySet("ColumnWidth", 20);
		vSheetMonth.OlePropertyGet("Columns", WideString("O")).OlePropertySet("ColumnWidth", 50);
		vSheetMonth.OlePropertyGet("Columns", WideString("P")).OlePropertySet("ColumnWidth", 15);
		vSheetMonth.OlePropertyGet("Columns", WideString("Q")).OlePropertySet("ColumnWidth", 50);

		setBorders(vSheetMonth, nRow, 14);
		setBorders(vSheetMonth, nRow, 15);
		setBorders(vSheetMonth, nRow, 16);
		setBorders(vSheetMonth, nRow, 17);

		setColor(vSheetMonth, nRow, 14, RGBToInt(240, 230, 140));
		setColor(vSheetMonth, nRow, 15, RGBToInt(240, 230, 140));
		setColor(vSheetMonth, nRow, 16, RGBToInt(240, 230, 140));
		setColor(vSheetMonth, nRow, 17, RGBToInt(240, 230, 140));

		ProgressBar1->Max = months.size();
		ProgressBar1->Min = 0;
		for (unsigned int i = 0; i < months.size(); i++) {
			nRow++;
			ProgressBar1->Position = i;
			nSum += months[i].Price;

			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,14).OlePropertySet("Value", WideString(months[i].Class));
			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,15).OlePropertySet("Value", WideString(months[i].Name));
			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,16).OlePropertySet("Value", WideString(months[i].Price));
			vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,16).OlePropertySet("NumberFormat", WideString(m_strNumberFormat));

			if (months[i].Price < 0) {
				setColor(vSheetMonth, nRow, 16, RGBToInt(240, 230, 140));
			}


			// TODO: check linkt to classification
			// vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,17).OlePropertySet("Value", WideString(months[i].LinkToClassification.c_str()));

			setBorders(vSheetMonth, nRow, 14);
			setBorders(vSheetMonth, nRow, 15);
			setBorders(vSheetMonth, nRow, 16);
			setBorders(vSheetMonth, nRow, 17);
		}
		nRow++;
		ProgressBar1->Position = 0;

		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,14).OlePropertySet("Value", WideString("Итого:"));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,16).OlePropertySet("Value", WideString(nSum));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,16).OlePropertySet("NumberFormat", WideString(m_strNumberFormat));
		vSheetMonth.OlePropertyGet("Cells").OlePropertyGet("Item",nRow,17).OlePropertySet("NumberFormat", WideString(m_strNumberFormat));
	}

	Log->Lines->Add("Сохраняю файл...");
	try {
		app.OlePropertySet("DisplayAlerts",false);
		excel.OleProcedure("SaveAs", WideString(m_strFileName.c_str()));
		Log->Lines->Add("Данные сохранены!");
	} catch (...) {
		Log->Lines->Add("Ошибка: Пожалуйста закройте все открытые копии файла и повторите операцию");
	}
    ProgressBar1->Position = 0;
	app.OleProcedure("Quit");
}
Example #8
0
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	edtFile->Text = "";
	m_strFileName = "";
	if (OpenDialog1->Execute()) {
		m_bBackup = false;

		UnicodeString strFileName = OpenDialog1->FileName;
		if (! FileExists (strFileName)) {
			MessageBox (Handle, UnicodeString(L"Файл '" + strFileName + L"' не существует").c_str(), L"prompt", MB_OK);
			edtFile->Text = "";
			return;
		}

		edtFile->Text = strFileName;
		// TODO: create backup now or later?
		// TODO: INIT OPERATIONS
		// TODO: scan month
		Variant var_Book,var_Sheet,var_Cell;

		Variant app = Variant::CreateObject("Excel.Application");
		// app.OlePropertySet("Visible",false);
		Variant excel;
		try {
			excel = app.OlePropertyGet("Workbooks").OleFunction("Open", WideString(strFileName.c_str()));
		} catch (...) {
			MessageBox (Handle, UnicodeString(L"Не получается открыть файл '" + strFileName + L"' как excel").c_str(), L"prompt", MB_OK);
			app.OleProcedure("Quit");
			edtFile->Text = "";
			return;
		}

		Log->Lines->Add("Файл загружен производиться анализ");
		Variant vSheets = excel.OlePropertyGet("Worksheets");

		m_nPageClassification = 0;
		m_vMonth.clear();
		cmbMonth->Items->Clear();
		cmbMonth->Items->Add("");
		int nSheets = vSheets.OlePropertyGet("Count");
		Log->Lines->Add("Всего листов: " + IntToStr(nSheets));
		for (int i = 0; i < nSheets; i++) {
			Variant vSheet = vSheets.OlePropertyGet("Item",i+1);
			UnicodeString str = vSheet.OlePropertyGet("Name");
			if (str.UpperCase() == UnicodeString("классификации").UpperCase()) {
				m_nPageClassification = i+1;
			};
			if (str.UpperCase().Pos("МЕСЯЦ ") > 0) {
				cmbMonth->Items->Add(str);
				exlSheet s;
				s.Number = i+1;
				s.Name = str;
				m_vMonth.push_back(s);
			}
			Log->Lines->Add("Лист " + IntToStr(i+1) + ": " + str);
		}

		if (m_nPageClassification == 0) {
			MessageBox (Handle, UnicodeString(L"Не найден лист 'классификации'").c_str(), L"prompt", MB_OK);
			app.OleProcedure("Quit");
			edtFile->Text = "";
			return;
		}

		if (m_vMonth.size() == 0) {
			MessageBox (Handle, UnicodeString(L"Не найден ни один лист с 'месяц xx'").c_str(), L"prompt", MB_OK);
			app.OleProcedure("Quit");
			edtFile->Text = "";
			return;
        }
		app.OleProcedure("Quit");
		m_strFileName = strFileName;
    }
}