概述:
1. 在 Perl 中,我使用 Spreadsheet::WriteExcel 写入 Excel 创建的 CSV 文件。
2. 要输入 CSV 文件的数据是由 Mysql 查询生成的。
发生了什么: 1. 正在生成 CSV。2. 只处理一行数据。
需要什么: 1. 以行的形式定期添加数据,比如每 30 秒。因此,它将每天创建一个巨大的 CSV 文件。这意味着将在 24 小时内添加 2880 行。
代码如下:
use strict;
use warnings;
use DBI;
use Spreadsheet::WriteExcel;
# DEFINING THE DATE
($sec,$min,$hour,$mday,$mon,$year) =localtime(time);
undef $sec;
undef $hour;
undef $min;
$year += 1900;
$mday;
$mon+=1;
if ($mon < 10) {
$mon = "0".$mon;
}
if ($mday < 10) {
$mday = "0".$mday;
}
# CREATION OF A NEW WORKBOOK AND ADDITION OF A WORKSHEET
$tdate = sprintf("%04d%02d%02d", $year, $mon, $mday);
$SubsLogFile = "Subscription-".$tdate;
$SubsLogFile .= ".csv";
my $workbook = Spreadsheet::WriteExcel->new($SubsLogFile);
my $worksheet = $workbook->add_worksheet();
#=============================================
# FORMAT FOR THE HEADER
my $format = $workbook->add_format();
$format->set_bold();
$format->set_color('red');
$format->set_align('center');
# CREATION OF THE HEADER
$worksheet->write(0, 0, "Sub Day", $format);
$worksheet->write(0, 1, "Sub Hour", $format);
#=============================================
###################
## SUBSCRIPTIONS ##
###################
# INPUTTING THE SERVER DETAILS
$db ="abcd";
$user = " abcd ";
$pass = " abcd ";
$host="100.100.100.100";
my $col = 2;
# GENERATING THE SQL QUERIES
$query1 = "SELECT COUNT(*) FROM subscription_detail WHERE creation_date >=CURRENT_DATE();";
$query2 = "SELECT COUNT(*) FROM subscription_detail WHERE DATE_FORMAT(creation_date,'%Y-%m-%d %H') >= DATE_FORMAT(NOW(),'%Y- %m-%d %H');";
# CONNECTING TO THE SERVER
$dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pass,{
PrintError => 0,
RaiseError => 0
} ) or die "Can't connect to the database: $DBI::errstr\n";
$sqlQuery1 = $dbh->prepare($query1) or die "Can't prepare $query1: $DBI::errstr\n";
$sqlQuery2 = $dbh->prepare($query2) or die "Can't prepare $query2: $DBI::errstr\n";
# EXECUTING THE QUERIES
my $rv = $sqlQuery1->execute or die "can't execute the query: $DBI::errstr\n";
my $rv = $sqlQuery2->execute or die "can't execute the query: $DBI::errstr\n";
#=============================================
# OUTPUTING THE VALUES
while (@row= $sqlQuery1->fetchrow_array()) {
my $data1 = $row[0];
$worksheet->write($col, 0, "$data1");
};
while (@row= $sqlQuery2->fetchrow_array()) {
my $data2 = $row[0];
$worksheet->write($col, 1, "$data2");
};
# Close Workbook
$workbook->close();
#
检查了整个互联网,但没有解决方案或提示。我已经尝试了很多东西,比如 sleep 30, counters tec,但它没有帮助。
感谢任何形式的帮助.. :)