我想使用php生成excelsheet,以便使用哪种方法或代码来生成动态excelsheet?请查看截图图片,了解我面临的问题。 screenshot of required excel sheet format 请问,任何人都可以帮我解决这些问题吗?提前谢谢。
我正在使用下面的代码示例。
$result = mysql_query("SELECT * FROM ".$pre."customers");
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++)
{
$headers[] = mysql_field_name($result , $i);
mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result)
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export1.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_row($result))
{
fputcsv($fp, array_values($row));
}
die;
}
但我不是说我可以将任何单元格锁定到这个excel表格中。那么哪个php方法或类适合生成excelsheet以供我使用?
答案 0 :(得分:0)
似乎你想要更多的CSV而不是可能的。你知道excel中的导入功能吗?您可以轻松设置文档,仅从您自己的php服务等接收动态内容为CSV(下部)...
我的Excel版本是丹麦语,但它的数据选项卡和“来自互联网”之类的东西:)
答案 1 :(得分:0)
public function get_excel($err = array())
{
// Clear any previous output
ob_end_clean();
// I assume you already have your $result
$search = ($this->uri->segment(2))?$this->uri->segment(2):'key';
$result=mysql_query("select * from job");
$num_fields = mysql_num_fields($result);
// Fetch MySQL result headers
$headers = array();
//$headers[] = "serial no";
for ($i = 0; $i < $num_fields; $i++) {
$headers[] = strtoupper(mysql_field_name($result , $i));
}
// Filename with current date
$current_date = date("y/m/d");
$filename = "MyFileName" . $current_date . ".csv";
// Open php output stream and write headers
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.$filename);
header('Pragma: no-cache');
header('Expires: 0');
// Write mysql headers to csv
fputcsv($fp, $headers);
//$row_tally = 0;
// Write mysql rows to csv
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
// $row_tally = $row_tally + 1;
//echo $row_tally.",";
fputcsv($fp, array_values($row));
}
die;
}
}