如何选择特定工作表并在其中显示数据。我已经使用了代码中的一些函数,我应该添加库函数来执行代码吗?
显示所有Excel文件数据,但我希望显示特定的页面或工作表。
<?php
require 'Classes/PHPExcel/IOFactory.php';
// Mysql database
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "import_db";
$inputfilename = $_POST['fileToUpload'];
$exceldata = array();
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Read your Excel workbook
try
{
$inputfiletype = PHPExcel_IOFactory::identify($inputfilename);
$objReader = PHPExcel_IOFactory::createReader($inputfiletype);
$objPHPExcel = $objReader->load($inputfilename);
}
catch(Exception $e)
{
die('Error loading file "'.pathinfo($inputfilename,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
//$sheetNames = $sheet->getSheetNames();
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$count=$objPHPExcel->getSheetCount();
$names = $objPHPExcel->getSheetNames();
echo $count;
echo $names;
//echo $sheetNames;
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++)
{
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
$exceldata[] = $rowData[0];
}
// Print excel data
echo "<table>";
foreach ($exceldata as $index => $excelraw)
{
echo "<tr>";
foreach ($excelraw as $excelcolumn)
{
echo "<td>".$excelcolumn."</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
答案 0 :(得分:0)
在尝试中更改以下代码,它将适合您。
try
{
$sheetname = 'Data Sheet #2'; // Sheet name which you want to load
$objReader = PHPExcel_IOFactory::createReader($inputfiletype); // Create a new Reader of the type defined in $inputfiletype
$objReader->setLoadSheetsOnly($sheetname); // Advise the Reader of which WorkSheets we want to load
$objPHPExcel = $objReader->load($inputfilename); //Load $inputfilenameto a PHPExcel Object
}