PHP讀取Excel資料

2021-09-21 14:21:39 字數 4456 閱讀 8141

承接上篇

設定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)) elseif($excel2007_reader->canread($file_path)) else

處理多sheet和遍歷內容
$table_object = $reader->load($file_path);

$all_sheets = $table_object->getallsheets();

$i = 0;

foreach ($all_sheets as $key => $sheet_obj)

}

處理樣式
$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)

$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)

$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)

$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)

$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)

$font_size = $font_object->getsize();

if (isset($font_size) && $font_size > 11)

$font_color = $font_object->getcolor()->getrgb();

if (isset($font_color) && $font_color != '000000')

$font_bold = $font_object->getbold();

if ($font_bold)

$font_italic = $font_object->getitalic();

if ($font_italic)

$font_underline = $font_object->getunderline();

if ($font_underline && $font_underline != phpexcel_style_font::underline_none)

$font_strikethrough = $font_object->getstrikethrough();

if ($font_strikethrough)

$horizontal = $alignment_object->gethorizontal();

if ($horizontal != 'general')

$vertical = $alignment_object->getvertical();

if ($vertical != 'bottom')

獲取資料
$cell_obj->getcalculatedvalue();//會處理公式和富文字的值 string

$cell_obj->getvalue();//不處理公式和富文字的值,存在多種型別

處理超連結

核心方法:$cell_obj->gethyperlink()->geturl();

$value = $cell_obj->getcalculatedvalue();

$url = $cell_obj->gethyperlink()->geturl();

$link = '' . $value . '';

處理公式
if (!$cell_obj->isformula()) 

$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]))

合併單元格
$merges = $sheet_obj->getmergecells();

if (empty($merges))

$merge_info = ;

foreach ($merges as $k => $merge)

列寬和行高
//行高

$row_iteratior = $sheet_obj->getrowiterator();

$row_hights = ;//實際行高值

$i= 0;

foreach ($row_iteratior as $k => $row)

//列寬

$col_iteratior = $sheet_obj->getcolumniterator();

$col_widths = ;//實際列寬值

$i= 0;

foreach ($col_iteratior as $k => $col)

**凍結

$freeze = $sheet_obj->getfreezepane(); // 返回 ie: a2

空的sheeft的處理

橫座標處理

excel的橫座標為a、b…、aa、ab型別,需要轉化座標。

private function _get_sheet_col_key($label)  else 

$col_key_map[$column_label] = $i;

} return $col_key_map[$label];

}

PHP讀取excel文件

php讀取excel文件 phpexcelreader比較輕量級,僅支援excel的讀取,實際上就是乙個reader。但是可惜的是不能夠支援excel 2007的格式 xlsx phpexcel比較強大,能夠將記憶體中的資料輸出成excel檔案,同時還能夠對excel做各種操作,下面主要介紹下如何使...

php讀取excel內容

使用php讀取到excel檔案中的內容 2.header content type text html charset utf 8 require once phpexcel classes phpexcel iofactory.php path test.xlsx objphpexcel phpe...

讀取Excel資料

方法2 相當簡單,excel就像資料庫,每個sheet就是乙個table.microsoft.jet.oledb驅動.之後是datareader迴圈,或dataset處理都非常簡單.注意 資料型別的轉換 region set connection string strconn provider mi...