-
April 15th, 2017, 05:48 AM
#1
Automate Excel
Hello. I new am these forum. Please help provide if can.
I want automate Excel from c++. I confidant am with c++. I have example code open Excel spreadsheet read/write cells. This work good. I find not documentation for spreadsheet functions available. Most for c#, VB not c++. Where is com model Excel c++ documentation please.
Is com best Excel automate - other ways better?
Thank you for help.
-
April 16th, 2017, 03:36 AM
#2
-
April 16th, 2017, 07:46 AM
#3
Re: Automate Excel
Thank for the reply. That is MFC that I not use. I use ordinary c++. It not code I try to find but documentation Excel com model c++. If have pointer to worksheet I get, what methods available can be used please? How set range for use? For Windows API Microsoft has documentation internet describe functions for c++. Where similar Excel com methods c++ documented please.
-
April 16th, 2017, 11:09 AM
#4
-
April 18th, 2017, 03:57 AM
#5
Re: Automate Excel
Thank for reply. Yes I aware of that. But same problem. Where documentation is Excel model? Examples used ActiveSheet, Add, Range, Value, Saved. Where documentation these and all available c++ please.
-
April 18th, 2017, 11:51 AM
#6
Re: Automate Excel
Victor Nijegorodov
-
April 18th, 2017, 01:33 PM
#7
Re: Automate Excel
Thank for reply. I know not MFC as taught c++ only. I know not VBA why want documentation for c++.
If pointer to worksheet how insert row after required row? Where documented for c++ - not VBA. How to change cell colour How to set bold. Where is object model documentation for c++ - not VBA.
Thank you.
Last edited by auto2000; April 18th, 2017 at 02:04 PM.
Reason: Spell
-
April 18th, 2017, 01:44 PM
#8
Re: Automate Excel
Originally Posted by auto2000
Where is object model documentation for c++ - not VBA.
I guess there is no any "c++" object model documentation, only VBA one.
However there is a lot of c++ wrapper classes to use this object model using MFC. I did it about a decade back. And I guess it still does work.
Last edited by 2kaud; April 19th, 2017 at 04:31 AM.
Victor Nijegorodov
-
April 19th, 2017, 02:51 AM
#9
Re: Automate Excel
This my test code
Code:
#include "imports.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace Excel;
using namespace std;
const string sname = "c:\\My files\\admin\\test.xls";
int main()
{
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
{
cout << "Failed to initialize COM library. Error code = 0x" << hex << hr << endl;
return 1;
}
_ApplicationPtr pXL;
if (FAILED(pXL.CreateInstance("Excel.Application")))
{
cout << "Failed to initialize Excel::_Application!" << endl;
return 2;
}
_WorkbookPtr pBook;
try {
pBook = pXL->Workbooks->Open(sname.c_str());
}
catch (...) {
cout << "Cannot open file" << endl;
pXL->Quit();
return 0;
}
pXL->PutVisible(0, FALSE);
_WorksheetPtr pWksheet = pXL->ActiveSheet;
//_WorksheetPtr pSheet = pBook->Sheets->Item[1];
RangePtr pRange = pWksheet->Cells;
for (int c = 5; (double)pRange->Item[c][3] != 0; ++c)
if ((double) pRange->Item[c][4] != 0)
cout << setprecision(0) << (double)pRange->Item[c][3] << " " << fixed << setprecision(2) << (double)pRange->Item[c][4] << endl;
/*
//pWksheet->Name = L"Sheet1";
// Read an Excel data cell. (Note Excel cells start from index = 1)
double value1 = pRange->Item[1][1];
cout << "Value in CELL [1][1] = " << value1 << endl;
double value2 = pRange->Item[1][2];
cout << "Value in CELL [1][2] = " << value2 << endl;
// Write/modify Excel data cells + save. (reopen xls file to verify)
cout << endl << "Modifying Excel cell values..." << endl << endl;
pRange->Item[1][1] = 10;
pRange->Item[1][2] = 11;
// Output new value
value1 = pRange->Item[1][1];
cout << "New value in CELL [1][1] = " << value1 << endl;
value2 = pRange->Item[1][2];
cout << "New value in CELL [1][2] = " << value2 << endl;
// Switch off alert prompting to save as
pXL->PutDisplayAlerts(LOCALE_USER_DEFAULT, VARIANT_FALSE);
// Save the values in book.xml and release resources
pWksheet->SaveAs(sname.c_str());
// And switch back on again...
pXL->PutDisplayAlerts(LOCALE_USER_DEFAULT, VARIANT_TRUE);
*/
pBook->Release();
pWksheet->Release();
pXL->Quit();
}
For pointer pWksheet what methods available - what are params? and for pRange? How to insert row? How to format a cell - range of cell?
Please help.
Last edited by auto2000; April 19th, 2017 at 02:53 AM.
Reason: Spell
-
April 19th, 2017, 03:30 AM
#10
Re: Automate Excel
Victor Nijegorodov
-
April 19th, 2017, 04:23 AM
#11
Re: Automate Excel
Thank. That was looking to find.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|