php - 使用 PHP 将整个 excel 文件显示为表格
问题描述
我有一个包含多张工作表的 excel 文件。我想使用 PHP 将 excel 文件中的数据显示为表格而不插入数据库。
解决方案
您可以使用 simplexlsx PHP 库来做到这一点
下面是库代码
https://github.com/fahadpatel/simplexlxs.git
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
require_once __DIR__.'/../src/SimpleXLSX.php';
echo '<h1>Read several sheets</h1>';
if ( $xlsx = SimpleXLSX::parse('countries_and_population.xlsx')) {
echo '<pre>'.print_r( $xlsx->sheetNames(), true ).'</pre>';
echo '<table cellpadding="10">
<tr><td valign="top">';
// output worsheet 1
$dim = $xlsx->dimension();
$num_cols = $dim[0];
$num_rows = $dim[1];
echo '<h2>'.$xlsx->sheetName(0).'</h2>';
echo '<table border=1>';
foreach ( $xlsx->rows( 1 ) as $r ) {
echo '<tr>';
for ( $i = 0; $i < $num_cols; $i ++ ) {
echo '<td>' . ( ! empty( $r[ $i ] ) ? $r[ $i ] : ' ' ) . '</td>';
}
echo '</tr>';
}
echo '</table>';
echo '</td><td valign="top">';
// output worsheet 2
$dim = $xlsx->dimension( 2 );
$num_cols = $dim[0];
$num_rows = $dim[1];
echo '<h2>'.$xlsx->sheetName(1).'</h2>';
echo '<table border=1>';
foreach ( $xlsx->rows( 2 ) as $r ) {
echo '<tr>';
for ( $i = 0; $i < $num_cols; $i ++ ) {
echo '<td>' . ( ! empty( $r[ $i ] ) ? $r[ $i ] : ' ' ) . '</td>';
}
echo '</tr>';
}
echo '</table>';
echo '</td></tr></table>';
} else {
echo SimpleXLSX::parseError();
}
推荐阅读
- java - 如何访问来自 ClientBuilder 的 get 请求的响应
- python - 从 json 文件中的字符串中删除标签(\r、\n、<、>)
- python - 带有 NLTK 或替代方法的浏览器中的 Python?
- c++ - MongoCXX 3.4 - 使用 unix 域套接字 URI
- ios - 适用于 Mac 的 Visual Studio。警告。Xcode 10.0 当前在首选项中选择,与正在使用的 Xamarin.ios 版本不兼容
- r - 创建一个计算表以显示八个基因列表中所有可能的组合对之间重叠基因的数量
- angular - 使用另一个变量的数字通过 Enum.[number] 调用枚举
- mysql - Mysql PDO 连接到外部服务器
- python - 获取 Falcon 应用程序中定义的所有路线的列表
- java - 数据库条目 onitemClick 的 getValue