Как отображать изображения в выводе Excel с помощью библиотеки PHPSPreadSheetPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как отображать изображения в выводе Excel с помощью библиотеки PHPSPreadSheet

Сообщение Anonymous »

Я разработал функцию для вывода ряда информации в WordPress, и я использую библиотеку PHPSPreadSheet для вывода информации. I followed the documentation of the phpspreadsheet site, but I am facing this error:

Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Writer\Exception: File http://persis.local/wp-content/uploads/ ... oat-01.jpg does not exist В e: \ xampp \ htdocs \ persis.local \ wp-content \ plugins \ by-products \ vendor \ phpoffice \ phpspreadsheet \ src \ phpspreadsheet \ writer \ xlsx \ contentTypes.php: 226 Трэйс: #0 E: \ xampp \ htdocs \ persis.local \ wp-content \ plugins \ by-products \ vendor \ phpoffice \ phpspreadsheet \ src \ phpspreadsheet \ writer \ xlsx \ contentTypes.php (137): Phpoffice \ phpspreadsheet \ writer \ xlsx \ contentypes-> getimagemimeType ('http: //persis.l ...') #1 E: \ xampp \ htdocs \ persis.local \ wp-content \ plugins \ by-products \ vendor \ phpoffice \ phpspreadsheet \ src \ phpspreadsheet \ writer \ xlsx.php (332): Phpoffice \ phpspreadsheet \ writer \ xlsx \ contentypes-> writecontenttypes (Object (phpoffice \ phpspreadsheet \ vallsheet), false) #2 e: \ xampp \ htdocs \ persis.local \ wp-content \ plugins \ by-products. Phpoffice \ phpspreadsheet \ writer \ xlsx-> save ('buy_dorder _...') #3 e: \ xampp \ htdocs \ persis.local \ wp-includes \ class-wp-hook.php (308): в E:\xampp\htdocs\persis.local\wp-content\plugins\by-products\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\ContentTypes.php on line 226

I searched a bit and made a series of changes in my code, but Это не имело никакого значения, ошибка все еще существует. Это мой код: < /p>
require_once 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
function bp_display_purchase_order_info_callback() {
echo '';
echo 'Purchase Order Information';
echo '';
echo '';
echo '';
echo 'Product Photo';
echo 'Product Name';
echo 'SKU';
echo 'Number of Purchases';
echo 'Producer Name';
echo 'Last Price';
echo '';
echo '';
echo '';

// Query the products to retrieve their details
$args = array(
'post_type' => 'product',
'posts_per_page' => - 1,
'orderby' => 'modified', // Sort by the update date
'order' => 'DESC', // Sort in descending order (newest to oldest)
);

$products = new WP_Query( $args );

if ( $products->have_posts() ) {
while ( $products->have_posts() ) {
$products->the_post();

// Get product details
$product_id = get_the_ID();
$product = wc_get_product( $product_id );
$product_name = $product->get_name();
$product_sku = $product->get_sku();
$product_image = get_the_post_thumbnail_url( $product_id, 'thumbnail' );

// Get custom "Purchase Quantity" field value
$purchase_quantity = get_post_meta( $product_id, '_purchase_quantity', true );

// Get the latest price information
$last_price = '';
$vendor_name = '';
$price_change = '';

if ( have_rows( 'details', $product_id ) ) {
$rows = get_field( 'details', $product_id );
$last_row = end( $rows );
$last_price = $last_row['price'];
$vendor_name = $last_row['vendor_name'];
$price_change = $last_row['price_change'];
}

// Display the product only if it has a "Number of Purchases" value
if ( $purchase_quantity > 0 ) {
echo '';
echo '';
echo '' . $product_name . '';
echo '' . $product_sku . '';
echo '' . $purchase_quantity . '';
echo '' . $vendor_name . '';
echo '';

if ( $last_price ) {
$price_text = '';

if ( ! empty( $price_change ) && strcasecmp( $price_change, 'yes' ) == 0 ) {
$price_text .= '' . $last_price . '';
} else {
$price_text .= $last_price;
}

echo $price_text;
}

echo '';
echo '';
}
}
wp_reset_postdata();
} else {
echo 'No products found.';
}

echo '';
echo '';

// Create a new Spreadsheet object
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Add column headers
$sheet->setCellValue( 'A1', 'Product Photo' );
$sheet->setCellValue( 'B1', 'Product Name' );
$sheet->setCellValue( 'C1', 'SKU' );
$sheet->setCellValue( 'D1', 'Number of Purchases' );
$sheet->setCellValue( 'E1', 'Producer Name' );
$sheet->setCellValue( 'F1', 'Last Price' );

// Set column headers style
$headerStyle = $sheet->getStyle( 'A1:F1' );
$headerStyle->getFont()->setBold( true );
$headerStyle->getAlignment()->setHorizontal( \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER );
$headerStyle->getBorders()->getAllBorders()->setBorderStyle( \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN );

$row = 2; // Start from row 2 for data

// Loop through the products and add data to the Excel sheet
if ( $products->have_posts() ) {
while ( $products->have_posts() ) {
$products->the_post();

// Get product details
$product_id = get_the_ID();
$product = wc_get_product( $product_id );
$product_name = $product->get_name();
$product_sku = $product->get_sku();
$product_image = get_the_post_thumbnail_url( $product_id, 'thumbnail' );

// Get custom "Purchase Quantity" field value
$purchase_quantity = get_post_meta( $product_id, '_purchase_quantity', true );

// Get the latest price information
$last_price = '';
$vendor_name = '';
$price_change = '';

if ( have_rows( 'details', $product_id ) ) {
$rows = get_field( 'details', $product_id );
$last_row = end( $rows );
$last_price = $last_row['price'];
$vendor_name = $last_row['vendor_name'];
$price_change = $last_row['price_change'];
}

// Display the product only if it has a "Number of Purchases" value
if ( $purchase_quantity > 0 ) {

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName( 'Product Image' );
$drawing->setDescription( 'Product Image' );
$drawing->setPath( $product_image ); /* put your path and image here */
$drawing->setCoordinates( 'A1' );
$drawing->setOffsetX( 110 );
$drawing->setRotation( 25 );
$drawing->getShadow()->setVisible( true );
$drawing->getShadow()->setDirection( 45 );
$drawing->setWorksheet( $spreadsheet->getActiveSheet() );

// Add data to the Excel sheet
$sheet->setCellValue( 'A' . $row, $product_image );
$sheet->setCellValue( 'B' . $row, $product_name );
$sheet->setCellValue( 'C' . $row, $product_sku );
$sheet->setCellValue( 'D' . $row, $purchase_quantity );
$sheet->setCellValue( 'E' . $row, $vendor_name );
$sheet->setCellValue( 'F' . $row, $last_price );

$row ++;
}
}
wp_reset_postdata();
}

// Adjust the column widths
foreach ( range( 'A', 'F' ) as $column ) {
$sheet->getColumnDimension( $column )->setAutoSize( true );
}

// Set the filename for the Excel file
$filename = 'purchase_order_info.xlsx';

// Save the Excel file
$writer = new Xlsx( $spreadsheet );
$writer->save( $filename );

// Output a link to download the Excel file
echo '
Download Excel File
';

echo '';
}
< /code>
Я проверял коды несколько раз и внес несколько изменений, но проблема заключается в месте. Как я могу решить эту проблему?

Подробнее здесь: https://stackoverflow.com/questions/768 ... et-library
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»