### Quick Start: Create Excel File with PHP-Xlswriter Source: https://context7_llms A basic guide to getting started with PHP-Xlswriter, demonstrating how to create a new Excel file and write data to it. ```php addSheet(Excel::createSheet('Sheet1')); $excel->save(__DIR__); ?> ``` -------------------------------- ### Install PHP-Xlswriter Source: https://context7_llms Instructions for installing the PHP-Xlswriter library, including recommendations for PECL and support for various operating systems like Mac, Ubuntu, Alpine, and Windows. ```bash pecl install xlswriter ``` -------------------------------- ### Build xlswriter PHP Extension with Dockerfile Source: https://xlswriter-docs.viest.me/zh-cn/an-zhuang/docker This Dockerfile uses a multi-stage build. The first stage (`build`) installs necessary build tools, downloads, compiles, and installs the xlswriter PHP extension. The second stage starts from a clean Alpine PHP image and copies the compiled extension from the build stage. ```Dockerfile FROM alpine:3.9.6 as build # 构建xlswriter扩展,根据自身需要替换版本号 ENV XLSWRITER_VERSION 1.3.4.1 RUN apk update \ && apk add --no-cache php7-pear php7-dev zlib-dev re2c gcc g++ make curl \ && curl -fsSL "https://pecl.php.net/get/xlswriter-${XLSWRITER_VERSION}.tgz" -o xlswriter.tgz \ && mkdir -p /tmp/xlswriter \ && tar -xf xlswriter.tgz -C /tmp/xlswriter --strip-components=1 \ && rm xlswriter.tgz \ && cd /tmp/xlswriter \ && phpize && ./configure --enable-reader && make && make install #------------------------------------------------------------------------------------------- FROM alpine:3.9.6 # 根据自身需要,添加其它软件 RUN apk update && apk add --no-cache php COPY --from=build /usr/lib/php7/modules/xlswriter.so /usr/lib/php7/modules/xlswriter.so RUN echo "extension=xlswriter.so" > /etc/php7/conf.d/xlswriter.ini ``` -------------------------------- ### xlswriter File Creation and Reading Example (PHP) Source: https://xlswriter-docs.viest.me/en/kuai-su-shang-shou/read-file-full This PHP example demonstrates how to use the xlswriter library to create an Excel file, set headers, and then read the data back. It initializes the Excel object with a configuration path and outputs the file path after setting the header. ```php $config = ['path' => './tests']; $excel = new \Vtiful\Kernel\Excel($config); $filePath = $excel->fileName('tutorial.xlsx') ->header(['Item', 'Cost']) ->output(); $data = $excel->openFile('tutorial.xlsx') ->openSheet() ->getSheetData(); var_dump($data) ``` -------------------------------- ### Install PHP Build Dependencies (zlib) Source: https://xlswriter-docs.viest.me/en/an-zhuang/windows This snippet shows how to download, extract, and compile the zlib library, a common dependency for building PHP extensions on Windows. It uses 7-Zip for extraction and CMake for building. ```bash cd PHP_BUILD_PATH/deps DownloadFile http://zlib.net/zlib-1.2.11.tar.gz 7z x zlib-1.2.11.tar.gz > NUL 7z x zlib-1.2.11.tar > NUL cd zlib-1.2.11 cmake -G "Visual Studio 14 2015" -DCMAKE_BUILD_TYPE="Release" -DCMAKE_C_FLAGS_RELEASE="/MT" cmake --build . --config "Release" ``` -------------------------------- ### PHP Example: Read File with xlswriter Source: https://xlswriter-docs.viest.me/en/reader/read-file-cursor This PHP example demonstrates how to use the xlswriter library to create an Excel file, set headers, and read rows. It initializes the Excel object, sets the file name and headers, and then opens the file to read its contents row by row. ```php $config = ['path' => './tests']; $excel = new \Vtiful\Kernel\Excel($config); $filePath = $excel->fileName('tutorial.xlsx') ->header(['Item', 'Cost']) ->output(); $excel->openFile('tutorial.xlsx') ->openSheet(); var_dump($excel->nextRow()); // ['Item', 'Cost'] var_dump($excel->nextRow()); // NULL ``` -------------------------------- ### PHP Example: Setting Gridlines in Excel Source: https://xlswriter-docs.viest.me/en/gong-zuo-biao/gridlines Demonstrates how to use the gridline function to set worksheet gridlines. This example initializes the Excel writer, sets the file name, adds headers, hides all gridlines, populates data, and outputs the Excel file. ```PHP $config = ['path' =>'./tests']; $excel = new \Vtiful\Kernel\Excel($config); $fileObject = $excel->fileName("tutorial01.xlsx"); $fileObject->header(['name','age']) ->gridline(\Vtiful\Kernel\Excel::GRIDLINES_HIDE_ALL) // Set the grid line of the worksheet ->data([ ['viest', 21], ['viest', 22], ['viest', 23], ]) ->output(); ``` -------------------------------- ### Download Excel File in PHP Source: https://xlswriter-docs.viest.me/en/download This snippet demonstrates how to generate an Excel file and trigger its download in PHP. It includes setting up the temporary directory, creating an Excel object, writing data (commented out), outputting the file, and setting appropriate HTTP headers for the download. It also handles temporary file cleanup. ```php function getTmpDir(): string { $tmp = ini_get('upload_tmp_dir'); if ($tmp !== False && file_exists($tmp)) { return realpath($tmp); } return realpath(sys_get_temp_dir()); } $config = [ 'path' => getTmpDir() . '/', ]; $fileName = 'tutorial01.xlsx'; $xlsxObject = new \Vtiful\Kernel\Excel($config); // Init File $fileObject = $xlsxObject->fileName($fileName); // Writing data to a file ...... // Outptu $filePath = $fileObject->output(); // Set Header header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Disposition: attachment;filename="' . $fileName . '"'); header('Cache-Control: max-age=0'); if (copy($filePath, 'php://output') === false) { // Throw exception } // Delete temporary file @unlink($filePath); ``` -------------------------------- ### PHP Example: Reading Excel Rows with Data Types Source: https://xlswriter-docs.viest.me/en/reader/data-type This PHP example demonstrates how to use the VtifulKernelExcel class to create an Excel file, write a header, and then read rows specifying the data type for each cell. It requires the xlswriter library and assumes a configuration with a test path. ```php $config = ['path' => './tests']; $excel = new \Vtiful\Kernel\Excel($config); $filePath = $excel->fileName('tutorial.xlsx') ->header(['Item', 'Cost']) ->output(); $excel->openFile('tutorial.xlsx') ->openSheet(); // When reading each row of cell data, you can specify each cell data type to read Var_dump($excel->nextRow([ \Vtiful\Kernel\Excel::TYPE_STRING, \Vtiful\Kernel\Excel::TYPE_STRING ])); ``` -------------------------------- ### Example Usage of insertDate Source: https://xlswriter-docs.viest.me/en/dan-yuan-ge/cha-ru-date Demonstrates how to use the insertDate function to write a header and a date value to an Excel file using the xlswriter library. ```php $excel = new \Vtiful\Kernel\Excel($config); $dateFile = $excel->fileName("free.xlsx") ->header(['date']); $dateFile->insertDate(1, 0, time(), 'mmm d yyyy hh:mm AM/PM'); $textFile->output(); ``` -------------------------------- ### Example: Inserting Text and Numeric Data Source: https://xlswriter-docs.viest.me/en/dan-yuan-ge/cha-ru-wen-zi Demonstrates how to create an Excel file, insert text ('viest') into the first column and numeric data (10000) with a specific numeric format ('#,##0') into the second column for multiple rows. ```php $excel = new \Vtiful\Kernel\Excel($config); $textFile = $excel->fileName("free.xlsx") ->header(['name', 'money']); for ($index = 0; $index < 10; $index++) { $textFile->insertText($index+1, 0, 'viest'); $textFile->insertText($index+1, 1, 10000, '#,##0'); // #,##0 is the cell data style } $textFile->output(); ``` -------------------------------- ### Create Percentage Stacked Bar Chart with xlswriter Source: https://xlswriter-docs.viest.me/zh-cn/tu-biao/tiao-xing-tu This example shows how to create a percentage stacked bar chart using the xlswriter PHP library. The process involves setting up the Excel environment, preparing the data, instantiating a percentage stacked bar chart, defining its series, axes, and title, and finally embedding the chart into an Excel spreadsheet. ```php './tests', ]; $dataHeader = [ 'Number', 'Batch 1', 'Batch 2', ]; $dataRows = [ [2, 10, 30], [3, 40, 60], [4, 50, 70], [5, 20, 50], [6, 10, 40], [7, 50, 30], ]; $fileObject = new VtifulKernelExcel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); $chart = new VtifulKernelChart($fileHandle, VtifulKernelChart::CHART_BAR_STACKED_PERCENT); $chartResource = $chart // series(string $value [, string $category]) ->series('=Sheet1!$B$2:$B$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$B$1') ->series('=Sheet1!$C$2:$C$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$C$1') ->axisNameX('Test number') ->axisNameY('Sample length (mm)') ->title('Results of sample analysis') ->style(13) ->toResource(); $filePath = $fileObject ->header($dataHeader) ->data($dataRows) ->insertChart(0, 4, $chartResource) ->output(); ``` -------------------------------- ### PHP-Xlswriter: Get Version Source: https://context7_llms Shows how to retrieve the current version of the PHP-Xlswriter library. ```php ``` -------------------------------- ### Create Stacked Bar Chart with PHP Source: https://xlswriter-docs.viest.me/en/tu-biao/bar-chart This code example illustrates how to generate a stacked bar chart using the xlswriter PHP library. It covers the necessary steps for file initialization, data preparation, chart object creation, series and axis configuration, and inserting the chart into the Excel file. ```php './tests', ]; $dataHeader = [ 'Number', 'Batch 1', 'Batch 2', ]; $dataRows = [ [2, 10, 30], [3, 40, 60], [4, 50, 70], [5, 20, 50], [6, 10, 40], [7, 50, 30], ]; $fileObject = new VtifulKernelExcel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); $chart = new VtifulKernelChart($fileHandle, VtifulKernelChart::CHART_BAR_STACKED); $chartResource = $chart // series(string $value [, string $category]) ->series('=Sheet1!$B$2:$B$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$B$1') ->series('=Sheet1!$C$2:$C$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$C$1') ->axisNameX('Test number') ->axisNameY('Sample length (mm)') ->title('Results of sample analysis') ->style(12) ->toResource(); $filePath = $fileObject ->header($dataHeader) ->data($dataRows) ->insertChart(0, 4, $chartResource) ->output(); ``` -------------------------------- ### Digital Style Formats for Excel Cells Source: https://xlswriter-docs.viest.me/en/dan-yuan-ge/cha-ru-wen-zi Provides examples of common digital style formats that can be applied to cells for displaying numeric data, such as decimal places and currency. ```php "0.000" "#,##0" "#,##0.00" "0.00" "0 \"dollar and\".00 \"cents\"" ``` -------------------------------- ### Insert Formula in Excel Sheet (PHP) Source: https://xlswriter-docs.viest.me/en/dan-yuan-ge/cha-ru-gong-shi Demonstrates how to insert a SUM formula into an Excel sheet using the xlswriter library. The example initializes an Excel writer, inserts text and numeric data, and then uses `insertFormula` to calculate the sum of a range of cells. ```php $excel = new \Vtiful\Kernel\Excel($config); $freeFile = $excel->fileName("free.xlsx") ->header(['name', 'money']); for($index = 1; $index < 10; $index++) { $textFile->insertText($index, 0, 'viest'); $textFile->insertText($index, 1, 10); } $textFile->insertText(12, 0, "Total"); $textFile->insertFormula(12, 1, '=SUM(B2:B11)'); $freeFile->output(); ``` -------------------------------- ### Set Global Row Read Type and Get Sheet Data Source: https://xlswriter-docs.viest.me/en/reader/set-type Opens an existing Excel file, sets the data type for each column in the sheet, and retrieves the sheet data. The setType method is crucial for interpreting data correctly upon reading. The example demonstrates setting types for string, integer, and timestamp columns. ```php $data = $excel->openFile('tutorial.xlsx') ->openSheet() ->setType([ \Vtiful\Kernel\Excel::TYPE_STRING, \Vtiful\Kernel\Excel::TYPE_INT, \Vtiful\Kernel\Excel::TYPE_TIMESTAMP, ]) ->getSheetData(); var_dump($data); ``` -------------------------------- ### Prepare Test Data with xlswriter Source: https://xlswriter-docs.viest.me/en/reader/skip-empty-row This snippet shows how to initialize the xlswriter library with a configuration path and then write test data, including headers and data rows, to an Excel file named 'tutorial.xlsx'. ```php $config = ['path' => './tests']; $excel = new \Vtiful\Kernel\Excel($config); // Write test data $filePath = $excel->fileName('tutorial.xlsx') ->header(['', 'Cost']) ->data([ [], ['viest', ''] ]) ->output(); ``` -------------------------------- ### Build Docker Image Source: https://xlswriter-docs.viest.me/zh-cn/an-zhuang/docker This command builds a Docker image using the specified Dockerfile and tags it with `viest/xlswriter:1.3.4.1`. ```bash docker build -f Dockerfile -t viest/xlswriter:1.3.4.1 . ``` -------------------------------- ### PHP-Xlswriter: Get Author Source: https://context7_llms Demonstrates how to retrieve the author information for the PHP-Xlswriter library. ```php ``` -------------------------------- ### Compile PHP Extension with xlswriter Source: https://xlswriter-docs.viest.me/en/an-zhuang/windows This snippet demonstrates how to compile a PHP extension, specifically 'php-ext-excel-export' with xlswriter support. It involves cloning the repository, initializing submodules, preparing the extension using phpize, configuring the build with dependencies, and finally compiling with nmake. ```bash cd PHP_PATH/ext git clone https://github.com/viest/php-ext-excel-export.git cd php-ext-excel-export git submodule update --init phpize configure.bat --with-xlswriter --with-extra-libs=PATH\zlib-1.2.11\Release --with-extra-includes=PATH\zlib-1.2.11 nmake ``` -------------------------------- ### 编译xlswriter以启用读取功能 Source: https://xlswriter-docs.viest.me/zh-cn/reader/data-type 在编译xlswriter库时,需要添加 `--enable-reader` 选项来启用数据读取功能。这确保了库能够处理文件读取操作。 ```bash ./configure --enable-reader ``` -------------------------------- ### Excel Date Format Characters Source: https://xlswriter-docs.viest.me/en/dan-yuan-ge/cha-ru-date Provides examples of common date format characters used with the insertDate function, referencing Microsoft Excel documentation for more styles. ```php "mm/dd/yy" "mmm d yyyy" "d mmmm yyyy" "dd/mm/yyyy hh:mm AM/PM" ``` -------------------------------- ### EasySwoole Framework Overview Source: https://xlswriter-docs.viest.me/en-1 EasySwoole is a distributed PHP framework based on Swoole Server, designed for APIs. It offers high performance by eliminating process evoke and file loading overheads. It supports multiple protocols and provides various components for development. ```text ______ _____ _ | ____| / ____| | | | |__ __ _ ___ _ _ | (___ __ __ ___ ___ | | ___ | __| / _` | / __| | | | | \___ \ \ \ /\ / / / _ \ / _ \ | | / _ \ | |____ | (_| | \__ \ | |_| | ____) | \ V V / | (_) | | (_) | | | | __/ |______| \__,_| |___/ \__, | |_____/ \_/\_/ \___/ \___/ |_| \___| __/ | |___/ ``` -------------------------------- ### Apply Number Formatting - PHP Source: https://xlswriter-docs.viest.me/en/yang-shi-lie-biao/number Demonstrates how to create and apply a number style using the VtifulKernelFormat class in PHP. This includes setting the format string and applying it to a specific column. ```php $config = [ 'path' => './tests' ]; $fileObject = new \Vtiful\Kernel\Excel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); // Create a style resource $format = new \Vtiful\Kernel\Format($fileHandle); $numberStyle = $format->number('#,##0')->toResource(); $filePath = $fileObject->header(['name', 'balance']) ->data([ ['viest', 10000], ['wjx', 100000] ]) ->setColumn('B:B', 50, $numberStyle) // Apply style ->output(); ``` -------------------------------- ### Read Excel Data Skipping Empty Rows Source: https://xlswriter-docs.viest.me/en/reader/skip-empty-row This example demonstrates reading an entire Excel file ('tutorial.xlsx') and its sheet ('Sheet1') while skipping any empty rows. It utilizes the `VtifulKernelExcel::SKIP_EMPTY_ROW` constant for this purpose. ```php // Read the full amount of data // Use \Vtiful\Kernel\Excel::SKIP_EMPTY_ROW to ignore blank lines $data = $excel->openFile('tutorial.xlsx') ->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_ROW) ->getSheetData(); ``` -------------------------------- ### Get Worksheet List in PHP Source: https://xlswriter-docs.viest.me/en/reader/sheet_list Retrieves a list of all worksheet names from an opened Excel file using the xlswriter library. This function is essential for iterating through and accessing individual sheets within a workbook. ```php $sheetList = $excel->openFile('tutorial.xlsx')->sheetList(); Foreach ($sheetList as $sheetName) { echo 'Sheet Name:' . $sheetName . PHP_EOL; // Get the worksheet data by the worksheet name $sheetData = $excel ->openSheet($sheetName) ->getSheetData(); var_dump($sheetData); } ``` -------------------------------- ### Swoole Framework Overview Source: https://xlswriter-docs.viest.me/en-1 Swoole is a high-performance PHP network framework that enhances R&D efficiency by providing built-in PHP coroutine and async support, multiple threads I/O modules. It allows developers to write applications using sync, async, or coroutine APIs. ```text ![](https://665362659-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lmm53W9Z0JOSkMcC6BM%2F-M5UmKn6h8NM9HsD43PS%2F-M5UmPKSi1LIfdT5fwSW%2Fswoole.png?generation=1587523738733175&alt=media) ``` -------------------------------- ### Set Text Color with xlswriter Source: https://xlswriter-docs.viest.me/zh-cn/yang-shi-lie-biao/font-color Demonstrates how to set the font color for cells in an Excel file using the xlswriter library. It shows examples of using both built-in color constants (like COLOR_ORANGE) and RGB hexadecimal values to define the color style. ```php $config = [ 'path' => './tests' ]; $fileObject = new \Vtiful\Kernel\Excel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); // 使用 扩展自带颜色常量 创建样式资源 $format = new \Vtiful\Kernel\Format($fileHandle); $colorStyle = $format->fontColor(\Vtiful\Kernel\Format::COLOR_ORANGE)->toResource(); // 使用 RGB16进制数 创建样式资源 $format = new \Vtiful\Kernel\Format($fileHandle); $colorStyle = $format->fontColor(0xFF69B4)->toResource(); $filePath = $fileObject->header(['name', 'age']) ->data([ ['viest', 21], ['wjx', 21] ]) ->setRow('A1', 50, $colorStyle) // 应用样式 ->output(); ``` -------------------------------- ### EasySwoole ASCII Art Source: https://xlswriter-docs.viest.me/zh-cn-1 This snippet displays an ASCII art representation of the EasySwoole logo. It is purely decorative and does not contain executable code. ```text ______ _____ _ | ____| / ____| | | | |__ __ _ ___ _ _ | (___ __ __ ___ ___ | | ___ | __| / _` | / __| | | | | \___ \ \ \ /\ / / / _ \ / _ \ | | / _ \ | |____ | (_| | \__ \ | |_| | ____) | \ V V / | (_) | | (_) | | | | __/ |______| \__,_| |___/ \__, | |_____/ \_/\_/ \___/ \___/ |_| \___| __/ | |___/ ``` -------------------------------- ### Create Percentage Stacked Bar Chart with PHP Source: https://xlswriter-docs.viest.me/en/tu-biao/bar-chart This PHP code snippet demonstrates the creation of a percentage stacked bar chart using the xlswriter library. It details the process of initializing the Excel file, preparing the data, instantiating the chart with the correct type, configuring chart elements, and embedding it within the spreadsheet. ```php './tests', ]; $dataHeader = [ 'Number', 'Batch 1', 'Batch 2', ]; $dataRows = [ [2, 10, 30], [3, 40, 60], [4, 50, 70], [5, 20, 50], [6, 10, 40], [7, 50, 30], ]; $fileObject = new VtifulKernelExcel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); $chart = new VtifulKernelChart($fileHandle, VtifulKernelChart::CHART_BAR_STACKED_PERCENT); $chartResource = $chart // series(string $value [, string $category]) ->series('=Sheet1!$B$2:$B$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$B$1') ->series('=Sheet1!$C$2:$C$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$C$1') ->axisNameX('Test number') ->axisNameY('Sample length (mm)') ->title('Results of sample analysis') ->style(13) ->toResource(); $filePath = $fileObject ->header($dataHeader) ->data($dataRows) ->insertChart(0, 4, $chartResource) ->output(); ``` -------------------------------- ### Create and Write Data to Excel File (PHP) Source: https://xlswriter-docs.viest.me/en/kuai-su-shang-shou/create-file This snippet demonstrates how to create a new Excel file named 'tutorial01.xlsx' with a worksheet named 'sheet1'. It sets the file path, adds a header row, and then populates the worksheet with sample data. The output method generates the file. ```php $config = ['path' => '/home/viest']; $excel = new \Vtiful\Kernel\Excel($config); // fileName will automatically create a worksheet, // you can customize the worksheet name, the worksheet name is optional $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1') ->header(['Item', 'Cost']) ->data([ ['Rent', 1000], ['Gas', 100], ['Food', 300], ['Gym', 50], ]) ->output(); ``` -------------------------------- ### PHP: Export with Fixed Memory Mode Source: https://xlswriter-docs.viest.me/en/nei-cun/gu-ding-nei-cun-mo-shi Demonstrates exporting data to an Excel file using the fixed memory mode in PHP. This mode optimizes memory usage by limiting it to a single row. The example shows how to initialize the Excel writer, set the file name, apply formatting like bold to cells, and output the data. ```PHP $config = ['path' => './tests']; $excel = new \Vtiful\Kernel\Excel($config); $fileObject = $excel->constMemory('tutorial01.xlsx'); $fileHandle = $fileObject->getHandle(); $format = new \Vtiful\Kernel\Format($fileHandle); $boldStyle = $format->bold()->toResource(); $fileObject->header(['name', 'age']) ->data([['viest', 21]]) ->setRow('A1', 10, $boldStyle) ->output(); ``` -------------------------------- ### Compile xlswriter with Reader Support Source: https://xlswriter-docs.viest.me/en/kuai-su-shang-shou/read-file-full This snippet shows the command to compile the xlswriter library with the necessary flag to enable reader functionality. Ensure your extended version is at least 1.2.7. ```bash ./configure --enable-reader ``` -------------------------------- ### Create Doughnut Chart in PHP Source: https://xlswriter-docs.viest.me/en/tu-biao/doughnut This PHP code demonstrates how to create a doughnut chart using the xlswriter library. It sets up data, configures the chart with a title and series, and inserts it into an Excel file named 'tutorial.xlsx'. ```php './tests', ]; $dataHeader = [ 'Category', 'Values', ]; $dataRows = [ ['Glazed', 50], ['Chocolate', 35], ['Cream', 15], ]; $fileObject = new VtifulKernelExcel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); $chart = new VtifulKernelChart($fileHandle, VtifulKernelChart::CHART_DOUGHNUT); $chartResource = $chart // series(string $value [, string $category]) ->series('=Sheet1!$B$2:$B$4', '=Sheet1!$A$2:$A$4') ->seriesName('Doughnut sales data') ->title('Popular Doughnut Types') ->style(10) ->toResource(); $filePath = $fileObject ->header($dataHeader) ->data($dataRows) ->insertChart(0, 4, $chartResource) ->output(); ``` -------------------------------- ### PHP - 解除工作表编辑保护 Source: https://xlswriter-docs.viest.me/zh-cn/protection/unlock 使用 unlocked() 函数创建解除编辑保护的样式,并将其应用于工作表的特定行。此代码演示了如何设置文件配置、创建 Excel 对象、获取文件句柄、创建格式对象、应用 unlocked() 样式,以及将样式应用于工作表的第二行,最后添加工作表保护并输出文件。 ```php $config = [ 'path' => './' ]; $fileObject = new \Vtiful\Kernel\Excel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); // 创建解除保护样式 $format = new \Vtiful\Kernel\Format($fileHandle); $unlockedStyle = $format->unlocked()->toResource(); $filePath = $fileObject->header(['name', 'age']) ->data([ ['wjx', 21] ]) ->setRow('A2', 50, $unlockedStyle) // 将工作表中的第二行解除编辑保护 ->protection() // 工作表添加编辑保护 ->output(); ``` -------------------------------- ### 设置行高和样式 Source: https://xlswriter-docs.viest.me/zh-cn/dan-yuan-ge/hang-dan-yuan-ge-yang-shi 使用 `setRow` 函数设置指定范围的行高,并应用预定义的样式(例如加粗)。此函数影响整行,即使指定的范围只覆盖了部分单元格。 ```php $config = ['path' => './tests']; $excel = new \Vtiful\Kernel\Excel($config); $fileObject = $excel->fileName('tutorial01.xlsx'); $fileHandle = $fileObject->getHandle(); $format = new \Vtiful\Kernel\Format($fileHandle); $boldStyle = $format->bold()->toResource(); $fileObject->header(['name', 'age']) ->data([['viest', 21]]) ->setRow('A1', 20, $boldStyle) ->output(); ``` -------------------------------- ### Create Stacked Bar Chart with xlswriter Source: https://xlswriter-docs.viest.me/zh-cn/tu-biao/tiao-xing-tu This code snippet illustrates how to generate a stacked bar chart using the xlswriter PHP library. It covers the initialization of an Excel file, data preparation, the creation of a stacked bar chart object, and the configuration of its visual elements like series, axes, and title before embedding it into an Excel file. ```php './tests', ]; $dataHeader = [ 'Number', 'Batch 1', 'Batch 2', ]; $dataRows = [ [2, 10, 30], [3, 40, 60], [4, 50, 70], [5, 20, 50], [6, 10, 40], [7, 50, 30], ]; $fileObject = new VtifulKernelExcel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); $chart = new VtifulKernelChart($fileHandle, VtifulKernelChart::CHART_BAR_STACKED); $chartResource = $chart // series(string $value [, string $category]) ->series('=Sheet1!$B$2:$B$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$B$1') ->series('=Sheet1!$C$2:$C$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$C$1') ->axisNameX('Test number') ->axisNameY('Sample length (mm)') ->title('Results of sample analysis') ->style(12) ->toResource(); $filePath = $fileObject ->header($dataHeader) ->data($dataRows) ->insertChart(0, 4, $chartResource) ->output(); ``` -------------------------------- ### 设置列样式 (PHP) Source: https://xlswriter-docs.viest.me/zh-cn/dan-yuan-ge/lie-dan-yuan-ge-yang-shi 使用 `setColumn` 函数设置指定列的宽度和样式。该函数接受列范围、宽度和格式处理器作为参数。示例展示了如何设置第一列的宽度并应用粗体样式。 ```PHP $config = ['path' => './tests']; $excel = new \Vtiful\Kernel\Excel($config); $fileObject = $excel->fileName('tutorial01.xlsx'); $fileHandle = $fileObject->getHandle(); $format = new \Vtiful\Kernel\Format($fileHandle); $boldStyle = $format->bold()->toResource(); $fileObject->header(['name', 'age']) ->data([['viest', 21]]) ->setColumn('A:A', 200, $boldStyle) ->output(); ``` -------------------------------- ### PHP Excel 数字格式化 Source: https://xlswriter-docs.viest.me/zh-cn/yang-shi-lie-biao/number 使用 xlswriter 库在 PHP 中为 Excel 文件中的数字列应用自定义格式。此代码演示了如何创建 Excel 文件对象,定义数字样式(例如千位分隔符和两位小数),然后将该样式应用于数据列。 ```php $config = [ 'path' => './tests' ]; $fileObject = new \Vtiful\Kernel\Excel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); // 创建样式资源 $format = new \Vtiful\Kernel\Format($fileHandle); $numberStyle = $format->number('#,##0')->toResource(); $filePath = $fileObject->header(['name', 'balance']) ->data([ ['viest', 10000], ['wjx', 100000] ]) ->setColumn('B:B', 50, $numberStyle) // 应用样式 ->output(); ``` -------------------------------- ### Prepare Excel File with Header and Data Source: https://xlswriter-docs.viest.me/en/reader/set-type Prepares an Excel file by setting a header row and inserting data. It initializes the Excel writer with a configuration, specifies the output filename, defines the header columns, and adds a row of data. The insertDate function is used to format a timestamp into a date. ```php $config = ['path' => './tests']; $excel = new \Vtiful\Kernel\Excel($config); $filePath = $excel->fileName('tutorial.xlsx') ->header(['Name', 'Age', 'Date']) ->data([ ['Viest', 24] ]) ->insertDate(1, 2, 1568877706) ->output(); ``` -------------------------------- ### Create Area Chart in PHP using xlswriter Source: https://xlswriter-docs.viest.me/zh-cn/tu-biao/mian-ji-tu This PHP code snippet demonstrates how to create an area chart in an Excel file using the xlswriter library. It involves initializing the Excel file, defining multiple data series for the chart, setting the chart's style, adding X and Y axis titles, setting the chart title, and finally inserting the chart into the worksheet before outputting the file. ```php $config = ['path' => './tests']; $fileObject = new \Vtiful\Kernel\Excel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); $chart = new \Vtiful\Kernel\Chart($fileHandle, \Vtiful\Kernel\Chart::CHART_AREA); $chartResource = $chart ->series('=Sheet1!$B$2:$B$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$B$1') ->series('=Sheet1!$C$2:$C$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$C$1') ->style(11)// 值为 1 - 48,可参考 Excel 2007 "设计" 选项卡中的 48 种样式 ->axisNameX('Test number') // 设置 X 轴名称 ->axisNameY('Sample length (mm)') // 设置 Y 轴名称 ->title('Results of sample analysis') // 设置图表 Title ->toResource(); $filePath = $fileObject->header(['Number', 'Batch 1', 'Batch 2']) ->data([ [2, 40, 30], [3, 40, 25], [4, 50, 30], [5, 30, 10], [6, 25, 5], [7, 50, 10], ])->insertChart(0, 3, $chartResource)->output(); ``` -------------------------------- ### PHP: Switch Worksheet with checkoutSheet Source: https://xlswriter-docs.viest.me/zh-cn/gong-zuo-biao/qie-huan-gong-zuo-biao Demonstrates how to use the checkoutSheet function to switch to a specific worksheet ('Sheet1') in an Excel file created with the ExcelWriter library. It shows the process of creating an Excel file, adding data to multiple sheets, and then switching back to the default sheet to append more data. ```php $config = [ 'path' => './tests' ]; $excel = new \Vtiful\Kernel\Excel($config); $fileObject = $excel->fileName("tutorial01.xlsx"); $fileObject->header(['name', 'age']) ->data([ ['viest', 21], ['viest', 22], ['viest', 23], ]); // 添加工作表,并插入数据 $fileObject->addSheet('twoSheet') ->header(['name', 'age']) ->data([['vikin', 22]]); // 切换回默认工作表,并追加数据 $fileObject->checkoutSheet('Sheet1') ->data([['sheet1']]); $filePath = $fileObject->output(); ``` -------------------------------- ### Compile xlswriter with Reader Enabled Source: https://xlswriter-docs.viest.me/en/reader/read-file-full This snippet shows the command to compile the xlswriter library with the `--enable-reader` flag, which is necessary for file reading functionality. ```bash ./configure --enable-reader ``` -------------------------------- ### Create Default Bar Chart with xlswriter Source: https://xlswriter-docs.viest.me/zh-cn/tu-biao/tiao-xing-tu This snippet demonstrates how to create a default bar chart using the xlswriter PHP library. It involves setting up Excel file configuration, defining data headers and rows, creating a chart object for a bar chart, configuring series, axis labels, and title, and then inserting the chart into the Excel file. ```php './tests', ]; $dataHeader = [ 'Number', 'Batch 1', 'Batch 2', ]; $dataRows = [ [2, 10, 30], [3, 40, 60], [4, 50, 70], [5, 20, 50], [6, 10, 40], [7, 50, 30], ]; $fileObject = new VtifulKernelExcel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); $chart = new VtifulKernelChart($fileHandle, VtifulKernelChart::CHART_BAR); $chartResource = $chart // series(string $value [, string $category]) ->series('=Sheet1!$B$2:$B$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$B$1') ->series('=Sheet1!$C$2:$C$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$C$1') ->axisNameX('Test number') ->axisNameY('Sample length (mm)') ->title('Results of sample analysis') ->style(11) ->toResource(); $filePath = $fileObject ->header($dataHeader) ->data($dataRows) ->insertChart(0, 4, $chartResource) ->output(); ``` -------------------------------- ### PHP-Xlswriter: Combination Style Source: https://context7_llms Demonstrates how to apply a combination of styles (e.g., font, color, alignment) to a cell using PHP-Xlswriter. ```php addSheet(Excel::createSheet('Sheet1')); $style = $excel->addStyle([ 'font-size' => 12, 'font-weight' => 'bold', 'font-color' => '#0000FF', 'align' => 'center', 'valign' => 'center' ]); $sheet->writeText('A1', 'Styled Text', $style); $excel->save(__DIR__); ?> ``` -------------------------------- ### Create Area Chart in PHP using xlswriter Source: https://xlswriter-docs.viest.me/en/tu-biao/area-map This snippet demonstrates how to create an area chart in an Excel file using the xlswriter PHP library. It involves setting up the Excel file, defining chart series with data and names, customizing the chart's style, axis labels, and title, and finally inserting the chart into the file. ```php $config = ['path' => './tests']; $fileObject = new \Vtiful\Kernel\Excel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle = $fileObject->getHandle(); $chart = new \Vtiful\Kernel\Chart($fileHandle, \Vtiful\Kernel\Chart::CHART_AREA); $chartResource = $chart ->series('=Sheet1!$B$2:$B$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$B$1') ->series('=Sheet1!$C$2:$C$7', '=Sheet1!$A$2:$A$7') ->seriesName('=Sheet1!$C$1') ->style(11)// Values ​​1 - 48, refer to 48 styles in the Excel 2007 Design tab ->axisNameX('Test number') // Set the X axis name ->axisNameY('Sample length (mm)') // Set the Y axis name ->title('Results of sample analysis') // Set the chart Title ->toResource(); $filePath = $fileObject->header(['Number', 'Batch 1', 'Batch 2']) ->data([ [2, 40, 30], [3, 40, 25], [4, 50, 30], [5, 30, 10], [6, 25, 5], [7, 50, 10], ])->insertChart(0, 3, $chartResource)->output(); ```