0

I downloaded LibXL to help modify a C++ program I've created. I have a bunch of global variables in my C++ file that I'd like to link to values in an excel spreadsheet. Can someone please explain how I'd do this if the excel sheet I'm linking to is called "Sheet 1" and the Workbook is called "Book 1". This is the code sample on the LibXL site for extracting data from an excel spreadsheet:

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;
        }
    }

    book->release();
}

Here is the block of code from my C++ program where I want to link certain excel cells to variables in C++:

{var1 = .0887; var2 = .175; var3 = .299; var4 = .292; var5 = .151; var6 = .051; var7 = .001; var8 = .02;}

My code that needs to be edited just features various variable names. I would want those variables/values to be linked to or equal to variables in an excel spreadsheet with the above name and book. I really wasn't sure how to implement this sample code I've shown, partially bc I did not understand the above sample.

  • So, which part(s) of the posted code do you not understand? – Mats Petersson Jul 13 '14 at 08:55
  • Well, I am not sure a) where to put this code w/in my file and how to run it (does it go inside main?)? b)What exactly is the name of the excel book and worksheet in the example? (i.e. what part of that am I changing and how would I relate it to my var1, var2). Just to mention also var1, var2, etc. those are all defined in my main() function. – user3753931 Jul 13 '14 at 09:01
  • Sheets are referenced by numbers in the example (0 being the first one, 1 being the second one). I think there is a way to get "names" of sheets, but it's more complex. How it relates to your code is impossible to say, since you haven't posted your code, nor what your spreadsheet looks like. You probably need some sort of translation from the "name" in the spreadsheet to your variables, which isn't something that C++ in itself supports. – Mats Petersson Jul 13 '14 at 09:06
  • Ok - so it looks like getSheet(0) relates to the worksheet and "example.xls" is the workbook. Can you explain how the readStr and readNum work? What are those relating to (seems like they'd be referencing the cells, but I don't follow that syntax) – user3753931 Jul 13 '14 at 09:10
  • As far as I understand readStr returns the string in the relevant cell, readNum returns a number from the cell. (I have not used LibXL, but have used a similar functionality in Python, in a program that used Excel as a "scripting language"). – Mats Petersson Jul 13 '14 at 09:15
  • Ok so you think something like readNum(3,1) returns a number if there is a number in the fourth column and second row (or vice versa for row and column) in otherwords cell D2 (or B4, I could try to verify this). If it is a string what would readNum return? And if its a number what would readStr return? Do I have to check each cell to make sure it returns the right type (I'm only worried about extracting numbers so I guess I'd just use readNum?)? – user3753931 Jul 13 '14 at 09:22
  • I expect "readStr" to return the string value in the cell, even if it's a number. But like I said, I haven't used this library (and it's two years since I did this in Python). There is a LibXL documentation page: http://libxl.com/spreadsheet.html?lang=cpp – Mats Petersson Jul 13 '14 at 09:27
  • Ok - does this go inside the main function? Do I list the full path for where the workbook is located or does it need to be in the same directory as the C++ program I am writing? I am working on just trying to mess around w the example code in a blank C++ program. – user3753931 Jul 13 '14 at 16:28
  • Where is "goes" is not something I can tell, since I still don't know what your code looks like or what, in a bigger picture, you are trying to achieve - I personally like to make `main` very short, and put things in other functions - you need a path that works for where the file is, easiest is to either use a full absolute path or place it in the same directory as your application (and no path), but any path that leads to the file should work. – Mats Petersson Jul 13 '14 at 16:37
  • Hi - sorry I am still having a little trouble, but don't want to outright share what my code is. Basically, I define the var1, var2 variables mentioned above in my main() function. Those are hardcoded. I then pass those into various methods that are called inside the main() function but defined outside my main() function. Where would I put this example code if I want to link the readStr outputs to my var1, var2, var3 variables? Also, do I implemement this sample code inside or outside my main() function? I understand you have not worked with this in a while, but any clues are very helpful. – user3753931 Jul 13 '14 at 17:35
  • I understand that you don't want to share your code, I'm just saying that I can't tell you what is the best thing to do, if I don't know what it looks like. What do you mean by "link readStr to var1", you mean like `if (sheet->readStr(...) == "var1") var1 = ...`? – Mats Petersson Jul 13 '14 at 17:41
  • I am saying I want to make var1 equal to the value returned by readStr () ..i.e. the data extracted should be put into a variable (var1, var2, etc.) and that will then be used by the rest of my program. Thanks again for all your help. – user3753931 Jul 14 '14 at 04:33

1 Answers1

0

use sdt::wcout for wide chars which are used in libXL. Also use if CELLTYPE_NUMBER / CELLTYPE_STRING and using namespace libxl!

You can use example: http://www.libxl.com/read-excel-data.html and simplize it.

Robert
  • 5,278
  • 43
  • 65
  • 115
pomaranga
  • 1
  • 2