c++读取包含数据和图表的Excel文件

发布于 2024-10-28 00:09:02 字数 126 浏览 1 评论 0原文

我正在使用 C++ 编写一个程序来读取 Excel 文件。我只需要文件的两列,而 Excel 文件只是不包含字段...它还包含图表。有 10 列,我需要的两列是第一列和第七列。转换成 CSV 不起作用。任何建议请。

谢谢。

I am writing a program to read a excel file using c++. i need only the two columns of the file and the excel file just dont contain fields...it also contains graphs. there are 10 columns and the two columns i need are the first and 7th. converting into CSV didnt work. Any suggestions plz.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半夏半凉 2024-11-04 00:09:02

您需要将文件另存为 CSV,然后使用 fopen 读入 CSV 文件。您还需要开始自己做作业,而不是向互联网上的人寻求帮助。

You need to save the file as CSV and then read in the CSV file using fopen. You also need to start doing your homework on your own and not asking people on the internet for help.

提笔书几行 2024-11-04 00:09:02

Excel 可以通过其 COM 接口以编程方式驱动。下面是一些代码:

#include <windows.h>
#include <iostream>

#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL" rename( "RGB", "MSORGB" ), rename( "DocumentProperties", "MSODocProps" )
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" rename ( "Application", "VBApplication" )

namespace Excel
{
    using namespace Office;
    using namespace VBIDE;
}

#import "C:\Program Files\Microsoft Office\OFFICE11\Excel.EXE" \
    rename( "RGB", "RGB_" ), \
    rename( "CopyFile", "CopyFile_" ), \
    rename( "DialogBox", "DialogBox_" ), \
    rename( "ReplaceText", "ReplaceText_" )

int main(int argc, char* argv[])
{
    ::CoInitialize( NULL );

    try
    {
        Excel::_ApplicationPtr excel( "Excel.Application" );
        Excel::WorkbooksPtr wbs = excel->Workbooks;
        Excel::_WorkbookPtr wb = wbs->Open( _bstr_t( "c:\\temp\\test.xls" ) );
        Excel::_WorksheetPtr sheet = wb->Sheets->GetItem( 1 ); // First sheet
        Excel::RangePtr range = sheet->Cells->GetItem( 1, 1 ); // Row, column
        _variant_t value = range->Value;
    }   
    catch( _com_error& e )
    {
        std::cerr << "0x" << std::hex << e.Error() << std::endl;
    }
    catch( ... )
    {
        std::cerr << "Unexpected error" << std::endl;
    }

    ::CoUninitialize();
    return 0;
}

该代码获取从光盘加载的电子表格中单元格 A1 中的值。您应该能够从这里开始读取两列数据。

Excel can be driven programatically through its COM interface. Here is some code:

#include <windows.h>
#include <iostream>

#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL" rename( "RGB", "MSORGB" ), rename( "DocumentProperties", "MSODocProps" )
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" rename ( "Application", "VBApplication" )

namespace Excel
{
    using namespace Office;
    using namespace VBIDE;
}

#import "C:\Program Files\Microsoft Office\OFFICE11\Excel.EXE" \
    rename( "RGB", "RGB_" ), \
    rename( "CopyFile", "CopyFile_" ), \
    rename( "DialogBox", "DialogBox_" ), \
    rename( "ReplaceText", "ReplaceText_" )

int main(int argc, char* argv[])
{
    ::CoInitialize( NULL );

    try
    {
        Excel::_ApplicationPtr excel( "Excel.Application" );
        Excel::WorkbooksPtr wbs = excel->Workbooks;
        Excel::_WorkbookPtr wb = wbs->Open( _bstr_t( "c:\\temp\\test.xls" ) );
        Excel::_WorksheetPtr sheet = wb->Sheets->GetItem( 1 ); // First sheet
        Excel::RangePtr range = sheet->Cells->GetItem( 1, 1 ); // Row, column
        _variant_t value = range->Value;
    }   
    catch( _com_error& e )
    {
        std::cerr << "0x" << std::hex << e.Error() << std::endl;
    }
    catch( ... )
    {
        std::cerr << "Unexpected error" << std::endl;
    }

    ::CoUninitialize();
    return 0;
}

This code gets the value in cell A1 in a spreadsheet loaded from disc. You should be able to go from here to reading your two columns of data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文