Laravel Excel - 选择要导出的列

时间:2016-08-13 18:05:18

标签: laravel-5 maatwebsite-excel

我正在从maatwebsite使用Laravel Excel,但我将数据导出到xls文件时出现问题。我有这个查询,但我不需要显示xls文件中的所有列,但我需要选择所有这些列来执行操作,然后再下载文件。在我的选择中我有8列,但在我的标题中我只有7个显示,但这不起作用,因为8ª列也出现。

我的功能:

 public function toExcel($id) { $b = Budget::find($id);

$budget = Budget_Item::join('budgets', 'budget__items.id_budget', '=', 'budgets.id')
    ->join('items_sizes', 'budget__items.id_itemSize', '=', 'items_sizes.id')
    ->join('items', 'items_sizes.id_item', '=', 'items.id')
    ->join('material_types', 'items_sizes.id_materialType', '=', 'material_types.id')
    ->select('items.reference AS Referência', 'items.name AS Descrição', 'items_sizes.size AS Tamanho', 'material_types.material_type AS Material', 'budget__items.amount AS Quantidade', 'items_sizes.unit_price AS Val.Unitário', 'budget__items.price AS Val.Total', 'budget__items.purchasePrice')
    ->where('id_budget', '=', $id)
    ->get();

$budgetUpdate = [];
$budgetUpdate[] = ['Referência', 'Descrição', 'Tamanho', 'Material', 'Quantidade', 'Val.Unitário', 'Val.Total'];
foreach ($budget as $key)
{
  if ($key->purchasePrice > 0)
  {
    $key->unit_price = $key->purchasePrice;
  }

  $budgetUpdated[] = $key->toArray();
}
Excel::create('Proposta_'.$b->reference, function($excel) use($budgetUpdated)
{
  // Set the title
  $excel->setTitle('Proposta');

  $excel->sheet('Sheetname', function($sheet) use($budgetUpdated)
  {
    $sheet->fromArray($budgetUpdated, null, 'A1', false, true);
  });

})->download('xls');}

我该如何解决?

感谢的

3 个答案:

答案 0 :(得分:2)

为了它的价值,我遇到了一个类似的问题,并没有找到一个修复方面,所以继承我的解决方案。
1.我在回调中查询数据库并获取我需要的所有数据。
2.然后我遍历集合并在每次迭代时创建一个新数组并分配一个值&头。非常适合从模型中选择列

Excel::create('User List Export', function($excel) {
        $excel->sheet('Users', function($sheet) {
            $email = yourModelGoesHere::all();
            foreach ($email as $key => $value) {
                $payload[] = array('email' => $value['email'], 'name' => $value['name']);
            }
            $sheet->fromArray($payload);
        });
    })->download('xls');

答案 1 :(得分:0)

您可以尝试这种方式。
$ user_id = Auth :: user() - > id

  constructor(props) {
    super(props);
    this.state = { text: '' };
  }

  handleChange = (text) => {
    let textTemp = text;
    if (textTemp[0] !== '1' && textTemp[0] !== '0') {
      textTemp = '';
    }
    if (textTemp.length === 2) {
      if (parseInt(textTemp.substring(0, 2)) > 12) {
        textTemp = textTemp[0];
      } else if (this.state.text.length === 1) {
        textTemp += '/';
      } else {
        textTemp = textTemp[0];
      }
    }
    this.setState({text: textTemp})
  }

  render() {
    return (
      <TextInput
        keyboardType={'numeric'}
        onChangeText={this.handleChange}
        value={this.state.text}
        maxLength={5}
      />
    );
  }

答案 2 :(得分:0)

在Laravel Excel 3.1上进行了测试 docs

在控制器上

public function exportAsCsv() 
{
    return Excel::download(new MyExport, 'invoices.xlsx');
}

在MyExport上,查看地图功能

 class MyExport implements FromCollection, WithHeadings, WithMapping{

    public function collection(){
           return MyTable:all();
    }

    // here you select the row that you want in the file
    public function map($row): array{
           $fields = [
              $row->myfield2,
              $row->myfield1,
         ];
        return fields;
    }
}

还要检查this