我有这个项目,我应该能够上传excel文件并读取内容,然后将信息上传到数据库.所以我决定使用一个库帮助我成为Maatwebsite/Laravel-Excel
但我尝试阅读文档http://www.maatwebsite.nl/laravel-excel/docs/import但我似乎无法找到我需要的文档.
例如,在第一行中我的excel文件John,Kennedy,Male
这在我的数据库corrensponds First Name,Last Name,Gender.我该如何阅读并上传?有人能帮我吗?
谢谢!
我的代码截至目前
public function postUploadCsv()
{
$rules = array(
'file' => 'required',
'num_records' => 'required',
);
$validator = Validator::make(Input::all(), $rules);
// process the form
if ($validator->fails())
{
return Redirect::to('customer-upload')->withErrors($validator);
}
else
{
$file = Input::file('file');
dd($file);
exit();
}
}
Run Code Online (Sandbox Code Playgroud) 在我的控制器中,我有如下代码:
Excel::create('Laravel Excel', function($excel) {
$excel->sheet('Excel sheet', function($sheet) {
$sheet->setOrientation('landscape');
});
})->export('xls');
Run Code Online (Sandbox Code Playgroud)
在别名数组的config/app.php中我定义了这个:
'Excel' => 'Maatwebsite\Excel\ExcelServiceProvider',
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我不能让它工作这个库...任何想法?
我试图允许用户使用带有产品信息的Laravel Excel文件下载 Excel 。我当前的网络路线如下所示:
Route::get('/excel/release', 'ExcelController@create')->name('Create Excel');
Run Code Online (Sandbox Code Playgroud)
我当前的导出如下所示:
class ProductExport implements FromQuery
{
use Exportable;
public function __construct(int $id)
{
$this->id = $id;
}
public function query()
{
return ProductList::query()->where('id', $this->id);
}
}
Run Code Online (Sandbox Code Playgroud)
我当前的控制器如下所示:
public function create(Request $request) {
# Only alowed tables
$alias = [
'product_list' => ProductExport::class
];
# Ensure request has properties
if(!$request->has('alias') || !$request->has('id'))
return Redirect::back()->withErrors(['Please fill in the required fields.'])->withInput();
# Ensure they can use this
if(!in_array($request->alias, array_keys($alias)))
return Redirect::back()->withErrors(['Alias ' . $request->alias …Run Code Online (Sandbox Code Playgroud) 我尝试使用php excel更新laravel,同时安装我在作曲家中发现了以下警告.
错误:
Warning: Ambiguous class resolution, "SettingsController" was found in both
"C:\xampp\htdocs\mti\app\controllers\SettingsController.php" and
"C:\xampp\htdocs\mti\app\controllers\SettingsControllerBackup.php", the first
will be used.Warning: Ambiguous class resolution, "ClassModel" was found in both
"C:\xampp\htdocs\mti\app\models\ClassModel.php" and "C:\xampp\htdocs\mti\
app\models\LoginModel.php", the first will be used.
Run Code Online (Sandbox Code Playgroud)
SettingsController:
<?php
class SettingsController extends BaseController
{
public function ChangePasswordLayout()
{
return View::make('settings/changepassword/changepassword');
}
public function ChangePasswordProcess()
{
$PasswordData = Input::all();
Validator::extend('pwdvalidation', function($field, $value, $parameters)
{
return Hash::check($value, Auth::user()->password);
});
$messages = array('pwdvalidation' => 'The Old Password is Incorrect');
$validator = Validator::make($PasswordData, User::$rulespwd, …Run Code Online (Sandbox Code Playgroud) 我正在使用Laravel Excel,我想加载一个视图文件并将其设置为特定的行.用laravel excel可以做到吗?bellow是将视图加载到工作表中的代码:
Excel::create('New file', function($excel) {
$excel->sheet('New sheet', function($sheet) {
$sheet->loadView('folder.view');
});
});
Run Code Online (Sandbox Code Playgroud) 直截了当的问题。如何使用 laravel-excel 获取电子表格中的总行数?
我现在有一个工作计数器,用于记录已处理的行数(在文件中CompanyImport),但在开始将行添加到数据库之前我需要总行数。
我正在导入的工作表几乎有 100 万行,因此我尝试创建一个进度条。
我的进口:
public function model(array $row)
{
# Counter
++$this->currentRow;
# Dont create or validate on empty rows
# Bad workaround
# TODO: better solution
if (!array_filter($row)) {
return null;
}
# Create company
$company = new Company;
$company->crn = $row['crn'];
$company->name = $row['name'];
$company->email = $row['email'];
$company->phone = $row['phone'];
$company->website = (!empty($row['website'])) ? Helper::addScheme($row['website']) : '';
$company->save();
# Everything empty.. delete address
if (!empty($row['country']) || !empty($row['state']) || !empty($row['postal']) || !empty($row['address']) || …Run Code Online (Sandbox Code Playgroud) 我试图将我的模型中的一些数据导出到带有"Laravel excel"的excel scheet,我已经完成了,但是我的结果远离我真正需要的东西
这就是我在下载的文件中得到的:
这就是我真正想要实现的目标:

我的控制器部分:
//casting export...
Excel::create('ExcelExport', function($excel) use($filters, $agents) {
$main_arr = array();
foreach($agents as $value){
$main_arr[] = Card::cardForUser($value, $filters)->toArray();
}
$excel->sheet('Sheetshit', function($sheet) use($main_arr) {
//You may ask me "why are you using foreach?"
// and my answer will be:"I don`t KNOW, because it WORKS!"
foreach($main_arr as $one){
$sheet->fromArray($one);
}
});
})->export('xls');
Run Code Online (Sandbox Code Playgroud)
型号部分:
public static function cardForUser($user_id, $filters = array()){
$query = static::UserId($user_id);//just gets 'where user id'
foreach($filters['fields'] as $select){
$query->addSelect($select);
}
return $query->get();
}
public function …Run Code Online (Sandbox Code Playgroud) 当我尝试根据特定请求将数据导出为 Excel 文件时,出现以下错误
PhpOffice\PhpSpreadsheet\Reader\Exception 无法加载 path_to_project\storage\framework\laravel-excel\laravel-excel-pz23rYwVENIOZUw7vnvhKaNjUkkNLNT8.html 作为 DOM 文档
我搜索了很多,但找不到任何答案,只有当semester = First Sem学期是第二、第三等时,问题才会出现,这个错误不会出现,我可以下载 Excel。
下面是我的代码
Route::get('/export', function () {
return Excel::download(new ReportExport(request()->exams, request()->course, request()->semesterName), ''.request()->course.' '.request()->semesterName.'('.request()->exams.').xlsx');
})
// ReportExcel.php
$data = Fee::with(['backpapers' => function($bp) use ($myCourse, $semName) {
$bp->where('course_name', $myCourse)->where('semester', $semName);
}])->get();
$table = '<table class="table table-border table-hover"><thead><tr><th>#</th><th>Roll No</th><th>Name</th><th>Reg_No</th><th>Father\'s Name</th><th>Gender</th><th>Aadhaar No</th><th>Mobile</th><th>Email</th><th>Paper 1</th><th>Paper 2</th><th>Paper 3</th><th>Paper 4</th><th>Exam Fee</th></tr></thead><tbody>';
$count = 1;
foreach ($data as $key => $bp) {
if (count($bp->backpapers) > 0) {
$table .= '<tr><td>'.$count.'</td><td>'.$bp->roll_number.'</td><td>'.$bp->name.'</td><td>'.($bp->registration_number != null ? $bp->registration_number …Run Code Online (Sandbox Code Playgroud) 我需要通过Laravel Excel导出具有复杂标题的工作表。我需要一个主标题和另一个子标题。
我正在这样尝试,
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
class InvoicesExport implements FromQuery, WithHeadings
{
public function headings(): array
{
return [
'Account 1' =>[
"Account 1 id",
"Account 1 branch"
],
'Account 2' =>[
"Account 2 id",
"Account 2 branch"
],
];
}
}
Run Code Online (Sandbox Code Playgroud)
但获取标题列,如 [“帐户 1 id”、“帐户 1 分支”]
有没有办法存档这个任务?
我有一个金额列,当数字没有小数时我想设置整数格式,当数字有小数时设置双精度格式。在这两种方式中,我想为数字添加分隔符。目前,我使用bindValue,但Excel单元格不知道金额列是数字格式,我应该选择它们并将它们转换为数字。
public function bindValue(Cell $cell, $value)
{
if (is_int($value)) {
$cell->setValueExplicit($value, '#,##0');
return true;
}else if (is_double($value)) {
$cell->setValueExplicit($value, '#,##0.00');
return true;
}else{
$cell->setValueExplicit($value, DataType::TYPE_STRING);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何修复它?
excel number-formatting laravel laravel-excel maatwebsite-excel
laravel-excel ×10
laravel ×8
php ×6
excel ×5
laravel-5 ×2
composer-php ×1
import ×1
model ×1
namespaces ×1
phpexcel ×1