C++ xlnt 插入图片
时间: 2023-09-13 22:08:24 浏览: 107
使用 xlnt 库插入图片需要先将图片读入内存,然后使用 `xlnt::worksheet::insert_image` 方法插入图片。以下是一个示例代码:
```c++
#include <iostream>
#include <fstream>
#include <xlnt/xlnt.hpp>
int main()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
// 读取图片
std::ifstream img_file("image.png", std::ios::binary);
std::vector<unsigned char> img_data((std::istreambuf_iterator<char>(img_file)), std::istreambuf_iterator<char>());
// 插入图片
auto img = ws.get_drawings().create_image("A1", img_data);
img.set_name("image.png");
wb.save("example.xlsx");
return 0;
}
```
其中,`img_data` 是存放图片数据的 `vector`,可以使用任何方法读取图片数据并存放到该 `vector` 中。在这个例子中,我们使用 `std::ifstream` 读取图片文件,并将文件数据存放到 `img_data` 中。
`xlnt::worksheet::insert_image` 方法的第一个参数是图片的位置,这里我们使用单元格的坐标 `"A1"`。第二个参数是图片数据,即 `img_data`。在这个例子中,我们将图片的名称设置为 `"image.png"`。
最后,使用 `xlnt::workbook::save` 方法将结果保存到文件中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)