我一直在尋找幾天來找到一個PHP腳本,它將讀取MsExcel(myFile.xls)文件並將其轉換爲CSV(myFile.csv)文件並將其輸出。有誰知道我可以做這個和/或幾個代碼示例?如何將XLS轉換爲PHP中的CSV文件?
-2
A
回答
2
此代碼讀取.xls文件,然後逐行將其轉換爲.csv。
但我會建議尋找任何外部庫或函數做同樣的,它會更容易 -
/* Get the excel.php class here: http://www.phpclasses.org/browse/package/1919.html */
require_once("../classes/excel.php");
$inputFile=$argv[1];
$xlsFile=$argv[2];
if(empty($inputFile) || empty($xlsFile)) {
die("Usage: ". basename($argv[0]) . " in.csv out.xls\n");
}
$fh = fopen($inputFile, "r");
if(!is_resource($fh)) {
die("Error opening $inputFile\n");
}
/* Assuming that first line is column headings */
if(($columns = fgetcsv($fh, 1024, "\t")) == false) {
print("Error, couldn't get header row\n");
exit(-2);
}
$numColumns = count($columns);
/* Now read each of the rows, and construct a
big Array that holds the data to be Excel-ified: */
$xlsArray = array();
$xlsArray[] = $columns;
while(($rows = fgetcsv($fh, 1024, "\t")) != FALSE) {
$rowArray = array();
for($i=0; $i<$numColumns;$i++) {
$key = $columns[$i];
$val = $rows[$i];
$rowArray["$key"] = $val;
}
$xlsArray[] = $rowArray;
unset($rowArray);
}
fclose($fh);
/* Now let the excel class work its magic. excel.php
has registered a stream wrapper for "xlsfile:/"
and that's what triggers its 'magic': */
$xlsFile = "xlsfile:/".$xlsFile;
$fOut = fopen($xlsFile, "wb");
if(!is_resource($fOut)) {
die("Error opening $xlsFile\n");
}
fwrite($fOut, serialize($xlsArray));
fclose($fOut);
exit(0);
相關問題
- 1. 將.csv文件轉換爲.xls文件
- 2. 將.xls文件轉換爲.csv文件?
- 3. 如何將csv文件轉換爲vb.net中的XLS文件
- 4. 將xls文件批量轉換爲csv
- 5. 如何將csv文件轉換爲sapui5中的xls或xlsx?
- 6. PHP將CSV轉換爲XLS - phpExcel錯誤
- 7. 將CSV轉換爲XLS
- 8. 如何將csv轉換爲xls in informatica
- 9. 將文件夾中的XLS/XLSX文件轉換爲CSV
- 10. 如何將xls文件轉換爲xml?
- 11. 如何將XSD文件轉換爲XLS
- 12. 如何使用PowerShell將多個xls文件轉換爲csv?
- 13. C#將csv轉換爲xls(使用現有的csv文件)
- 14. 如何將xls文件轉換爲訪問2000 VBA中的csv文件?
- 15. 如何把.xls文件轉換爲.csv文件?
- 16. Python:將xls中的多個文件轉換爲csv
- 17. 如何使用PHP將Excel XLS轉換爲CSV
- 18. 將.xls文件轉換爲.csv文件時出錯
- 19. 將CSV轉換爲XLS的腳本
- 20. 將.dat文件轉換爲.xls文件
- 21. 將xml文件轉換爲xls文件
- 22. Delphi 7將XLS(X)轉換爲CSV
- 23. MS Access將.csv轉換爲.xls
- 24. 使用Mulesoft將XLS轉換爲CSV
- 25. 使用python將csv轉換爲xls
- 26. 將Xls自動轉換爲CSV
- 27. 如何將HSSFWorkbook轉換爲CSV文件..?
- 28. 如何將.rpt文件轉換爲csv
- 29. 使用PHPExcel將多個xls文件轉換爲csv
- 30. 使用VB.net將.CSV文件轉換爲.XLS
XLS意味着MS Excel中,對不對?在Windows上,可以使用Office自動化(基本上是一個榮耀的VB腳本)來完成它。你有沒有考慮過?或者LibreOffice方法會更可口嗎? – hardmath