简体   繁体   中英

How do i create a function to export data to a excel file PHP

Hi all i have a plugin that i built. that displays data in a table that looks like this:

Now i need to export this into into a excel file in PHP without a library as this would require me to add the library to moodle and i'm not so sure how to do this.

This is the code to create the table in html on the site.

<?php

    require_once('../../config.php');
    global $DB;

    $courseid = required_param('courseid', PARAM_INT);
    $syncid = optional_param('syncid', 0, PARAM_INT);

    $context = context_system::instance();
    $heading = get_string('pluginname', 'local_gradebook_sync');
    $title = get_string('pluginname', 'local_gradebook_sync');
    $url = new moodle_url('/local/gradebook_sync/sync.php', ['courseid' => $courseid]);

    $PAGE->set_context($context);
    $PAGE->set_heading($heading);
    $PAGE->set_title($title);
    $PAGE->set_url($url);

    $gradebook_sync = $DB->get_record('local_gradebook_sync', ['id' => $syncid]);
    $destination_course = get_course($gradebook_sync->destinationcourseid);

    echo $OUTPUT->header();

    echo $OUTPUT->heading($destination_course->fullname . ' (' . $destination_course->shortname . ')');

    $table = new html_table();
    $table->head = ['Section', 'Activity', 'Activity type', 'Status'];

    $source_course_modules_sql = <<<SQL
        SELECT
            cm.id,
            m.name AS 'module',
            cm.instance AS 'instance',
            cs.section,
            cm.idnumber
        FROM {course_modules} cm
        JOIN {course_sections} cs ON cs.id = cm.section
        JOIN {modules} m ON m.id = cm.module
        WHERE
            cm.course = :course
            AND
            m.name NOT IN ('book', 'resource', 'folder', 'imscp', 'label', 'page','url')
    SQL;
    $source_course_modules_variables = ['course' => $gradebook_sync->sourcecourseid];
    $source_course_modules = $DB->get_records_sql($source_course_modules_sql, $source_course_modules_variables);

    foreach($source_course_modules as $source_course_module) {
        $source_course_module->activitytitle = $DB->get_record_sql('SELECT name FROM {' . $source_course_module->module . '} WHERE id = :id', ['id' => $source_course_module->instance])->name;
    }

    $destination_course_modules_sql = <<<SQL
        SELECT
            cm.id,
            cs.section,
            IFNULL(NULLIF(cs.name, ''), IF(cs.section = 0, 'General', CONCAT('Topic ', cs.section))) AS 'sectiontitle',
            m.name AS 'module',
            cm.instance AS 'instance',
            cm.idnumber
        FROM {course_modules} cm
        JOIN {course_sections} cs ON cs.id = cm.section
        JOIN {modules} m ON m.id = cm.module
        WHERE
            cm.course = :course
            AND
            m.name NOT IN ('book', 'resource', 'folder', 'imscp', 'label', 'page','url')
    SQL;
    $destination_course_modules_variables = ['course' => $gradebook_sync->destinationcourseid];
    $destination_course_modules = $DB->get_records_sql($destination_course_modules_sql, $destination_course_modules_variables);

    foreach($destination_course_modules as $destination_course_module) {
        $destination_course_module->activitytitle = $DB->get_record_sql('SELECT name FROM {' . $destination_course_module->module . '} WHERE id = :id', ['id' => $destination_course_module->instance])->name;

        switch($destination_course_module->module) {
            case 'assign':
                $destination_course_module->activitytype = 'Assignment';
                break;
            case 'assignment':
                $destination_course_module->activitytype = 'Assignment 2.2';
                break;
            case 'book':
                $destination_course_module->activitytype = 'Book';
                break;
            case 'chat':
                $destination_course_module->activitytype = 'Chat';
                break;
            case 'checklist':
                $destination_course_module->activitytype = 'Checklist';
                break;
            case 'choice':
                $destination_course_module->activitytype = 'Choice';
                break;
            case 'data':
                $destination_course_module->activitytype = 'Database';
                break;
            case 'feedback':
                $destination_course_module->activitytype = 'Feedback';
                break;
            case 'folder':
                $destination_course_module->activitytype = 'Folder';
                break;
            case 'forum':
                $destination_course_module->activitytype = 'Forum';
                break;
            case 'glossary':
                $destination_course_module->activitytype = 'Glossary';
                break;
            case 'h5pactivity':
                $destination_course_module->activitytype = 'H5P';
                break;
            case 'hvp':
                $destination_course_module->activitytype = 'H5P';
                break;
            case 'imscp':
                $destination_course_module->activitytype = 'IMS content package';
                break;
            case 'label':
                $destination_course_module->activitytype = 'Label';
                break;
            case 'lesson':
                $destination_course_module->activitytype = 'Lesson';
                break;
            case 'lti':
                $destination_course_module->activitytype = 'External tool';
                break;
            case 'page':
                $destination_course_module->activitytype = 'Page';
                break;
            case 'questionnaire':
                $destination_course_module->activitytype = 'Questionnaire';
                break;
            case 'quiz':
                $destination_course_module->activitytype = 'Quiz';
                break;
            case 'resource':
                $destination_course_module->activitytype = 'File';
                break;
            case 'scorm':
                $destination_course_module->activitytype = 'SCORM package';
                break;
            case 'survey':
                $destination_course_module->activitytype = 'Survey';
                break;
            case 'url':
                $destination_course_module->activitytype = 'URL';
                break;
            case 'wiki':
                $destination_course_module->activitytype = 'Wiki';
                break;
            case 'workshop':
                $destination_course_module->activitytype = 'Workshop';
                break;
            default:
                $destination_course_module->activitytype = 'Unknown';
        }

        $destination_course_module->status = 'INVALID';

        foreach($source_course_modules AS $source_course_module) {
            if((trim($source_course_module->activitytitle) == trim($destination_course_module->activitytitle)) && ($source_course_module->section == $destination_course_module->section) && ($source_course_module->module == $destination_course_module->module)) {
                $destination_course_module->status = 'VALID';
            }
        }

        $table_row = new html_table_row([$destination_course_module->sectiontitle, $destination_course_module->activitytitle, $destination_course_module->activitytype, $destination_course_module->status]);
        $table->data[] = $table_row;
    }

    echo html_writer::table($table);

    echo $OUTPUT->footer();

Can anyone help as i am really stumped.

Note: this is a Moodle plugin if that is helpful

Thanks in advance.

It should help you without installing any packages

PHP_XLSXWriter

For Moodle, I'd recommend searching the code first.

There is already an excel class you can use.

For an example, see mod/survey/download.php

require_once("$CFG->libdir/excellib.class.php");

$workbook = new MoodleExcelWorkbook('-');

// Send HTTP headers
$downloadfilename = 'myfilename.xls';
$workbook->send($downloadfilename);

$myxls = $workbook->add_worksheet('myworksheet');

$headers = array("column1","column2","column3");
$row = 0;
$col = 0;
foreach ($headers as $header) {
    $myxls->write_string($row, $col++, $header);
}

foreach ($grades as $grade) {
    $row++;
    $col = 0;
    foreach ($grade as $name => $value) {
        $myxls->write_string($row, $col++, $value);
    }
}

$workbook->close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM