📄 default.php
字号:
$this->print_text_cells($table->head, $options); } } function print_text_data(&$table, &$options) { if (isset($table->data)) { $this->expand_spans($table, 'data'); foreach ($table->data as $cells) { $this->print_text_cells($cells, $options); } } } function print_text_stat(&$table, &$options) { if (isset($table->stat)) { $this->expand_spans($table, 'stat'); foreach ($table->stat as $cells) { $this->print_text_cells($cells, $options); } } } function print_text_foot(&$table, &$options) { if (isset($table->foot)) { $this->expand_spans($table, 'foot'); foreach ($table->foot as $cells) { $this->print_text_cells($cells, $options); } } } function print_text_cells(&$cells, &$options) { // do nothing if there are no cells if (empty($cells) || is_string($cells)) return; // convert to tab-delimted string $str = implode("\t", $cells); // replace newlines in string $str = preg_replace("/\n/", ",", $str); // set best newline for this browser (if it hasn't been done already) if (empty($this->nl)) { $s = &$_SERVER['HTTP_USER_AGENT']; $win = is_numeric(strpos($s, 'Win')); $mac = is_numeric(strpos($s, 'Mac')) && !is_numeric(strpos($s, 'OS X')); $this->nl = $win ? "\r\n" : ($mac ? "\r" : "\n"); } print $str.$this->nl; }///////////////////////////////////////////// print an Excel report function print_excel_report(&$course, &$hotpot, &$tables, &$options) { global $CFG; // create Excel workbook if (file_exists("$CFG->libdir/excellib.class.php")) { // Moodle >= 1.6 require_once("$CFG->libdir/excellib.class.php"); $wb = new MoodleExcelWorkbook("-"); $wsnamelimit = 0; // no limit } else { // Moodle <= 1.5 require_once("$CFG->libdir/excel/Worksheet.php"); require_once("$CFG->libdir/excel/Workbook.php"); $wb = new Workbook("-"); $wsnamelimit = 31; // max length in chars } // send HTTP headers $this->print_excel_headers($wb, $course, $hotpot); // create one worksheet for each table foreach($tables as $table) { unset($ws); if (empty($table->caption)) { $wsname = ''; } else { $wsname = strip_tags($table->caption); if ($wsnamelimit && strlen($wsname) > $wsnamelimit) { $wsname = substr($wsname, -$wsnamelimit); // end of string // $wsname = substr($wsname, 0, $wsnamelimit); // start of string } } $ws = &$wb->add_worksheet($wsname); $row = 0; $this->print_excel_head($wb, $ws, $table, $row, $options); $this->print_excel_data($wb, $ws, $table, $row, $options); $this->print_excel_stat($wb, $ws, $table, $row, $options); $this->print_excel_foot($wb, $ws, $table, $row, $options); } // close the workbook (and send it to the browser) $wb->close(); } function print_excel_headers(&$wb, &$course, &$hotpot) { $downloadfilename = clean_filename("$course->shortname $hotpot->name.xls"); if (method_exists($wb, 'send')) { // Moodle >=1.6 $wb->send($downloadfilename); } else { // Moodle <=1.5 header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=$downloadfilename" ); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0,pre-check=0"); header("Pragma: public"); } } function print_excel_head(&$wb, &$ws, &$table, &$row, &$options) { // define format properties $properties = array( 'bold'=>1, 'align'=>'center', 'v_align'=>'bottom', 'text_wrap'=>1 ); // expand multi-column and multi-row cells $this->expand_spans($table, 'head'); // print the headings $this->print_excel_cells($wb, $ws, $table, $row, $properties, $table->head, $options); } function print_excel_data(&$wb, &$ws, &$table, &$row, &$options) { // do nothing if there are no cells if (empty($table->data)) return; // define format properties $properties = array('text_wrap' => (empty($options['reportwrapdata']) ? 0 : 1)); // expand multi-column and multi-row cells $this->expand_spans($table, 'data'); // print rows foreach ($table->data as $cells) { $this->print_excel_cells($wb, $ws, $table, $row, $properties, $cells, $options); } } function print_excel_stat(&$wb, &$ws, &$table, &$row, &$options) { // do nothing if there are no cells if (empty($table->stat)) return; // define format properties $properties = array('align'=>'right'); // expand multi-column and multi-row cells $this->expand_spans($table, 'stat'); // print rows $i_count = count($table->stat); foreach ($table->stat as $i => $cells) { // set border on top and bottom row $properties['top'] = ($i==0) ? 1 : 0; $properties['bottom'] = ($i==($i_count-1)) ? 1 : 0; // print this row $this->print_excel_cells($wb, $ws, $table, $row, $properties, $cells, $options, $table->statheadercols); } } function print_excel_foot(&$wb, &$ws, &$table, &$row, &$options) { // do nothing if there are no cells if (empty($table->foot)) return; // define format properties $properties = array('bold'=>1, 'align'=>'center'); // expand multi-column and multi-row cells $this->expand_spans($table, 'foot'); // print rows $i_count = count($table->foot); foreach ($table->foot as $i => $cells) { // set border on top and bottom row $properties['top'] = ($i==0) ? 1 : 0; $properties['bottom'] = ($i==($i_count-1)) ? 1 : 0; // print this footer row $this->print_excel_cells($wb, $ws, $table, $row, $properties, $cells, $options); } } function print_excel_cells(&$wb, &$ws, &$table, &$row, &$properties, &$cells, &$options, $statheadercols=NULL) { // do nothing if there are no cells if (empty($cells) || is_string($cells)) return; // print cells foreach($cells as $col => $cell) { unset($fmt_properties); $fmt_properties = $properties; if (empty($fmt_properties['text_wrap'])) { if (strlen("$cell")>=9) { // long cell value $fmt_properties['align'] = 'left'; } } else { if (strlen("$cell")<9 && strpos("$cell", "\n")===false) { // short cell value (wrapping not required) $fmt_properties['text_wrap'] = 0; } } // set bold, if required (for stat) if (isset($statheadercols)) { $fmt_properties['bold'] = in_array($col, $statheadercols) ? 1 : 0; $fmt_properties['align'] = in_array($col, $statheadercols) ? 'right' : $table->align[$col]; } // set align, if required if (isset($table->align[$col]) && empty($fmt_properties['align'])) { $fmt_properties['align'] = $table->align[$col]; } // check to see that an identical format object has not already been created unset($fmt); if (isset($wb->pear_excel_workbook)) { // Moodle >=1.6 $fmt_properties_obj = (object)$fmt_properties; foreach ($wb->pear_excel_workbook->_formats as $id=>$format) { if ($format==$fmt_properties_obj) { $fmt = &$wb->pear_excel_workbook->_formats[$id]; break; } } } else { // Moodle <=1.5 foreach ($wb->formats as $id=>$format) { if (isset($format->properties) && $format->properties==$fmt_properties) { $fmt = &$wb->formats[$id]; break; } } if (is_numeric($cell) || empty($options['reportencoding'])) { // do nothing } else { $in_charset = ''; if (function_exists('mb_convert_encoding')) { $in_charset = mb_detect_encoding($cell, 'auto'); } if (empty($in_charset)) { $in_charset = get_string('thischarset'); } if ($in_charset != 'ASCII' && function_exists('mb_convert_encoding')) { $cell = mb_convert_encoding($cell, $options['reportencoding'], $in_charset); } } } // create new format object, if necessary (to avoid "too many cell formats" error) if (!isset($fmt)) { $fmt = &$wb->add_format($fmt_properties); $fmt->properties = &$fmt_properties; // set vertical alignment if (isset($fmt->properties['v_align'])) { $fmt->set_align($fmt->properties['v_align']); } else { $fmt->set_align('top'); // default } } // write cell if (is_numeric($cell) && !preg_match("/^0./", $cell)) { $ws->write_number($row, $col, $cell, $fmt); } else { $ws->write_string($row, $col, $cell, $fmt); } } // end foreach $col // increment $row $row++; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -