gpt4 book ai didi

php - 在 PHP 中读取 xls (Excel) 文件

转载 作者:行者123 更新时间:2023-12-02 15:03:37 26 4
gpt4 key购买 nike

我对此很陌生,所以我想问一个问题。我使用 html 上传了一个 excel 文件,当我尝试学习如何使用 php 读取 excel 文件时,这是我从 IBM 获得的:

<?php

$filename = basename($_FILES["file"]["name"]);

$ext = substr($filename, strrpos($filename, '.') + 1);

function get_data($qnum, $qtype, $qname, $qtext, $atext, $pcredit, $rcounts, $r, $qcount, $cfac, $sd, $disc_inx, $disc_coef)
{
global $data;

$data[] = array('QNum' => $qnum, 'Qtype' => $qtype, 'Questionname' => $qname, 'Questiontext' => $qtext,
'Answertext' => $atext, 'PartialCredit' => $pcredit, 'RCounts' => $r, 'QCount' => $qcount,
'Correctionfacility' => $cfac, 'DiscriminationIndex' => $disc_inx, 'DiscriminationCoefficient' => $disc_coef);
}

if ($ext == "xls") {
$dom = DOMDocument::load($_FILES['file']['tmp_name']);
$rows = $dom->getElementsByTagName('Row');
$first_row = true;
foreach ($rows as $row) {
if (!$first_row) {
$qnum = "";
$qtype = "";
$qname = "";
$qtext = "";
$atext = "";
$pcredit = "";
$r = "";
$qcount = "";
$cfac = "";
$disc_inx = "";
$disc_coef = "";

$index = 1;
$cells = $row->getElementsByTagName('Cell');
foreach ($cells as $cell) {
$ind = $cell->getAttribute('Index');
if ($ind != null)
$index = $ind;

if ($index == 1)
$qnum = $cell->nodeValue;
if ($index == 2)
$qtype = $cell->nodeValue;
if ($index == 3)
$qname = $cell->nodeValue;
if ($index == 4)
$qtext = $cell->nodeValue;
if ($index == 5)
$atext = $cell->nodeValue;
if ($index == 6)
$pcredit = $cell->nodeValue;
if ($index == 7)
$r = $cell->nodeValue;
if ($index == 8)
$qcount = $cell->nodeValue;
if ($index == 9)
$cfac = $cell->nodeValue;
if ($index == 10)
$disc_inx = $cell->nodeValue;
if ($index == 11)
$disc_coef = $cell->nodeValue;

$index += 1;
}
get_data($qnum, $qtype, $qname, $qtext, $atext, $pcredit, $r, $qcount, $cfac, $disc_inx, $disc_coef);
}
$first_row = false;
}
}

else {
echo "Invalid file!";
}
?>

我遇到了语法错误

Warning: DOMDocument::load(): Start tag expected, '<' not found in /tmp/phpwUIpyZ, line: 1 in /var/www/moodle/question/upload_file.php on line 16 Fatal error: Call to a member function getElementsByTagName() on a non-object in /var/www/moodle/question/upload_file.php on line 17.

我的代码有什么错误?需要帮助,谢谢!

最佳答案

Excel 不是有效的 DOMDocument ,因此您当然不能使用 DOMDocument :)

我建议使用现成的东西,例如 PHPExcelReader .

祝你好运,
谢。

关于php - 在 PHP 中读取 xls (Excel) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7469235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com