How to add a new row to Excel file using unmanaged C++? - c++

How can I add a new row (with contents) to an existing Excel .xls file using unmanaged C++ running on Windows?
I don't mind using OLE, COM, or any external free library, whatever is the easiest way.

There is a COM interface which is well documented.
I'd suggest you start with the Workbooks.Open method to open an existing excel file.
If you only need basic features (no formatting, formula's, ...), you can also use BasicExcel: A c++ library which doesn't have any dependencies (it reads and writes the excel file as a compound file) and is much easier to use than the COM interface (at least from c++).

I've used SQL to do this. I don't have sample code handy, but a quick google search brought this up: Link
Hope its helpful.

If you have no restrictions to use managed libraries you can check NPOI, a managed library to handle Excel file format.
Since it is managed it should be possible to register it as a COM server. If, for any reason, it proves hard/impossible to register it as a COM server you can write a thin COM server (either in C++ or C# or whatever you prefer) to expose just the functionality you need to your unmanaged C++ code.

I've used this one: ExcelFormatLib, it's great and simple to use, C++, well maintained, compiles and works without any trouble.

Related

How to write to, edit, and retrieve specific cells from an Excel doc with C++?

Basically, I want to be to be able to pass data between Excel cells and
my C++ program. I don't have any experience in Excel/C++ interactions and I haven't been able to find a coherent explanation or documentation on any websites. If someone could link me some references or provide one themselves it would be much appreciated. Thanks.
If this is for a Windows system, you could always use one of the available managed Excel libraries, such as OfficeWriter or Aspose.
There also might be similar libraries specifically for c++, I know we (OfficeWriter) used to make one.
Edit: Looks like there are a few out there, like LibXL and BasicExcel.
If the application will run on an end user machine with Excel installed, you can easily use the Excel interop and keep Excel hidden.
In addition to LibXL and BasicExcel mentioned by smoore, there is:
ExcelFormat Library is an improved version of the BasicExcel library and will allow you to read and write simple values. It is free.
xlslib will also read and write simple values, I have not tried it tho. It is also free.
Number Duck, is a commercial library that I have written, It supports reading and writing values, formulas and pictures. The website has examples of how to use the features.

Saving a webpage to disk using C++

I've managed to download a "file" from the internet with the help of wininet library, but I can't seem to save a "webpage" i.e. something I can edit later on with a text editor or with ifstream.
In this case, what are the tools I should resort to? Can wininet save a webpage to disk? Should I consider cURL (though I haven't managed to download regular files due to lack of documentation of cURL)? Do I need to learn what's called socket programming?
NB: I'm on Windows, using MinGW but can switch to MSVC if necessary, I'm looking for source code in the webpage, eventually I'm after the text in a webpage.
Also, I am not familiar with any of the functions in wininet, curl, or sockets. What do I need to learn of these?
Any help is greatly appreciated!
If your program is going to run both on windows and unix, then use cURL. Otherwise, stick with MSVC and WinINet functions http://msdn.microsoft.com/en-us/library/windows/desktop/aa385473(v=vs.85).aspx It's much easier to use in terms of the efforts required to get your program running and distributed (esp. if you're not linking your program against cUrl statically. Otherwise, you'll need to take libcurl.dll everywhere your program runs on Windows). With WinINet, you simply need to include a header and a library to use the functions.
If you're going to use WinINet, refer to this code snippet: http://www.programmershelp.co.uk/showcode.php?e=57
Use the same code except for the while loop. Instead of reading one byte at a time, read them by chunks and write them to the output file handle.
If you're going to use cURL, refer to this post: Download file using libcurl in C/C++

Help programmatically add text to an existing PDF

I need to write a program that displays a PDF which a third-party supplies. I need to insert text data in to the form before displaying it to the user. I do have the option to convert the PDF in to another format, but it has to look exactly like the original PDF. C++ is the preferred language of choice, but other languages can be investigated (e.g. C#). It need to work on a Windows desktop machine.
What libraries, tools, strategies, or other programming languages do you suggest investigate to accomplish this task? Are there any online examples you could direct me to.
Thank-you in advance.
What about PoDoFo:
The PoDoFo library is a free, portable
C++ library which includes classes to
parse PDF files and modify their
contents into memory. The changes can
be written back to disk easily. The
parser can also be used to extract
information from a PDF file (for
example the parser could be used in a
PDF viewer). Besides parsing PoDoFo
includes also very simple classes to
create your own PDF files. All classes
are documented so it is easy to start
writing your own application using
PoDoFo.
iTextSharp is a free library that you can use in .Net applications. Take a look at the iText page - that is for the iText project, which is a Java library. iTextSharp is part of that project, and is a port to C# and .Net.
Consider Python It have a lot PDF librarys (both creating and extracting) eg:
http://pypi.python.org/pypi/pdfsplit/0.4.2
http://pypi.python.org/pypi/JagPDF/1.4.0
http://pypi.python.org/pypi/pdfminer/20091129
http://pypi.python.org/pypi/podofo/0.0.1
http://pypi.python.org/pypi/pyFPDF/1.52
There are also good tools for using C/C++ code in Python and to create .exe form Python scripts. If you decide to use different language consider Python as prototyping language!

How to read/write data into excel 2007 in c++?

How to read/write data into excel 2007 in c++?
Excel provides COM interface which you can use from your C++ application.
I have experience with Excel 2003 only but I think for excel 2007 it will also work.
This can be done e.g. with #import or in the way described in this article:
http://support.microsoft.com/kb/216686
There is a python solution (using COM dispatch) here: http://snippets.dzone.com/posts/show/2036
It's not C++, but the COM interface should be the same no matter which language you use, right?
You wouldn't need to port everything. Just __init__, set_range, get_value, set_value, save (or save_as), close, and quit. You might also need to dispose of garbage (as python has automatic gc).
Or you could just port (and modify) the following code (which I haven't tested, as I don't have excel anymore - you should probably check it by downloading python and pythonwin):
from win32com.client import Dispatch
app = Dispatch("Excel.Application")
app.Visible = True # spooky - watch the app run on your desktop!
app.Workbooks.Open("C:\\book.xls")
range = app.ActiveWorkbook.Sheets(1).Range("a1")
print 'The range was:'
print range.Value
range.Value = 42
print 'The value is now 42'
app.ActiveWorkbook.Save()
app.ActiveWorkbook.SaveAs("C:\\excel2.xls")
app.ActiveWorkbook.Close()
app.Quit()
# any gc to do?
Excel 2007 files are simply zip files. Try to rename .xlsx to .zip: you can extract files and folders. With a text editor you can view that they are all XML files.
So the solution:
use a common class to unzip your xlsx
use an xml parser to grab you data
if you have modified somethig, re-zip all
No COM object required.
Depending on your c++ compiler you can easyly find the required sources.
There are three main things you need to do.
1) Ensure pre-requisite files are installed and locatable.
Blindingly obvious I know, but make sure you have a suitable version of Excel installed, so that you can locate the required Microsoft libraries (and their locations).
namely MSO.DLL, VBE6EXT.OLB and EXCEL.EXE
2) Set up the Microsoft libraries.
In your C++ code, let's say you start with a simple console application, be sure to include the import libraries in any C++ application that interfaces with Excel. In my example I use:
#import "C:\\Program Files (x86)\\Common Files\\microsoft shared\\OFFICE11\\MSO.DLL" \
rename( "RGB", "MSORGB" )
using namespace Office;
#import "C:\\Program Files (x86)\\Common Files\\microsoft shared\\VBA\\VBA6\\VBE6EXT.OLB"
using namespace VBIDE;
#import "C:\\Program Files (x86)\\Microsoft Office\\OFFICE11\\EXCEL.EXE" \
rename( "DialogBox", "ExcelDialogBox" ) \
rename( "RGB", "ExcelRGB" ) \
rename( "CopyFile", "ExcelCopyFile" ) \
rename( "ReplaceText", "ExcelReplaceText" ) \
exclude( "IFont", "IPicture" ) no_dual_interfaces
3) Use the Excel Object Model in your C++ code
For example, to declare an Excel Application Object pointer for reading / writing an Excel Workbook:
Excel::_ApplicationPtr pXL;
pXL->Workbooks->Open( L"C:\\dump\\book.xls" );
And to access and manipulate the Excel Worksheet and the cells within it:
Excel::_WorksheetPtr pWksheet = pXL->ActiveSheet;
Excel::RangePtr pRange = pWksheet->Cells;
double value = pRange->Item[1][1];
pRange->Item[1][1] = 5.4321;
And so on. I have a more in-depth discussion at this following blog posting.
A low tech way I have used on a couple of projects is to make a simple script/macro/whatever that runs an external program. Write that external program in C++. Get that external program to read its input from and write its output to a .csv file (simple comma separated value text file). Excel can easily read and write .csv files, so this setup gives you everything you need to craft a viable solution.
This can all be done via the IDispatch interface. It can get really bloody complicated quickly (Cheers Microsoft) but at least once you've got your head round Excel you'll find integrating with any other MS application easy too :)
Fortunately there is someone over on codeguru who has made the process nice and easy. Once I'd read through that code I started to get my head round what excel was doing and, furthermore, i found it became REALLY easy to extend it to do other things that I wanted. Just remember that you are sending commands to Excel via the IDispatch interface. This means you need to think about how YOU would do something to figure out how to do it programatically.
Edit: the code guru example is for Excel 2003 but it should be fairly easy to extend it to 2007 :)
Start here with the OpenXml Sdk. Download the SDK from here. The SDK uses .NET, so you might need to use C++.NET
It has been a very long time but I have used the Jet OLEDB to get at Excel files. You might start searching in that direction.
I have used a 3rd Party component for this in the past: OLE XlsFile from SM Software (not free, but inexpensive). The advantage of this component over the Microsoft COM components is that you can use it to write XLS files even if Excel is not installed.
It also allows you to create speadsheets or workbooks with embedded formulas and formatting, something not possible if you use CSV files as an interchange format.
If you need the best performance, writing binary Excel files is the way to go and writing them is not as difficult as reading them. The binary file format is relatively well documented by the OpenOffice.org project:
http://sc.openoffice.org/excelfileformat.pdf
and Microsoft has also released the documentation:
http://download.microsoft.com/download/0/B/E/0BE8BDD7-E5E8-422A-ABFD-4342ED7AD886/Excel97-2007BinaryFileFormat(xls)Specification.xps
This solution will work best if you have to write many Excel files, mostly with data and little formatting, and if you don't want to open the files at the same time. If you write a single Excel file to open it afterwards, you can probably use the propsed C++ COM Interface as the other posters have explained.
Using the COM interface will necessitate expensive out-of-process calls unless you write an Excel COM Addin. If you write an Addin that imports your data into the current Excel sheet, filling the sheet will still be much slower than dumping a file, but for a single file at a time this can be acceptable depending on your user scenario. If you do decide to use the COM interface, minimize the number of calls to Excel. Use the methods that allow to insert an array of values if they exist, set style properties by row and column is possible and not per cell.
You can use ODBC to read and write data from an excel file easily without Excel. The link:Here
The link for how to write data using odbc: Here
I've used a set of C++ classes that is available from CodeProject before to write to XLS files.
It's really easy to use, and free! You can find it here.
Took me no time to set up.

VCL alternative to IStorage

To preface I am using Borland C++ and the VCL.
I need some sort of structured storage object which can be saved to disk as a single file and can contain multiple named blobs of binary data which I can programatically enumerate, access and manipulate.
The IStorage interface seems to be close what I want but I would prefer a VCL style solution as converting TStream's (VCL) to IStream's among other things gets awkward and their must be a simpler solution using the VCL stuff allready available, like a TIniFile but for binary data.
I don't want to use XML as the binary data blobs are very large and when correctly encoded for use in XML it's completely inneficient.
Thanks in advance for all suggestions.
A zip file is de facto a standard container, and it seems you can get a TStream interface to them: http://www.tek-tips.com/faqs.cfm?fid=6734
SolFS (Solid File System) from Eldos.
http://www.eldos.com/solfs/
Very reliable, but might not be the cheapest solution ($372 for one developer).
I went for ZipForge by ComponentAce. It's fast, is being actively developed/supported and has the perfect interface to support what I want.
Thanks for everybodys other suggestions.
If you don't want to store huge file(it has 2GB limit), GPStructuredStorage might be handy.
It's a open source structured storage library written in pure delphi, still maintained and seems to be worked with D2010.