Writting Excel file

J

jacob navia

Abhay said:
How can i write a excel file through c program.?

lcc-win provides an example using the COM dispatch
interface. You can have the example if you buy the
lcc-win full version.

Basically you
1) First you obtain the Excel dispatch interface with:

if (FAILED(CLSIDFromProgID(L"Excel.Application", &clsExcelApp)))
{
// Handle error
return FALSE;
}
// start a new copy of Excel, grab the IDispatch interface
if (FAILED(CoCreateInstance(&clsExcelApp, NULL, CLSCTX_LOCAL_SERVER,
&IID_IDispatch, &pdispExcelApp)))
{
// Handle error
return FALSE;
}


2) Once you have the dispatch interface, you call the appropriate
routines:

BOOL CreateChart()
{
BOOL fResult;
VARIANTARG varg1, varg2;
IDispatch *pdispWorkbook = NULL;
IDispatch *pdispWorksheet = NULL;
IDispatch *pdispRange = NULL;
IDispatch *pdispCrt = NULL;
LPOLESTR apszNames[4] = { L"North", L"South", L"East", L"West" ;

// Set wb = [application].Workbooks.Add(template := xlWorksheet)
ClearAllArgs();
if (!Invoke(pdispExcelApp, L"Workbooks", &varg1,
DISPATCH_PROPERTYGET, 0))
return FALSE;

ClearAllArgs();
AddArgumentInt2(L"Template", 0, xlWorksheet);
fResult = Invoke(varg1.pdispVal, L"Add", &varg2,
DISPATCH_METHOD, 0);
ReleaseVariant(&varg1);
if (!fResult)
return FALSE;
pdispWorkbook = varg2.pdispVal;

// Set ws = wb.Worksheets(1)
ClearAllArgs();
AddArgumentInt2(NULL, 0, 1);
if (!Invoke(pdispWorkbook, L"Worksheets", &varg2,
DISPATCH_PROPERTYGET, 0))
goto CreateChartBail;
pdispWorksheet = varg2.pdispVal;

// SETUP THE DATA LABELS
// ws.Range("A1:D1").Value = Array("North",
// "South", "East", "West")
ClearAllArgs();
AddArgumentCString(NULL, 0, L"A1:D1");
if (!Invoke(pdispWorksheet, L"Range", &varg2,
DISPATCH_PROPERTYGET, DISP_FREEARGS))
goto CreateChartBail;

AddArgumentCStringArray(NULL, 0, apszNames, 4);
fResult = Invoke(varg2.pdispVal, L"Value", NULL,
DISPATCH_PROPERTYPUT, DISP_FREEARGS);
ReleaseVariant(&varg2);
if (!fResult)
goto CreateChartBail;

// SETUP THE DATA SERIES VALUES
// ws.Range("A2") = 5.2
if (!SetRangeValueDouble(pdispWorksheet, L"A2", 5.2))
goto CreateChartBail;
// ws.Range("B2") = 10
if (!SetRangeValueDouble(pdispWorksheet, L"B2", 10.0))
goto CreateChartBail;
// ws.Range("C2") = 8
if (!SetRangeValueDouble(pdispWorksheet, L"C2", 8.0))
goto CreateChartBail;
// ws.Range("D2") = 20
if (!SetRangeValueDouble(pdispWorksheet, L"D2", 20))
goto CreateChartBail;
// set sourceRange = ws.Range("A1:D2")
ClearAllArgs();
AddArgumentCString(NULL, 0, L"A1:D2");
if (!Invoke(pdispWorksheet, L"Range", &varg2,
DISPATCH_PROPERTYGET, DISP_FREEARGS))
goto CreateChartBail;
pdispRange = varg2.pdispVal;

// set crt = wb.Charts.Add
ClearAllArgs();
if (!Invoke(pdispWorkbook, L"Charts", &varg1, DISPATCH_PROPERTYGET, 0))
goto CreateChartBail;
ClearAllArgs();
fResult = Invoke(varg1.pdispVal, L"Add", &varg2, DISPATCH_METHOD, 0);
ReleaseVariant(&varg1);
if (!fResult)
goto CreateChartBail;
pdispCrt = varg2.pdispVal;

// crt.ChartWizard source := sourceRange,
// gallery := xl3DPie, format := 7, _
// plotBy := xlRows, categoryLabels := 1,
// seriesLabels := 0, hasLegend := 2, _
// title := "Sales Percentages"
ClearAllArgs();
AddArgumentCString(L"title", 0, L"Sales Percentages");
AddArgumentInt2(L"hasLegend", 0, 2);
AddArgumentInt2(L"seriesLabels", 0, 0);
AddArgumentInt2(L"categoryLabels", 0, 1);
AddArgumentInt2(L"plotBy", 0, xlRows);
AddArgumentInt2(L"format", 0, 7);
AddArgumentInt2(L"gallery", 0, xl3DPie);
// will auto-free
AddArgumentDispatch(L"source", 0, pdispRange);
pdispRange = NULL;
if (!Invoke(pdispCrt, L"ChartWizard", NULL, DISPATCH_METHOD,
DISP_FREEARGS))
goto CreateChartBail;

// wb.Saved = True
// ' So that Excel won't ask whether to save this
// document on close.
ClearAllArgs();
AddArgumentBool(NULL, 0, TRUE);
Invoke(pdispWorkbook, L"Saved", NULL, DISPATCH_PROPERTYPUT, 0);

fResult = TRUE;

CreateChartExit:
if (pdispWorkbook != NULL)
(*(pdispWorkbook->lpVtbl->Release))(pdispWorkbook);
if (pdispWorksheet != NULL)
(*(pdispWorksheet->lpVtbl->Release))(pdispWorksheet);
if (pdispRange != NULL)
(*(pdispRange->lpVtbl->Release))(pdispRange);
if (pdispCrt != NULL)
(*(pdispCrt->lpVtbl->Release))(pdispCrt);
return fResult;

CreateChartBail:
fResult = FALSE;
goto CreateChartExit;
}

3) You will need a lot of support code for this, that would be too
lengthy to post here, but this is basically how you do it.

lcc-win provides a lot of support for this kind of programming. You
can reead more at the URL below
 
J

jacob navia

Abhay said:
How can i write a excel file through c program.?

I think I misunderstood your question. I thought you wanted to send
data to Excel, but mayybe you want to just write a CSV file.
In the later case ignore my message.
 
S

santosh

Abhay said:
How can i write a excel file through c program.?

Like all other text files. Using fprintf or similar functions.
Do you have a specific problem?
 
M

Malcolm McLean

Abhay said:
How can i write a excel file through c program.?
Probably what you want is a coma-separated values file.

These are easy to write - just call fprintf with commas and newlines in the
right place - harder to read. However you can download a reader from my
website.

To include Excel entities like charts you will need to know the Excel
format. Jacob Navia seems to know where to obtain a libbrary of writing
funntions from.
 
K

Kenny McCormack

How do you plan to introduce each coma? With a blunt instrument?

Keep in mind that neither commas nor comas are good choices for
delimiters. The best, native, delimiter for Excel is TAB.
 
A

Antoninus Twink

How do you plan to introduce each coma? With a blunt instrument?

Marvelous. CBF was clearly so excited by his little bit of humor that he
even forgot to berate the OP for an OT post, and Maclean for an OT
reply.

Oh, and fix your fricking signature.
 
R

Randy Howard

I think I misunderstood your question. I thought you wanted to send
data to Excel, but mayybe you want to just write a CSV file.
In the later case ignore my message.

It could be that wants to write a .xls file directly. I would guess
that before either your original guess or a .csv file. Either way,
it's not well specified, and probably better answered in some group
specifically for Microsoft programming.
 
K

Kenneth Brody

Abhay said:
How can i write a excel file through c program.?

Use fopen() to open the file, making sure to specify binary mode.
Use fwrite() to write your data.
Use fclose() to close the file when you're done.

As to _what_ to write, you'll need to go elsewhere to find specs on
the Excel file format. Perhaps this URL is what you're looking for:

http://www.microsoft.com/interop/docs/OfficeBinaryFormats.mspx

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top