简体   繁体   中英

XSLXWriter Insert multiple images at center of cell in a column

I am trying to use libxlsxwriter to insert images in a column row by row but the images are out of cell bound and on top of each other.

Edit:

I found that this line of code might be causing some issues worksheet_set_default_row(worksheet,110,true);

and changed my code to the following:

#include <QCoreApplication>
#include "QProcess"
#include "QThread"
#include "xlsxwriter.h"
#include <QtGui/QImage>
#include <QBuffer>
#include<QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QImage* img = new QImage("F:/Muaz/Programming/Visual C++/Qt/web_browser_open_close/Image0004.jpg");

    int pixel_width = int(25 * 7 + 0.5) + 5;
    int pixel_height = int(4.0 / 3.0 * 110);

    QImage scaled_img =img->scaled(pixel_width,pixel_height,Qt::KeepAspectRatio);

    int new_h = scaled_img.height();
    int new_w = scaled_img.width();

    QByteArray sbytes;
    QBuffer buffer(&sbytes);
    buffer.open(QIODevice::WriteOnly);
    scaled_img.save(&buffer, "jpg");
    buffer.close();

    unsigned char* data =new unsigned char[sbytes.size()];
    memcpy(data,sbytes.constData(),sbytes.size());

    QImage* img1 = new QImage("F:/Muaz/Programming/Visual C++/Qt/web_browser_open_close/Image0003.jpg");

    QImage scaled_img1 =img1->scaled(pixel_width,pixel_height,Qt::KeepAspectRatio);

    qInfo() << "width: " << new_w << ", height: " << new_h;
    qInfo() << "width: " << scaled_img1.width() << ", height: " << scaled_img1.height();

    QByteArray sbytes1;
    QBuffer buffer1(&sbytes1);
    buffer1.open(QIODevice::WriteOnly);
    scaled_img1.save(&buffer1, "jpg");
    buffer1.close();

    unsigned char* data1 =new unsigned char[sbytes1.size()];
    memcpy(data1,sbytes1.constData(),sbytes1.size());

    qInfo() << sbytes.size();
    qInfo() << sbytes1.size();

    lxw_workbook  *workbook  = workbook_new("F:/Muaz/Programming/Visual C++/Qt/web_browser_open_close/sample.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, "Shop Items");

    worksheet_set_row(worksheet,0,110,NULL);
    worksheet_set_row(worksheet,1,110,NULL);
    worksheet_set_row(worksheet,2,110,NULL);
    worksheet_set_row(worksheet,3,110,NULL);
    worksheet_set_column(worksheet,1,1,25,NULL);

    lxw_image_options lio = {.x_offset =  (pixel_width-new_w)/2, .y_offset = (pixel_height-new_h)/2};

    worksheet_write_string(worksheet, 0, 0, "Ürün Adı", NULL);

    worksheet_insert_image_buffer_opt(worksheet,0,1,data,sbytes.size(), &lio);

    worksheet_write_string(worksheet, 1, 0, "Ürün Adı", NULL);

    lio = {.x_offset =  (pixel_width-scaled_img1.width())/2, .y_offset = (pixel_height-scaled_img1.height())/2};

    worksheet_insert_image_buffer_opt(worksheet,1,1,data1,sbytes1.size(), &lio);

    worksheet_write_string(worksheet, 2, 0, "Ürün Adı", NULL);

    workbook_close(workbook);

    return a.exec();
}

Now I get the following output:

在此处输入图片说明

The first image appears so small while the second one is slightly below the border line. Why is this?

How to insert the image at the center of the cell and within bounds of the cell width and height? Thanks.

You have to set the dpi to 96 (default for excel and other MS products) so that it is displayed correctly in the position at the correct size.

as QImage does have only set dot per meter i converted inch to meter then set dpm as follows:

QImage img

img.setDotsPerMeterX(3780);
img.setDotsPerMeterY(3780);

Now every thing is working as it should.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM