處理中-內容的處理
設置reader和編碼
$file_path = 'test.xls';
$excel5_reader = new PHPExcel_Reader_Excel5();
$excel5_reader->_defaultEncoding = $encoding;
$excel2007_reader = new PHPExcel_Reader_Excel2007();
$excel2007_reader->_defaultEncoding = $encoding;
$reader = null;
if($excel5_reader->canRead($file_path)) {
$reader = $excel5_reader;
} elseif($excel2007_reader->canRead($file_path)) {
$reader = $excel2007_reader;
} else {
return array();
}
處理多sheet和遍歷內容
- 核心方法:$all_sheets = $table_object->getAllSheets();
$table_object = $reader->load($file_path);
$all_sheets = $table_object->getAllSheets();
$i = 0;
foreach ($all_sheets as $key => $sheet_obj) {
$sheet_title = $sheet_obj->getTitle();
$row_iterator = $sheet_obj->getRowIterator();
foreach ($row_iterator as $k1 => $row_obj) {
$cell_iterator = $row_obj->getCellIterator();
}
}
處理樣式
- 核心方法: $cell_obj->getStyle();
- 下述方法爲轉化爲css標準樣式,可以直接拿來使用
$style_object = $cell_obj->getStyle();
$style = [];//css標準樣式
$fill_object = $style_object->getFill();
$font_object = $style_object->getFont();
$borders_object = $style_object->getBorders();
$alignment_object = $style_object->getAlignment();
$fill_type = $fill_object->getFillType();
if (PHPExcel_Style_Fill::FILL_SOLID === $fill_type) {
$color = $fill_object->getStartColor()->getRGB();
$style['background-color'] = '#'. $color;
}
$top_border_style = $borders_object->getTop()->getBorderStyle();
$top_border_color = $borders_object->getTop()->getColor()->getRGB();
$top_border_style = $this->_trick_excel_border_style($top_border_style);
if (PHPExcel_Style_Border::BORDER_NONE !== $top_border_style) {
$style['border-top'] = '1px '. $top_border_style . ' #' . $top_border_color;
}
$left_border_style = $borders_object->getLeft()->getBorderStyle();
$left_border_color = $borders_object->getLeft()->getColor()->getRGB();
$left_border_style = $this->_trick_excel_border_style($left_border_style);
if (PHPExcel_Style_Border::BORDER_NONE !== $left_border_style) {
$style['border-left'] = '1px '. $left_border_style . ' #' . $left_border_color;
}
$bottom_border_style = $borders_object->getBottom()->getBorderStyle();
$bottom_border_color = $borders_object->getBottom()->getColor()->getRGB();
$bottom_border_style = $this->_trick_excel_border_style($bottom_border_style);
if (PHPExcel_Style_Border::BORDER_NONE !== $bottom_border_style) {
$style['border-bottom'] = '1px '. $bottom_border_style . ' #' . $bottom_border_color;
}
$right_border_style = $borders_object->getRight()->getBorderStyle();
$right_border_color = $borders_object->getRight()->getColor()->getRGB();
$right_border_style = $this->_trick_excel_border_style($right_border_style);
if (PHPExcel_Style_Border::BORDER_NONE !== $right_border_style) {
$style['border-right'] = '1px '. $right_border_style . ' #' . $right_border_color;
}
$font_size = $font_object->getSize();
if (isset($font_size) && $font_size > 11) {
$style['font-size'] = $font_size . 'px';
}
$font_color = $font_object->getColor()->getRGB();
if (isset($font_color) && $font_color != '000000') {
$style['color'] = '#' . $font_color;
}
$font_bold = $font_object->getBold();
if ($font_bold) {
$style['font-weight'] = 'bold';
}
$font_italic = $font_object->getItalic();
if ($font_italic) {
$style['font-style'] = "italic";
}
$font_underline = $font_object->getUnderline();
if ($font_underline && $font_underline != PHPExcel_Style_Font::UNDERLINE_NONE) {
$style['text-decoration'] = "underline";
}
$font_strikethrough = $font_object->getStrikethrough();
if ($font_strikethrough) {
$style['text-decoration'] = "line-through";
}
$horizontal = $alignment_object->getHorizontal();
if ($horizontal != 'general') {
$style['text-align'] = $horizontal;
}
$vertical = $alignment_object->getVertical();
if ($vertical != 'bottom') {
$style['vertical-align'] = $vertical;
}
獲取數據
- 核心方法:$cell_obj->getCalculatedValue();
- 注意防止注入
$cell_obj->getCalculatedValue();//會處理公式和富文本的值 string
$cell_obj->getValue();//不處理公式和富文本的值,存在多種類型
處理超鏈接
核心方法:$cell_obj->getHyperlink()->getUrl();
$value = $cell_obj->getCalculatedValue();
$url = $cell_obj->getHyperlink()->getUrl();
$link = '<a href="'.$url.'" target="_blank">' . $value . '</a>';
處理公式
- $cell_obj->getValue();
- 需要先判斷是否是公式值
- 返回格式string,ie A1:E1
if (!$cell_obj->isFormula()) {
return null;
}
$value = $cell_obj->getValue();//A1:E1
preg_match('/([a-zA-Z]+)\(([a-zA-Z]+)(\d+)\:([a-zA-Z]+)(\d+)\)/', $value, $ranges); // 獲得ranges範圍
if (empty($ranges[1])) {
return null;
}
合併單元格
- $merges = $sheet_obj->getMergeCells();
- 返回的是merge數據數組
$merges = $sheet_obj->getMergeCells();
if (empty($merges)) {
return null;
}
$merge_info = [];
foreach ($merges as $k => $merge) { //A1:E1
preg_match('/([a-zA-Z]+)(\d+)\:([a-zA-Z]+)(\d+)/i', $merge, $ranges); //獲得range範圍
//todo something
}
列寬和行高
//行高
$row_iteratior = $sheet_obj->getRowIterator();
$row_hights = [];//實際行高值
$i= 0;
foreach ($row_iteratior as $k => $row) {
$row_height = (int)($sheet_obj->getRowDimension($k)->getRowHeight());
$row_hights[$i] = $row_height;
$i++;
}
//列寬
$col_iteratior = $sheet_obj->getColumnIterator();
$col_widths = [];//實際列寬值
$i= 0;
foreach ($col_iteratior as $k => $col) {
$col_width = (int)($sheet_obj->getColumnDimension($k)->getWidth())*7;
$col_widths[$i] = $col_width;
$i++;
}
表格凍結
$freeze = $sheet_obj->getFreezePane(); // 返回 ie: A2
其他問題
空的sheeft的處理
- 有樣式,無數據
- 默認邊框和空白邊框的處理
橫座標處理
Excel的橫座標爲A、B…、AA、AB類型,需要轉化座標。
private function _get_sheet_col_key($label) {
$col_key_map = [];
$abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for ($i=0; $i < 600; $i++) {
$a = (int)($i / 26);
$b = $i % 26;
$column_label = '';
if ($a == 0) {
$column_label = $column_label . substr($abc, $b, 1);
} else {
$a = $a -1;
$column_label = substr($abc, $a, 1) . substr($abc, $b, 1);
}
$col_key_map[$column_label] = $i;
}
return $col_key_map[$label];
}