在 C++ 中从 Excel 读取单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13200809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:04:46  来源:igfitidea点击:

Read cells from Excel in C++?

c++excelfstream

提问by Carpetfizz

I was wondering how you can read specific cells from an Excel spreadsheet, in C++. I understand we have to use the "fstream" library, but I don't know exactly how I could get those values from a certain cell, and print it on the screen. Any help would be appreciated, thanks! Carpetfizz

我想知道如何在 C++ 中从 Excel 电子表格中读取特定单元格。我知道我们必须使用“fstream”库,但我不知道如何从某个单元格获取这些值并将其打印在屏幕上。任何帮助将不胜感激,谢谢!地毯汽水

回答by 0x90

in linux you have this free: http://libxls.sourceforge.net/

在 linux 中你有这个免费的:http: //libxls.sourceforge.net/

in windows you have http://www.libxl.com/which seems to cost money:

在 Windows 中,您有http://www.libxl.com/这似乎要花钱:

Book* book = xlCreateBook();
if(book)
{
    if(book->load(L"example.xls"))
    {
        Sheet* sheet = book->getSheet(0);
        if(sheet)
        {
            const wchar_t* s = sheet->readStr(2, 1);
            if(s) wcout << s << endl;

            double d = sheet->readNum(3, 1);
            cout << d << endl;
        }
    }

I think the best thing to do is to save the files as .csvwhich is more friendly to work with.

我认为最好的办法是将文件保存.csv为更友好的使用方式。

more references:

更多参考:

  1. What is a simple and reliable C library for working with Excel files?

  2. Reading from and writing to Excel files in C++

  1. 什么是用于处理 Excel 文件的简单可靠的 C 库?

  2. 在 C++ 中读取和写入 Excel 文件

回答by 0x90

Excel versions before Excel 2007 use a proprietary binary format, but Excel 2007 and later versions use XML (writes Wikipedia).

Excel 2007 之前的 Excel 版本使用专有的二进制格式,但 Excel 2007 及更高版本使用 XML(维基百科写道)。

There's also a C++ libraryfor dealing with Excel files.

还有一个用于处理 Excel 文件的 C++ 库

回答by HumbleWebDev

For writing, use:

对于写作,请使用:

https://sourceforge.net/projects/simplexlsx/

https://sourceforge.net/projects/simplexlsx/

For reading, I use:

为了阅读,我使用:

sourceforge.net/projects/xlsxio/?source=directory

sourceforge.net/projects/xlsxio/?source=directory

Also, for xlsxio, I wrote an OO wrapper, to make it more friendly for c++ integration. It only supports reading, but you should probable use simplexlsx for writing anyways!

此外,对于 xlsxio,我编写了一个 OO 包装器,以使其对 C++ 集成更加友好。它只支持阅读,但无论如何你应该使用 simplexlsx 进行写作!

#include "XlsxBook.h"
#include "XlsxSheet.h"

https://drive.google.com/file/d/0B_HJu4VOsY8hMnRla2NMOEM3Z2M/view?usp=sharing

https://drive.google.com/file/d/0B_HJu4VOsY8hMnRla2NMOEM3Z2M/view?usp=sharing