Commit a9b06589 by wenyi.chen

主页修改

parent cdeb8398
...@@ -20,95 +20,9 @@ class Test extends BaseController ...@@ -20,95 +20,9 @@ class Test extends BaseController
{ {
public function index() public function index()
{ {
try{ $dir = "/opt/huisheng/wwwroot/api.raisound.com/html/--chat/"; // 文件夹路径
// 创建一个新的 Spreadsheet 对象 $new_dir = "/path/to/your/new_files"; // 新文件夹路径
$spreadsheet = new Spreadsheet(); $files = scandir($dir); // 获取文件夹中的文件列表
$sheet = $spreadsheet->getActiveSheet(); dump($files);
// 创建一个 Drawing 对象,并设置图片的路径
$drawing = new Drawing();
$drawing->setName('Sample Image');
$drawing->setDescription('Sample Image');
$drawing->setPath('./image_0.jpg'); // 图片路径
$drawing->setCoordinates('B1'); // 图片放置的单元格位置
$drawing->setWorksheet($sheet); // 关联到工作表
// 调整图片大小以适应单元格(可选)
$drawing->setHeight(36); // 图片高度,单位为磅
$drawing->setWidth(32); // 图片宽度,单位为磅
// 保存 ExcelFile 文件
$writer = new Xlsx($spreadsheet);
$writer->save('excel_with_image.xlsx');
}catch (\Exception $e){
var_dump($e->getMessage());
}
}
/**
* 读取Excel文件
*/
public function index4() {
try{
// 创建一个新的Excel对象
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$publicPath = Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR;
// 图片路径
$imagePath = './image_0.png';
// 将图片读取为二进制数据
$imageContent = file_get_contents($imagePath);
// 创建一个Drawing对象
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Sample Image');
$drawing->setDescription('Sample Image');
// $drawing->setPath(''); // 设置为空,因为我们使用二进制数据
$drawing->setCoordinates('A1'); // 设置图片要插入的单元格位置
$drawing->setWorksheet($sheet);
// 将图片二进制数据添加到Drawing对象中
$drawing->setMimeType(mime_content_type($imagePath));
$drawing->setData($imageContent);
// 设置图片的尺寸(可选)
$drawing->setHeight(150); // 图片高度
$drawing->setWidth(150); // 图片宽度
// 创建写入器
$writer = new Xlsx($spreadsheet);
// 设置Excel文件要保存的路径
$outputFileName = './file.xlsx';
// 保存Excel文件
$writer->save($outputFileName);
// 下载Excel文件(或者你可以返回文件路径让用户自己下载)
// return response()->download($outputFileName, 'file.xlsx')->deleteFileAfterSend(true);
}catch (\Exception $e){
var_dump($e->getMessage());
}
}
public function index3()
{
$spreadsheet = IOFactory::load('4.xlsx');
$allSheets = $spreadsheet->getAllSheets();
foreach ($allSheets as $sheet) {
$drawings = $sheet->getDrawingCollection();
foreach ($drawings as $index=>$drawing) {
// 图片类型:JPEG, PNG, GIF
$imageType = $drawing->getExtension();
$imageName = 'image_' . $index . '.' . $imageType;
$imagePath = 'images/' . $imageName;
// 保存图片到本地
$drawing->getPath() ?
Filesystem::put($imagePath, file_get_contents($drawing->getPath())) :
$drawing->save($imagePath);
echo "图片已保存到:{$imagePath}\n";
}
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment