ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilExcelXMLAdapter.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 {
16 
17  function __construct()
18  {
19  $worksheets = array();
20 
22  include_once './Services/Excel/PHPExcel/1.7.6/Classes/PHPExcel.php';
23 
25  include_once './Services/Excel/PHPExcel/1.7.6/Classes/PHPExcel/Writer/Excel2007.php';
26 
27  // set cache to temp dir to prevent memory exhaustion
29  $cacheSettings = array();
30  PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
31 
32  // Create new PHPExcel object
33  $this->objPHPExcel = new PHPExcel();
34 
35  // Set properties
36  $this->objPHPExcel->getProperties()->setCreator("ILIAS Open Source");
37  $this->objPHPExcel->getProperties()->setLastModifiedBy("ILIAS Open Source");
38  $this->objPHPExcel->getProperties()->setTitle($this->strFormKey);
39  $this->objPHPExcel->getProperties()->setSubject($this->strFormKey);
40  $this->objPHPExcel->getProperties()->setDescription($this->strFormKey);
41  $this->objPHPExcel->getProperties()->setKeywords("Office 2007 ILIAS Open Source");
42  $this->objPHPExcel->getProperties()->setCategory("wcms data storage");
43 
44  $this->objPHPExcel->setActiveSheetIndex(0);
45  }
46 
47  public function save()
48  {
49  include_once "./Services/Utilities/classes/class.ilUtil.php";
50  $objWriter = new PHPExcel_Writer_Excel2007($this->objPHPExcel);
51  $tempname = ilUtil::ilTempnam() .".xlsx";
52  $objWriter->save($tempname);
53  return $tempname;
54  }
55 
56  public function deliver($outputfilename)
57  {
58  $tempname = $this->save();
59  header('Content-Type: appplication/excel');
60  header('Content-Transfer-Encoding: binary');
61  header('Content-Disposition: attachment; filename="' . $outputfilename . '"');
62  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
63  header('Pragma: public');
64  header('Expires: 0');
65  print file_get_contents($tempname);
66  unlink($tempname);
67  exit;
68  }
69 
70  public function getWorksheetCount()
71  {
72  return $this->objPHPExcel->getSheetCount();
73  }
74 
75  public function setActiveWorksheet($index = 0)
76  {
77  $this->objPHPExcel->setActiveSheetIndex($index);
78  }
79 
80  public function setWorksheetTitle($title)
81  {
82  try
83  {
84  $this->objPHPExcel->getActiveSheet()->setTitle(substr($title, 0, 31));
85  }
86  catch (Exception $e)
87  {
88 
89  }
90  }
91 
92  public function setColumnWidth($columnindex, $width)
93  {
94  if (strcmp($width, 'auto') == 0)
95  {
96  $this->objPHPExcel->getActiveSheet()->getColumnDimension($this->getCellTitle($columnindex))->setAutoSize(true);
97  }
98  else
99  {
100  $this->objPHPExcel->getActiveSheet()->getColumnDimension($this->getCellTitle($columnindex))->setWidth($width);
101  }
102  }
103 
104  public function addWorksheet($title = "")
105  {
106  $objWorksheet = $this->objPHPExcel->createSheet();
107  try
108  {
109  $objWorksheet->setTitle(substr($title, 0, 31));
110  }
111  catch (Exception $e)
112  {
113 
114  }
115  return $objWorksheet;
116  }
117 
118  public function setCellValue($row, $col, $value, $format = CELL_FORMAT_NONE)
119  {
120  $cell = $this->getCellTitle($col).($row+1);
121  $this->objPHPExcel->getActiveSheet()->setCellValue($cell, $value);
122  switch ($format)
123  {
125  $this->objPHPExcel->getActiveSheet()->getStyle($cell)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME);
126  break;
127  case CELL_FORMAT_PERCENT:
128  $this->objPHPExcel->getActiveSheet()->getStyle($cell)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE);
129  break;
130  case CELL_FORMAT_MINUTES:
131  $this->objPHPExcel->getActiveSheet()->getStyle($cell)->getNumberFormat()->setFormatCode('hh:mm');
132  break;
133  case CELL_FORMAT_TITLE:
134  $this->objPHPExcel->getActiveSheet()->getStyle($cell)->applyFromArray($this->formatCellTitle());
135  break;
136  case CELL_FORMAT_CENTER:
137  $this->objPHPExcel->getActiveSheet()->getStyle($cell)->applyFromArray($this->formatCellCenter());
138  break;
139  case CELL_FORMAT_BOLD:
140  $this->objPHPExcel->getActiveSheet()->getStyle($cell)->applyFromArray($this->formatCellBold());
141  break;
142  case CELL_FORMAT_NONE:
143  default:
144  break;
145  }
146  }
147 
148  protected function formatCellTitle()
149  {
150  return array(
151  'font' => array(
152  'bold' => true,
153  'color' => array(
154  'argb' => 'FF000000'
155  )
156  ),
157  'fill' => array(
159  'color' => array(
160  'argb'=>'80AAAAAA'
161  ),
162  ),
163  'alignment' => array(
165  )
166  );
167  }
168 
169  protected function formatCellCenter()
170  {
171  return array(
172  'font' => array(
173  'bold' => false,
174  'color' => array(
175  'argb' => 'FF000000'
176  )
177  ),
178  'alignment' => array(
180  )
181  );
182  }
183 
184  protected function formatCellBold()
185  {
186  return array(
187  'font' => array(
188  'bold' => true,
189  'color' => array(
190  'argb' => 'FF000000'
191  )
192  )
193  );
194  }
195 
196  protected function formatCellPercent()
197  {
198  return array(
199  'font' => array(
200  'bold' => true,
201  'color' => array(
202  'argb' => '00000080'
203  )
204  ),
205  'alignment' => array(
207  )
208  );
209  }
210 
211  public function getFileExtension()
212  {
213  return "xlsx";
214  }
215 
216  private function getCellTitle($index)
217  {
218  if ($index < 26) {
219  return chr(65 + $index);
220  } elseif ($index < 702) {
221  return chr(64 + ($index / 26)).chr(65 + $index % 26);
222  }
223  return chr(64 + (($index - 26) / 676)).chr(65 + ((($index - 26) % 676) / 26)).chr(65 + $index % 26);
224  }
225 }