Ejemplo n.º 1
0
static void copy_sheet(const char* from, const char* to)
{
	BasicExcel xls;

	xls.Load(from);
	xls.SaveAs(to);
}
Ejemplo n.º 2
0
int main(int argc, char* argv[])
{
	BasicExcel e;

	// Load a workbook with one sheet, display its contents and save into another file.
	e.Load("example1.xls");	
	BasicExcelWorksheet* sheet1 = e.GetWorksheet("Sheet1");
	if (sheet1)
	{
		size_t maxRows = sheet1->GetTotalRows();
		size_t maxCols = sheet1->GetTotalCols();
		cout << "Dimension of " << sheet1->GetAnsiSheetName() << " (" << maxRows << ", " << maxCols << ")" << endl;

		printf("          ");
		for (size_t c=0; c<maxCols; ++c) printf("%10d", c+1);
		cout << endl;

		for (size_t r=0; r<maxRows; ++r)
		{
			printf("%10d", r+1);
			for (size_t c=0; c<maxCols; ++c)
			{
				BasicExcelCell* cell = sheet1->Cell(r,c);
				switch (cell->Type())
				{
					case BasicExcelCell::UNDEFINED:
						printf("          ");
						break;

					case BasicExcelCell::INT:
						printf("%10d", cell->GetInteger());
						break;

					case BasicExcelCell::DOUBLE:
						printf("%10.6lf", cell->GetDouble());
						break;

					case BasicExcelCell::STRING:
						printf("%10s", cell->GetString());
						break;

					case BasicExcelCell::WSTRING:
						wprintf(L"%10s", cell->GetWString());
						break;
				}
			}
			cout << endl;
		}
	}
	cout << endl;
	e.SaveAs("example2.xls");

	// Create a new workbook with 2 worksheets and write some contents.
	e.New(2);
	e.RenameWorksheet("Sheet1", "Test1");
	BasicExcelWorksheet* sheet = e.GetWorksheet("Test1");
	BasicExcelCell* cell;
	if (sheet)
	{
		for (size_t c=0; c<4; ++c)
		{
			cell = sheet->Cell(0,c);
			cell->Set((int)c);
		}

		cell = sheet->Cell(1,3);
		cell->SetDouble(3.141592654);

		sheet->Cell(1,4)->SetString("Test str1");
		sheet->Cell(2,0)->SetString("Test str2");
		sheet->Cell(2,5)->SetString("Test str1");

		sheet->Cell(4,0)->SetDouble(1.1);
		sheet->Cell(4,1)->SetDouble(2.2);
		sheet->Cell(4,2)->SetDouble(3.3);
		sheet->Cell(4,3)->SetDouble(4.4);
		sheet->Cell(4,4)->SetDouble(5.5);

		sheet->Cell(4,4)->EraseContents();
	}

	sheet = e.AddWorksheet("Test2", 1);
	sheet = e.GetWorksheet(1); 
	if (sheet)
	{
		sheet->Cell(1,1)->SetDouble(1.1);
		sheet->Cell(2,2)->SetDouble(2.2);
		sheet->Cell(3,3)->SetDouble(3.3);
		sheet->Cell(4,4)->SetDouble(4.4);
		sheet->Cell(70,2)->SetDouble(5.5);
	}
	e.SaveAs("example3.xls");

	// Load the newly created sheet and display its contents
	e.Load("example3.xls");

	size_t maxSheets = e.GetTotalWorkSheets();
	cout << "Total number of worksheets: " << e.GetTotalWorkSheets() << endl;
	for (size_t i=0; i<maxSheets; ++i)
	{
		BasicExcelWorksheet* sheet = e.GetWorksheet(i);
		if (sheet)
		{
			size_t maxRows = sheet->GetTotalRows();
			size_t maxCols = sheet->GetTotalCols();
			cout << "Dimension of " << sheet->GetAnsiSheetName() << " (" << maxRows << ", " << maxCols << ")" << endl;

			if (maxRows>0)
			{
				printf("          ");
				for (size_t c=0; c<maxCols; ++c) printf("%10d", c+1);
				cout << endl;
			}

			for (size_t r=0; r<maxRows; ++r)
			{
				printf("%10d", r+1);
				for (size_t c=0; c<maxCols; ++c)
				{
					cout << setw(10) << *(sheet->Cell(r,c));	// Another way of printing a cell content.				
				}
				cout << endl;
			}
			if (i==0)
			{
				ofstream f("example4.csv");
				sheet->Print(f, ',', '\"');	// Save the first sheet as a CSV file.
				f.close();
			}
		}
		cout << endl;
	}
	return 0;
}
Ejemplo n.º 3
0
int main (int argc, char *argv[])
{
	srand (int(time(NULL)));
        if (argc != 2)
        {
		if (argc < 2)
			cout << "Error. Input file was not specified.\n";
		if (argc > 2)
			cout << "Error. Too many arguments.\n";
                return 1;
        }

	QSqlDatabase database = QSqlDatabase::addDatabase(DBTYPE);
	database.setHostName(HOST);
	database.setPort(GATE);
	database.setDatabaseName(DATABASE);
	database.setUserName(USER);
	database.setPassword(PASS);
	database.open();
        int tryCount = 0;
	while (!database.isOpen() && tryCount++ < DBLIMIT)
	{
                cout << "Database connection error. Attempting to reconnect in 5 seconds.\n";
                sleep(5);
                cout <<  "Reconnecting attempt... " << tryCount << "/" << DBLIMIT << "\n";
		database.open();
	}
	if (!database.isOpen())
	{
		cout << "Database connection error.";
		return 2;
	}

        FILE *phpFile, *myFile;
        char tmpName[500]="0";
        strcpy(tmpName, argv[1]);
        char* here = strrchr(tmpName, '/');
        if (here != NULL)
        {
            here+=1;
            strcpy(here, "tmp_file.xls\0");
        }
        else
            strcpy(tmpName, "tmp_file.xls\0");

        phpFile = fopen(argv[1], "rb");
        myFile = fopen(tmpName, "wb");
        if (phpFile == NULL)
        {
            cout << "File open error: " << argv[1] << endl;
            return 3;
        }
        if (myFile == NULL)
        {
            cout << "Temp file create error" << endl;
            return 4;
        }
        char buff[1024] = "\0";
        int size = 0;
        while (!feof(phpFile) && !ferror(phpFile))
        {
            size = fread(buff, 1, 1024, phpFile);
            fwrite(buff, 1, size, myFile);
        }
        if (ferror(phpFile))
        {
            cout << "File read error." << endl;
            return 5;
        }
        fclose(phpFile);
        fclose(myFile);

        BasicExcel xls;
        if (!xls.Load(tmpName))
        {
            cout << "Specified file is not excel file. " << endl;
            return 6;
        }
	BasicExcelWorksheet* sheet = xls.GetWorksheet(0);
	int rows = sheet->GetTotalRows(), test = 0, c0=0, c1=0, c2=0, c3=0;
	// XLS TYPES: 0-UNDEFINED, 1-INTEGER, 2-DOUBLE, 3-STRING, 4-WSTRING, 5-FORMULA
	while (test < rows)
	{
		c0=sheet->Cell(test, 0)->Type();
		c1=sheet->Cell(test, 1)->Type();
		c2=sheet->Cell(test, 2)->Type();
		c3=sheet->Cell(test, 3)->Type();
                if ((c0 != 3 && c0 != 4) || (c1 != 3 && c1 != 4) || (c2 != 1 && c2 != 2) || (c3 != 3))
		{
			cout << "Error. Wrong table format.\nc0=" << c0 << "; c1=" << c1 << "; c2=" << c2 << "; c3=" << c3 << ";\n";
                        return 7;
		}
		test++;
	}

        QSqlQuery insertQuery(database), testQuery(database);
        QString templ = "insert into users (name, surname, pass, mail, indeks) values (\"%1\", \"%2\", \"%3\", \"%4\", %5);";
        QString testTemplIndex = "SELECT id FROM users WHERE %1 = %2";
        QString testTemplMail = "SELECT id FROM users WHERE %1 = \"%2\"";
        QString statement, name, surname, mail, index, pass;
        QRegExp mailPattern("^[a-zA-Z0-9_\\.\\-]+@[a-zA-Z0-9\\-]+\\.[a-zA-Z0-9\\-\\.]+$");
        char passwd[9];

        int i=0, insert_test = 0, pasch=0;
	while (i < rows)
	{
                getCellVal(name, sheet, i, 0);
                getCellVal(surname, sheet, i, 1);
                getCellVal(mail, sheet, i, 3);
                index.clear();
                index = QString::number(sheet->Cell(i,2)->GetInteger());
                strcpy(passwd, "AAAAAAAA");
                for (pasch=0; pasch < 8; pasch++)
                {
                    passwd[pasch]+= (rand()%26);
                    if (rand()%2)
                        passwd[pasch]+= 32;
                }
                passwd[pasch]='\0';
                pass = passwd;

                cout << index.toStdString() << endl << name.toUtf8().data() << endl << surname.toUtf8().data() << endl << mail.toStdString() << endl;

                if (name.indexOf(QRegExp("[^A-Za-z]")) != -1)
                {
                    cout << "1" << endl << QString::fromUtf8("Błędne imie").toUtf8().data() << endl << "0" << endl;
                    i++;
                    continue;
                }
                if (surname.indexOf(QRegExp("[^A-Za-z]")) != -1)
                {
                    cout << "1" << endl << QString::fromUtf8("Błędne nazwisko").toUtf8().data() << endl << "0" << endl;
                    i++;
                    continue;
                }
                if (index.indexOf(QRegExp("[^0-9]")) != -1)
                {
                    cout << "1" << endl << QString::fromUtf8("Błędny index").toUtf8().data() << endl << "0" << endl;
                    i++;
                    continue;
                }
                if (!mailPattern.exactMatch(mail))
                {
                    cout << "1" << endl << QString::fromUtf8("Błędny adres email").toUtf8().data() << endl << "0" << endl;
                    i++;
                    continue;
                }

                testQuery.exec(testTemplIndex.arg(QString("indeks"), index));
                if (testQuery.size() != 0)
                {
                    testQuery.next();
                    cout << "1" << endl << QString::fromUtf8("Użytkownik o podanym indeksie jest już w bazie, ale został dodany do grupy").toUtf8().data() << endl << QString::number(testQuery.value(0).toInt()).toAscii().data() << endl;
                    i++;
                    continue;
                }

                testQuery.exec(testTemplMail.arg(QString("mail"), mail));
                if (testQuery.size() != 0)
                {
                    cout << "1" << endl << QString::fromUtf8("Użytkownik o podanym emailu jest już w bazie").toUtf8().data() << endl << "0" << endl;
                    i++;
                    continue;
                }


                statement = templ.arg(name, surname, (QCryptographicHash::hash(pass.toAscii(), QCryptographicHash::Sha1)).toHex(), mail, index);

                if (!insertQuery.exec(statement))
                    cout << "1" << endl << insertQuery.lastError().text().toUtf8().data() << endl << "0" << endl;
                else
                {
                    cout << "0" << endl << passwd << endl << QString::number(insertQuery.lastInsertId().toInt()).toAscii().data() << endl;
                    insert_test++;
                }
		i++;
	}

        if ( remove(tmpName) != 0)
        {
            cout << "Error deleting temp file." << endl;
            return 8;
        }
        if (remove(argv[1]) != 0)
        {
            cout << "Error deleting file " << argv[1] << endl;
            return 9;
        }
        if (insert_test <= 0)
            return 200;
        if (insert_test < rows)
            return 100;
        return 0;
}