我刚开始使用libxlsxwriter来创建excel表。我的问题是我不知道如何集中图片以及如何以原始尺寸打印图片。只考虑该组只有3列,每列具有不同的大小,例如30,10,20。我需要知道找到偏移量和比例值需要什么计算。
lxw_image_options options = {.x_offset = 0, .y_offset = 0,.x_scale = 0.9, .y_scale = 0.9};
worksheet_insert_image_opt(worksheet, row, 0, img_path, &options);
有了这个,我需要知道图片可以容纳多少行。然后我才能创建即将到来的集合而不重叠。
答案 0 :(得分:1)
图片有多少行?
我想在这里展示我的发现以即兴表达结果。
int row=1;
lxw_image_options options = {.x_offset = 0, .y_offset = 0};
worksheet_insert_image_opt(worksheet, row, 2,"logo.png", &options);
row+=(options.height/worksheet->default_row_pixels);
这里我使用变量 options.height 来计算图片保存的行数。 libxlsxwriter确实从图像文件读取高度(以像素为单位)。它仅对读取初始化变量使用struct变量选项,它永远不会在集合中写入任何内容。但我在工作表单的 worksheet_insert_image_opt 函数中添加了 user_options-> height = options-> height; 这一行。
lxw_error worksheet_insert_image_opt(lxw_worksheet *self,
lxw_row_t row_num, lxw_col_t col_num,
const char *filename,
lxw_image_options *user_options)
{
FILE *image_stream;
char *short_name;
lxw_image_options *options;
if (!filename) {
LXW_WARN("worksheet_insert_image()/_opt(): "
"filename must be specified.");
return LXW_ERROR_NULL_PARAMETER_IGNORED;
}
/* Check that the image file exists and can be opened. */
image_stream = fopen(filename, "rb");
if (!image_stream) {
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"file doesn't exist or can't be opened: %s.",
filename);
return LXW_ERROR_PARAMETER_VALIDATION;
}
/* Get the filename from the full path to add to the Drawing object. */
short_name = lxw_basename(filename);
if (!short_name) {
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"couldn't get basename for file: %s.", filename);
fclose(image_stream);
return LXW_ERROR_PARAMETER_VALIDATION;
}
/* Create a new object to hold the image options. */
options = calloc(1, sizeof(lxw_image_options));
if (!options) {
fclose(image_stream);
return LXW_ERROR_MEMORY_MALLOC_FAILED;
}
if (user_options) {
memcpy(options, user_options, sizeof(lxw_image_options));
options->url = lxw_strdup(user_options->url);
options->tip = lxw_strdup(user_options->tip);
}
/* Copy other options or set defaults. */
options->filename = lxw_strdup(filename);
options->short_name = lxw_strdup(short_name);
options->stream = image_stream;
options->row = row_num;
options->col = col_num;
if (!options->x_scale)
options->x_scale = 1;
if (!options->y_scale)
options->y_scale = 1;
if (_get_image_properties(options) == LXW_NO_ERROR) {
user_options->height=options->height;
STAILQ_INSERT_TAIL(self->image_data, options, list_pointers);
return LXW_NO_ERROR;
}
else {
free(options);
return LXW_ERROR_IMAGE_DIMENSIONS;
}
}
这是我如何计算行数。如果有更好的方法,请告诉我。
答案 1 :(得分:0)
如何以原始尺寸打印图片
libxlsxwriter根据图像中的宽度,高度和DPI信息,将图像以原始大小插入xlsx文件。它应该以与通过用户界面在Excel中执行的方式完全相同的方式插入图像。
但是,OpenOffice或LibreOffice中的图像可能显示的大小不正确。这不是一个libxlsxwriter问题:Excel也会发生同样的事情。
要在精确位置插入图像,您需要知道图像的尺寸(以像素为单位)以及DPI,因为Excel会根据它的默认DPI(通常为96)进行缩放。以下是插入两个图像的示例一排:
/*
* An example of inserting images with the libxlsxwriter library.
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("demo.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_image_options options = {.x_offset = 0, .y_offset = 0,
.x_scale = 1, .y_scale = 1};
double default_dpi = 96.0;
double image_dpi = 90.0;
int32_t image_height = 138;
int32_t image_offset = (int32_t)(image_height * default_dpi/image_dpi);
/* Insert the first image. */
worksheet_insert_image(worksheet, 1, 2, "logo.png");
/* Insert the second image relative to the first. */
options.y_offset += image_offset;
worksheet_insert_image_opt(worksheet, 1, 2, "logo.png", &options);
workbook_close(workbook);
return 0;
}
输出: