I found some code on the web a while back. But I don't know who wrote it.

The code below looks for worksheet names in a excel file. I am not familiar with C++ (and do little to nothing with binary files). Can someone help me convert the code below from C++ to VB.net? Thank you in advance.

trf

IStorage *pStorage = 0;
HRESULT hResult = ::StgOpenStorage(L"C:\\MyExcel.xls", NULL, STGM_READ |STGM_SHARE_DENY_WRITE, NULL, 0, &pStorage);
IStream *pStream = 0;
hResult = pStorage->OpenStream(L"Workbook", NULL, STGM_READ |STGM_SHARE_EXCLUSIVE, 0, &pStream);
char buf[32000]; DWORD dwRead = 0; short id = 0, len = 0;
    while (1) {
        hResult = pStream->Read(&id, 2, &dwRead);
            if( (FAILED(hResult)) || (dwRead != 2) ) break;
            hResult = pStream->Read(&len, 2, &dwRead);
            if( (FAILED(hResult)) || dwRead != 2 || ((id == 0) && (len == 0)) ) break;
            if( (id & 0x00FF) == 0x85 ) {
                pStream->Read(buf, len, &dwRead);
                buf[len] = 0;
                cout << buf+8 << endl;
                continue;
            }
            if(len > sizeof(buf)) len = sizeof(buf);
            pStream->Read(buf, (long)len, &dwRead);
            if((DWORD)len != dwRead) break;
    }

Recommended Answers

All 4 Replies

You should forget about converting C++ to VB.NET and look for something originally in .NET. It's a lot easier to use the excel library or ADO.NET than to hack the file format as a binary stream. ;) There are a lot of threads on reading and writing excel on the vb.net forum, go check those out.

Sorry in advance for the length.

I hear you here. I find myself in a tight spot and there isn’t much I can do. I use the .Net Report Viewer control to render Excel 97 Biff8 files. Rendering is a nice side feature of the control (the main being report viewing). I need to create Biff8 files from code running on a server. Since my code runs on a server, Excel automation is out (too many instances of it will need to run and it will crash). I could look a buying something but my employer isn’t big on that. Besides I have something working perfectly. Perfectly that is except the output doesn’t have the gridlines on. Its true BIFF8 (which is a requirement) but with the gridlines off there has been too much confusion from the LOB. So I’ve been told, get those grids on.

I’ve looked off-and-on for weeks for a solution, but have found nothing. All I could think to do at this point was to pull a document explaining the file format (http://sc.openoffice.org/excelfileformat.pdf) and try to write the updates to the files myself. It didn’t sound too bad until I tried it (ha).

So then I read up on the Biff8 for Excel, found it was an OLE2 Binary file, and went looking for anything related to updating that. Surprisingly I found some C++ articles at least talking about it. Then I found this, which appears to read through an Excel file. I figured this was at least a start.

Anyway given a more detailed explanation of my situation and why I find myself needing to reverse engineer the above C++ example, do you think anyone might be able to help me here? In the interim I’m using ADO to create basic XLS files but I will only be able to get by on that so long.

Thanks for any ideas.

You can buid a DLL and call from your .NET program.

Edit: removed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.