我正在使用PhpExcel在我的codeigniter应用程序中将报告下载为excel。当我尝试下载我的报告时,它会在浏览器上显示一些字符,而不是下载文件。这在localhost中工作正常,但在服务器中不起作用。我不知道这个问题是什么。这是我的代码:
查看
<div class="box-content">
<div class="row">
<div class="col-lg-12">
<p class="introtext"><?= lang('customize_report'); ?></p>
<div id="form">
<?php echo form_open("reports/getSalesaccReport"); ?>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="reference_no"><?= lang("reference_no"); ?></label>
<?php echo form_input('reference_no', (isset($_POST['reference_no']) ? $_POST['reference_no'] : ""), 'class="form-control tip" id="reference_no"'); ?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="user"><?= lang("created_by"); ?></label>
<?php
$us[""] = "";
foreach ($users as $user) {
$us[$user->id] = $user->first_name . " " . $user->last_name;
}
echo form_dropdown('user', $us, (isset($_POST['user']) ? $_POST['user'] : ""), 'class="form-control" id="user" data-placeholder="' . $this->lang->line("select") . " " . $this->lang->line("user") . '"');
?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="customer"><?= lang("customer"); ?></label>
<?php echo form_input('customer', (isset($_POST['customer']) ? $_POST['customer'] : ""), 'class="form-control" id="customer" data-placeholder="' . $this->lang->line("select") . " " . $this->lang->line("customer") . '"'); ?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="biller"><?= lang("biller"); ?></label>
<?php
$bl[""] = "";
foreach ($billers as $biller) {
$bl[$biller->id] = $biller->company != '-' ? $biller->company : $biller->name;
}
echo form_dropdown('biller', $bl, (isset($_POST['biller']) ? $_POST['biller'] : ""), 'class="form-control" id="biller" data-placeholder="' . $this->lang->line("select") . " " . $this->lang->line("biller") . '"');
?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="warehouse"><?= lang("warehouse"); ?></label>
<?php
$wh[""] = "";
foreach ($warehouses as $warehouse) {
$wh[$warehouse->id] = $warehouse->name;
}
echo form_dropdown('warehouse', $wh, (isset($_POST['warehouse']) ? $_POST['warehouse'] : ""), 'class="form-control" id="warehouse" data-placeholder="' . $this->lang->line("select") . " " . $this->lang->line("warehouse") . '"');
?>
</div>
</div>
<?php if($this->Settings->product_serial) { ?>
<div class="col-sm-4">
<div class="form-group">
<?= lang('serial_no', 'serial'); ?>
<?= form_input('serial', '', 'class="form-control tip" id="serial"'); ?>
</div>
</div>
<?php } ?>
<div class="col-sm-4">
<div class="form-group">
<?= lang("start_date", "start_date"); ?>
<?php echo form_input('start_date', (isset($_POST['start_date']) ? $_POST['start_date'] : ""), 'class="form-control datetime" id="start_date"'); ?>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<?= lang("end_date", "end_date"); ?>
<?php echo form_input('end_date', (isset($_POST['end_date']) ? $_POST['end_date'] : ""), 'class="form-control datetime" id="end_date"'); ?>
</div>
</div>
</div>
<div class="form-group">
<div
class="controls"> <?php echo form_submit('submit_report', $this->lang->line("submit"), 'class="btn btn-primary"'); ?> </div>
</div>
<?php echo form_close(); ?>
</div>
控制器
function getSalesaccReport()
{
if ($this->input->post('customer')) {
$customerid = $this->input->post('customer');
} else {
$customerid = NULL;
}
echo $customerid;
if ($this->input->post('biller')) {
$biller = $this->input->post('biller');
} else {
$biller = NULL;
}
if ($this->input->post('warehouse')) {
$warehouse = $this->input->post('warehouse');
} else {
$warehouse = NULL;
}
if ($this->input->post('start_date')) {
$start_date = $this->input->post('start_date');
} else {
$start_date = NULL;
} echo $start_date;
if ($this->input->post('end_date')) {
$end_date = $this->input->post('end_date');
} else {
$end_date = NULL;
} echo $end_date;
if ($start_date) {
$start_date = $this->sma->fld($start_date);
$end_date = $this->sma->fld($end_date);
}
if (!$this->Owner && !$this->Admin) {
$user = $this->session->userdata('user_id');
}
$dateTmp = "DATE_FORMAT(".$this->db->dbprefix('sales').".date,'%m/%Y')";
$condition = "if(".$this->db->dbprefix('sales').".biller_id = 5, 'A', if(".$this->db->dbprefix('sales').".biller_id = 6, 'B',if(".$this->db-> dbprefix('sales').".biller_id = 7,'C','D' )))";
$this->db
->select("date, ".$this->db->dbprefix('warehouses').".name,
CONCAT(".$this->db->dbprefix('warehouses').".name,'-',".$dateTmp.",'-', ".$condition.",".$this->db->dbprefix('sales').".invoice_no) as month," ." biller, customer,
scan_no,unit_price,tin,cin,cst,total_discount, item_tax, shipping,subtotal,payment_status,sales.id as saleid,total_items,total,biller_id,tax_rate_id,grand_total,quantity", FALSE)
->from('sales')
->join('sale_items', 'sale_items.sale_id=sales.id', 'left')
->join('companies','companies.id=sales.customer_id', 'left')
->join('warehouses', 'warehouses.id=sales.warehouse_id', 'left')
->group_by('sales.id')
->order_by('sales.date desc');
if ($biller) {
$this->db->where('sales.biller_id', $biller);
}
if ($customerid) {
$this->db->where('sales.customer_id', $customerid);
}
if ($warehouse) {
$this->db->where('sales.warehouse_id', $warehouse);
}
if (($start_date) && ($end_date)) {
$this->db->where($this->db->dbprefix('sales').'.date BETWEEN "' . $start_date . '" and "' . $end_date . '"');
}
/* if ($start_date) {
$this->db->where($this->db->dbprefix('sales').'.date LIKE "' . $start_date . '"');
}
if ($end_date) {
$this->db->where($this->db->dbprefix('sales').'.date LIKE "' . $end_date . '"');
} */
$q = $this->db->get();
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
} else {
$data = NULL;
}
if (!empty($data))
{
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle(lang('sales_report'));
$this->excel->getActiveSheet()->SetCellValue('A1', lang('date'));
$this->excel->getActiveSheet()->SetCellValue('B1', lang('branch'));
$this->excel->getActiveSheet()->SetCellValue('C1', lang('Vch_No.'));
$this->excel->getActiveSheet()->SetCellValue('D1', lang('Voucher_Type'));
$this->excel->getActiveSheet()->SetCellValue('E1', lang('Particulars'));
$this->excel->getActiveSheet()->SetCellValue('F1', lang('TIN_No.'));
$this->excel->getActiveSheet()->SetCellValue('G1', lang('CIN'));
$this->excel->getActiveSheet()->SetCellValue('H1', lang('CST_No.'));
$this->excel->getActiveSheet()->SetCellValue('I1', lang('Scan_No'));
$this->excel->getActiveSheet()->SetCellValue('J1', lang('Product'));
$this->excel->getActiveSheet()->SetCellValue('K1', lang('quantity'));
$this->excel->getActiveSheet()->SetCellValue('L1', lang('Rate'));
$this->excel->getActiveSheet()->SetCellValue('M1', lang('Sales_@__5%'));
$this->excel->getActiveSheet()->SetCellValue('N1', lang('Output_Vat_@_5%'));
$this->excel->getActiveSheet()->SetCellValue('O1', lang('Sales_@_14.5%'));
$this->excel->getActiveSheet()->SetCellValue('P1', lang('Output_Vat_@_14.5%'));
$this->excel->getActiveSheet()->SetCellValue('Q1', lang('Sales_@_0%'));
$this->excel->getActiveSheet()->SetCellValue('R1', lang('Scanning_charge'));
$this->excel->getActiveSheet()->SetCellValue('S1', lang('Scanning_Discount'));
$this->excel->getActiveSheet()->SetCellValue('T1', lang('Shipping'));
$this->excel->getActiveSheet()->SetCellValue('U1', lang('Gross_Total'));
$row = 2;
$total = 0;
$paid = 0;
$balance = 0;
$quantity= 0;
$prdt_price = 0;
$sale5=0.00;
$output5=0.00;
$sale145=0.00;
$output145=0.00;
$sale0=0.00;
$discount=0;
foreach ($data as $data_row)
{
$saleid=$data_row->saleid;
$billerid=$data_row->biller_id;
$gross_total= $data_row->grand_total + shipping;
if($billerid==7)
{
$sale0=0;
$discount=$data_row->total_discount;
/*$scanqty=$data_row->quantity;
if($scanqty==0.5)
{
$scanning_charge+=500;
}
else if($scanqty==1)
{
$scanning_charge+=1000;
}*/
$this->db->select("sum(`quantity`*1000) as scanning_charge", FALSE);
$this->db->where('sale_id',$saleid);
$queryres = $this->db->get('sale_items');
if($queryres->num_rows() > 0)
{
foreach($queryres->result() as $queryres1)
{
$scanning_charge=$queryres1->scanning_charge;
}
}
else
{
}
}
else
{
$scanning_charge=0;
$discount=0;
}
if(($billerid==5) || ($billerid==6))
{
$this->db->select("sum( case when tax_rate_id = 1 then `net_unit_price`*`quantity` else 0 end) as sale0", FALSE);
$this->db->where('sale_id',$saleid);
$res1=$this->db->get('sale_items');
if($res1->num_rows() > 0)
{
echo $billerid;
foreach ($res1->result() as $row31)
{
$sale0=$row31->sale0;
}
}
else
{
}
}
$total0=$data_row->total;
$this->db->select("GROUP_CONCAT(DISTINCT product_name) as products,SUM(quantity)as prdtqty")
->from('sale_items')
->where('sale_id',$saleid);
$q1 = $this->db->get();
if ($q1->num_rows() > 0)
{
foreach (($q1->result()) as $row1)
{
$products = $row1->products;
$prdtqty = $row1->prdtqty;
}
}
else
{
$products="";
}
$sql="select sum( case when tax_rate_id = 2 then net_unit_price*quantity else 0 end) as sale5,
sum( case when tax_rate_id = 3 then net_unit_price*quantity else 0 end) as sale145,
sum( case when tax_rate_id = 2 then item_tax else 0 end) as output5,
sum( case when tax_rate_id = 3 then item_tax else 0 end) as output145
from sma_sale_items
where sale_id=".$saleid;
$res=$this->db->query($sql);
if($res->num_rows() > 0)
{
foreach ($res->result() as $row3)
{
$sale5=$row3->sale5;
$output5=$row3->output5;
$sale145=$row3->sale145;
$output145=$row3->output145;
}
}
$this->excel->getActiveSheet()->SetCellValue('A' . $row, $this->sma->hrld($data_row->date));
$this->excel->getActiveSheet()->SetCellValue('B' . $row, $data_row->name);
$this->excel->getActiveSheet()->SetCellValue('C' . $row, $data_row->month);
$this->excel->getActiveSheet()->SetCellValue('D' . $row, $data_row->biller);
$this->excel->getActiveSheet()->SetCellValue('E' . $row, $data_row->customer);
$this->excel->getActiveSheet()->SetCellValue('F' . $row, $data_row->tin);
$this->excel->getActiveSheet()->SetCellValue('G' . $row, $data_row->cin);
$this->excel->getActiveSheet()->SetCellValue('H' . $row, $data_row->cst);
$this->excel->getActiveSheet()->SetCellValue('I' . $row, $data_row->scan_no);
$this->excel->getActiveSheet()->SetCellValue('J' . $row, $products);
$this->excel->getActiveSheet()->SetCellValue('K' . $row, $prdtqty);
$this->excel->getActiveSheet()->SetCellValue('L' . $row, $data_row->total);
$this->excel->getActiveSheet()->SetCellValue('M' . $row, $sale5);
$this->excel->getActiveSheet()->SetCellValue('N' . $row, $output5);
$this->excel->getActiveSheet()->SetCellValue('O' . $row, $sale145);
$this->excel->getActiveSheet()->SetCellValue('P' . $row, $output145);
$this->excel->getActiveSheet()->SetCellValue('Q' . $row, $sale0);
$this->excel->getActiveSheet()->SetCellValue('R' . $row, $scanning_charge);
$this->excel->getActiveSheet()->SetCellValue('S' . $row, $discount);
$this->excel->getActiveSheet()->SetCellValue('T' . $row, $data_row->shipping);
$this->excel->getActiveSheet()->SetCellValue('U' . $row, $gross_total);
$total += $data_row->subtotal;
$paid += $data_row->item_tax;
$row++;
}
$this->excel->getActiveSheet()->getColumnDimension('A')->setWidth(18);
$this->excel->getActiveSheet()->getColumnDimension('B')->setWidth(8);
$this->excel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$this->excel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
$this->excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$this->excel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
$this->excel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
$this->excel->getActiveSheet()->getColumnDimension('I')->setWidth(12);
$this->excel->getActiveSheet()->getColumnDimension('J')->setWidth(30);
$this->excel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
$this->excel->getActiveSheet()->getColumnDimension('L')->setWidth(11);
$this->excel->getActiveSheet()->getColumnDimension('M')->setWidth(13);
$this->excel->getActiveSheet()->getColumnDimension('N')->setWidth(17);
$this->excel->getActiveSheet()->getColumnDimension('O')->setWidth(14);
$this->excel->getActiveSheet()->getColumnDimension('P')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('Q')->setWidth(12);
$this->excel->getActiveSheet()->getColumnDimension('R')->setWidth(18);
$this->excel->getActiveSheet()->getColumnDimension('S')->setWidth(20);
$this->excel->getActiveSheet()->getColumnDimension('T')->setWidth(12);
$this->excel->getActiveSheet()->getColumnDimension('U')->setWidth(14);
$filename = 'sales_report';
$this->excel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$this->excel->getActiveSheet()->getStyle('E2:E' . $row)->getAlignment()->setWrapText(true);
ob_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
header('Cache-Control: max-age=0');
ob_clean();
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
exit();
}
else{
$this->session->set_flashdata('error', lang('nothing_found'));
redirect('reports/sales_accounts');
}
}
任何人都可以帮我解决这个问题..我真的需要这个......提前谢谢。
答案 0 :(得分:2)
试试这个
ob_clean();
ob_start(); # added
header('Content-type: application/vnd.ms-excel; charset=UTF-8" ); # Improved with UTF charset
header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
header('Cache-Control: max-age=0');
ob_clean(); # remove this
如果您想在2003年和2007年下载此内容,则无需进行任何更改
2007 - file format xlsx
2003 - file format xls