ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilExcelXLSAdapter.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Services/Excel/classes/class.ilExcelAdapter.php";
5 
14 {
15  var $adapter;
16  var $workbook;
25 
26  function __construct()
27  {
28  $worksheets = array();
29 
30  $this->excelfile = ilUtil::ilTempnam();
31  include_once "./Services/Excel/classes/class.ilExcelWriterAdapter.php";
32  $this->adapter = new ilExcelWriterAdapter($this->excelfile, FALSE);
33  $this->workbook = $this->adapter->getWorkbook();
34  $this->workbook->setVersion(8); // Use Excel97/2000 Format
35 
36  $this->format_bold =& $this->workbook->addFormat();
37  $this->format_bold->setBold();
38  $this->format_center =& $this->workbook->addFormat();
39  $this->format_center->setColor('black');
40  $this->format_center->setAlign('center');
41  $this->format_percent =& $this->workbook->addFormat();
42  $this->format_percent->setNumFormat("0.00%");
43  $this->format_datetime =& $this->workbook->addFormat();
44  $this->format_datetime->setNumFormat("DD/MM/YYYY hh:mm:ss");
45  $this->format_minutes =& $this->workbook->addFormat();
46  $this->format_minutes->setNumFormat("mm:ss");
47  $this->format_title =& $this->workbook->addFormat();
48  $this->format_title->setBold();
49  $this->format_title->setColor('black');
50  $this->format_title->setPattern(1);
51  $this->format_title->setFgColor('silver');
52 
53  $this->addWorksheet('table 1');
54  }
55 
56  public function save()
57  {
58  $this->workbook->close();
59  return $this->excelfile;
60  }
61 
62  public function deliver($outputfilename)
63  {
64  $this->workbook->close();
65  ilUtil::deliverFile($this->excelfile, $outputfilename, "application/vnd.ms-excel", false, true);
66  exit;
67  }
68 
69  public function getWorksheetCount()
70  {
71  return count($this->workbook->worksheets());
72  }
73 
74  public function setActiveWorksheet($index = 0)
75  {
76  $this->activesheet = $index;
77  }
78 
79  protected function getActiveSheet()
80  {
81  $sheets = $this->workbook->worksheets();
82  return $sheets[$this->activesheet];
83  }
84 
85  public function setWorksheetTitle($title)
86  {
87  include_once "./Services/Excel/classes/class.ilExcelUtils.php";
88  $this->getActiveSheet()->name = substr($this->convertToWindowsCharset($title), 0, 31);
89  }
90 
91  public function addWorksheet($title = "")
92  {
93  include_once "./Services/Excel/classes/class.ilExcelUtils.php";
94  if (strlen($title))
95  {
96  $objWorksheet = $this->workbook->addWorksheet($this->convertToWindowsCharset($title));
97  }
98  else
99  {
100  $objWorksheet = $this->workbook->addWorksheet();
101  }
102  $this->activesheet = $this->getWorksheetCount()-1;
103  return $objWorksheet;
104  }
105 
106  public function setColumnWidth($columnindex, $width)
107  {
108  }
109 
110  public function setCellValue($row, $col, $value, $format = CELL_FORMAT_NONE)
111  {
112  include_once "./Services/Excel/classes/class.ilExcelUtils.php";
113  switch ($format)
114  {
116  $this->getActiveSheet()->write($row, $col, $value, $this->format_datetime);
117  break;
118  case CELL_FORMAT_PERCENT:
119  $this->getActiveSheet()->write($row, $col, $value, $this->format_percent);
120  break;
121  case CELL_FORMAT_MINUTES:
122  $this->getActiveSheet()->write($row, $col, $value, $this->format_minutes);
123  break;
124  case CELL_FORMAT_CENTER:
125  $this->getActiveSheet()->write($row, $col, $value, $this->format_center);
126  break;
127  case CELL_FORMAT_TITLE:
128  $this->getActiveSheet()->writeString($row, $col, $this->convertToWindowsCharset($value), $this->format_title);
129  break;
130  case CELL_FORMAT_BOLD:
131  $this->getActiveSheet()->writeString($row, $col, $this->convertToWindowsCharset($value), $this->format_bold);
132  break;
133  case CELL_FORMAT_NONE:
134  default:
135  $this->getActiveSheet()->writeString($row, $col, $this->convertToWindowsCharset($value));
136  break;
137  }
138  }
139 
140  public function getFileExtension()
141  {
142  return "xls";
143  }
144 
149  function convertToWindowsCharset($string) {
150  $charset = mb_detect_encoding(
151  $string,
152  "UTF-8, ISO-8859-1, ISO-8859-15",
153  true
154  );
155 
156  $string = mb_convert_encoding($string, "Windows-1252", $charset);
157  return $string;
158  }
159 
160 
161 
162 }