Writting Excel file

Discussion in 'C Programming' started by Abhay, Feb 21, 2008.

  1. Abhay

    Abhay Guest

    How can i write a excel file through c program.?
    Abhay, Feb 21, 2008
    #1
    1. Advertising

  2. Abhay

    jacob navia Guest

    Abhay wrote:
    > 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
    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Feb 21, 2008
    #2
    1. Advertising

  3. Abhay

    jacob navia Guest

    Abhay wrote:
    > 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.


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Feb 21, 2008
    #3
  4. Abhay

    santosh Guest

    Abhay wrote:

    > 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?
    santosh, Feb 21, 2008
    #4
  5. "Abhay" <> wrote in message
    news:...
    > 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.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Feb 21, 2008
    #5
  6. Abhay

    CBFalconer Guest

    Malcolm McLean wrote:
    > "Abhay" <> wrote:
    >
    >> How can i write a excel file through c program.?

    >
    > Probably what you want is a coma-separated values file.


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

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Feb 21, 2008
    #6
  7. In article <>,
    CBFalconer <> wrote:
    >Malcolm McLean wrote:
    >> "Abhay" <> wrote:
    >>
    >>> How can i write a excel file through c program.?

    >>
    >> Probably what you want is a coma-separated values file.

    >
    >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.
    Kenny McCormack, Feb 21, 2008
    #7
  8. Björn Paetzel, Feb 21, 2008
    #8
  9. On 21 Feb 2008 at 14:26, CBFalconer wrote:
    > Malcolm McLean wrote:
    >> "Abhay" <> wrote:
    >>
    >>> How can i write a excel file through c program.?

    >>
    >> Probably what you want is a coma-separated values file.

    >
    > 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.

    > --
    > [mail]: Chuck F (cbfalconer at maineline dot net)
    > [page]: <http://cbfalconer.home.att.net>
    > Try the download section.
    >
    >
    >


    Oh, and fix your fricking signature.
    Antoninus Twink, Feb 21, 2008
    #9
  10. Abhay

    Randy Howard Guest

    On Thu, 21 Feb 2008 06:45:11 -0600, jacob navia wrote
    (in article <fpjrot$46r$>):

    > Abhay wrote:
    >> 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.


    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.

    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
    Randy Howard, Feb 22, 2008
    #10
  11. Abhay wrote:
    >
    > 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:>
    Kenneth Brody, Feb 22, 2008
    #11
  12. santosh wrote:
    > Abhay wrote:
    >
    >> 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?
    >


    Ggggg-g-g-g. :)
    Oleksandr Gavenko, Feb 24, 2008
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boris Condarco

    writting excel files

    Boris Condarco, Oct 12, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,884
  2. =?Utf-8?B?RmVuZw==?=
    Replies:
    1
    Views:
    540
    Craig Deelsnyder
    Jun 13, 2005
  3. Replies:
    9
    Views:
    604
  4. Replies:
    1
    Views:
    361
    Oliver Wong
    Feb 7, 2006
  5. Replies:
    1
    Views:
    312
    Dimitri Maziuk
    Feb 7, 2006
Loading...

Share This Page