php操作excel以及組建的使用

推薦閱讀PHP 百萬級(jí)數(shù)據(jù)導(dǎo)出方案(多 CSV 文件壓縮)
php有自帶函數(shù)fputcsv,fgetcsv是可以行操作,寫入和讀取excel數(shù)據(jù)的

提示 導(dǎo)出的數(shù)據(jù)可以直接寫成鍵值對(duì)數(shù)組的形式,即可得到統(tǒng)一的標(biāo)題

圖片.png
寫入
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

讀取
$fp = fopen('file.csv', 'r');
$data = fgetcsv($fp);

組件的使用

參考資料

1.基本

2.Excel::create('Filename', function($excel) {

    // Call writer methods here

});

從數(shù)組創(chuàng)建工作表

數(shù)組

從一個(gè)陣列中使用創(chuàng)建一個(gè)新文件->fromArray($source, $nullValue, $startCell, $strictNullComparison, $headingGeneration)的封板內(nèi)。

Excel::create('Filename', function($excel) {

    $excel->sheet('Sheetname', function($sheet) {

        $sheet->fromArray(array(
            array('data1', 'data2'),
            array('data3', 'data4')
        ));

    });

})->export('xls');

或者您可以使用->with()。

$sheet->with(array(
    array('data1', 'data2'),
    array('data3', 'data4')
));

如果你想傳遞閉包內(nèi)的變量,使用 use($data)

$data = array(
    array('data1', 'data2'),
    array('data3', 'data4')
);

Excel::create('Filename', function($excel) use($data) {

    $excel->sheet('Sheetname', function($sheet) use($data) {

        $sheet->fromArray($data);

    });

})->export('xls');

$sheet->fromArray($data, null, 'A1', false, false);
五個(gè)參數(shù)
1.表格里面的內(nèi)容
5。false則不現(xiàn)實(shí)標(biāo)題   標(biāo)題可以自動(dòng)生成

行操作

操縱某一行

更改單元格值

$sheet->row(1, array(
 'test1', 'test2'
));

// Manipulate 2nd row
$sheet->row(2, array(
    'test3', 'test4'
));

操作行單元格

$sheet->row(1, function($row) {
    $row->setBackground('#000000');
}); 

追加行

// Append row after row 2
$sheet->appendRow(2, array(
    'appended', 'appended'
));

// Append row as very last
$sheet->appendRow(array(
    'appended', 'appended'
));

前端行

// Add before first row
$sheet->prependRow(1, array(
    'prepended', 'prepended'
));

// Add as very first
$sheet->prependRow(array(
    'prepended', 'prepended'
));

附加多行

// Append multiple rows
$sheet->rows(array(
    array('test1', 'test2'),
    array('test3', 'test4')
));

// Append multiple rows
$sheet->rows(array(
    array('test5', 'test6'),
    array('test7', 'test8')
));

細(xì)胞操作

$sheet->cell('A1', function($cell) {

    // manipulate the cell
    $cell->setValue('data1');

});

$sheet->cells('A1:A5', function($cells) {

    // manipulate the range of cells

});

設(shè)置背景

要改變一系列單元格的背景,我們可以使用->setBackground($color, $type, $colorType)

// Set black background
$cells->setBackground('#000000');

更改字體

// Set with font color
$cells->setFontColor('#ffffff');

// Set font family
$cells->setFontFamily('Calibri');

// Set font size
$cells->setFontSize(16);

// Set font weight to bold
$cells->setFontWeight('bold');

// Set font
$cells->setFont(array(
    'family'     => 'Calibri',
    'size'       => '16',
    'bold'       =>  true
));

設(shè)置邊框

// Set all borders (top, right, bottom, left)
$cells->setBorder('solid', 'none', 'none', 'solid');

// Set borders with array
$cells->setBorder(array(
    'top'   => array(
        'style' => 'solid'
    ),
));

設(shè)置水平對(duì)齊

// Set alignment to center
$cells->setAlignment('center');
設(shè)置垂直對(duì)齊
// Set vertical alignment to middle
 $cells->setValignment('center');

設(shè)置垂直對(duì)齊

// Set vertical alignment to middle
 $cells->setValignment('center');

自動(dòng)尺寸

默認(rèn)情況下,導(dǎo)出的文件會(huì)自動(dòng)調(diào)整大小。要更改此行為,您可以更改配置或使用setter:

// Set auto size for sheet
$sheet->setAutoSize(true);

// Disable auto size for sheet
$sheet->setAutoSize(false);

// Disable auto size for columns
$sheet->setAutoSize(array(
    'A', 'C'
));

列合并

合并細(xì)胞

要合并的單元格區(qū)域,請(qǐng)使用->mergeCells($range)。

$sheet->mergeCells('A1:E1');

合并列和行

要合并列和行,使用->setMergeColumn($array)。

$sheet->setMergeColumn(array(
    'columns' => array('A','B','C','D'),
    'rows' => array(
        array(2,3),
        array(5,11),
    )
));
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 使用首先需要了解他的工作原理 1.POI結(jié)構(gòu)與常用類 (1)創(chuàng)建Workbook和Sheet (2)創(chuàng)建單元格 (...
    長(zhǎng)城ol閱讀 8,743評(píng)論 2 25
  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯(cuò)誤還是無法避免 以后資料會(huì)慢慢更新 大...
    數(shù)據(jù)革命閱讀 13,250評(píng)論 2 33
  • 轉(zhuǎn)自鏈接 目錄 1.認(rèn)識(shí)NPOI 2.使用NPOI生成xls文件 2.1創(chuàng)建基本內(nèi)容 2.1.1創(chuàng)建Workboo...
    腿毛褲閱讀 11,159評(píng)論 1 3
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評(píng)論 19 139
  • 上次給大家分享了《2017年最全的excel函數(shù)大全(2)——web函數(shù)》,這次分享給大家查找和引用函數(shù)(上)。 ...
    幸福的耗子閱讀 5,006評(píng)論 1 5

友情鏈接更多精彩內(nèi)容