总计列的最简单方法?
我有一个 PHP 脚本,最多可导入 10 个左右不同的 CSV 文件。每个文件的每一行都包含银行帐户信息,包括余额。将所有 CSV 数据导入数据库后,我想通过比较数据库中的帐户总余额与 CSV 文件的帐户总余额来确保数据正确输入。
我至少看到了几个选项:
在 Excel 中手动合计所有帐户余额 - 恶心。
编写一个 PHP 脚本来读取每个 CSV 文件并合计帐户余额 - 也很糟糕。
我希望存在第三种选择。如果我能做这样的事情那就太棒了:
<代码>excel --file="cd.csv" | sum --column="E"
这显然不是真的,但希望你能明白。使用 PHP、MySQL、Linux 命令、Excel 和/或任何其他工具的某种组合,有没有一种简单的方法可以做到这一点?
I have a PHP script that imports up to 10 or so different CSV files. Each row of each file contains bank account info, including balance. After I've imported all the CSV data into my database, I'd like to make sure the data got in there correctly by comparing the total account balance in the database to the total account balance of the CSV files.
I see at least a few options:
Manually total up all the account balances in Excel - yuck.
Write a PHP script to read each CSV file and total up the account balances - also yuck.
Some third option that I hope exists. It would be amazing if I could do something like:
excel --file="cd.csv" | sum --column="E"
That's obviously not a real thing but hopefully you get the idea. Using some combination of PHP, MySQL, Linux commands, Excel and/or any other tools, is there a simple way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不必为您提供完整的答案,但 AWK 应该能够解决您的问题:看看这 2 篇文章:
https://superuser.com/questions/53652/transforming-csv-file-using-sed
Shell 命令对整数求和,每行一个?
我不是 AWK 专家,无法提供解决方案,但也许其他人可以帮助我们这里。
另一种选择(您可能也认为恶心)是使用像 PHPExcel 这样的库
Don't have to complete answer for you, but AWK should be able to solve your problem: Have a look at these 2 posts:
https://superuser.com/questions/53652/transforming-csv-file-using-sed
Shell command to sum integers, one per line?
I'm not enough of a AWK expert to give the solution, but perhaps someone else can help us here.
Another option (which you may also consider yuck) is to use a library like PHPExcel
您可以使用
fgetcsv()
迭代 CSV 文件,将每一行转换为值数组。您可以在每次迭代时累积包含余额的数组元素的值,直到获得总和。使用glob
获取文件夹中的 CSV 文件列表。You can iterate over the CSV file using
fgetcsv()
which converts each line to an array of values. You can accumulate the value of the array element containing the balance as you move thru each iteration until you get the sum total. Useglob
to get the list of CSV files in a folder.您可能不必“手动”合计帐户余额,如果您可以使用应用程序中的 Excel 函数,VBA 中的 Excel 公式将为:
其中 A:A 代表 A 列。
You may not have to "manually" total up the account balances, if you can use Excel functions from your application, the Excel formula in VBA would be:
where A:A is for column A.
尝试使用 CSVFix 及其摘要选项。它将为您提供比您需要的更多的数据,但应该易于使用。
否则,这听起来对 Awk 来说是一个很好的用途。
Try using CSVFix with it's summary option. It will get you more data than you need, but should be easy to use.
Otherwise, this sounds like a good use for Awk.
为什么不能用公式自动汇总 Excel 中的帐户余额,然后将其与其余数据一起导出?
Why can't you just automagically total up the account balances in excel with a formula and export them with the rest of the data?
有点不同的角度:利用 MySql CSV 引擎< /a> 将 CSV 文件公开给 Mysql,然后执行正常的 SQL SUM。
另请参阅:CSV 存储引擎
A bit of a different angle: Make use of the MySql CSV engine to expose your CSV files to Mysql and then do a normal SQL SUM.
See also: The CSV Storage Engine