找了一些用com 操作 excel 的资料,
都是用模板才能产生新的 .xls文件,
谁能给一个不用模板的例子, 给出关键的代码片断就行,
比如//////////////////////
_Application app;
app.CreateDispatch("Excel.Application",NULL); 
 
Workbooks workbooks =app.GetWorkbooks();_Workbook workbook = workbooks.Add(_variant_t("d:\\a\\Book1.xls")); /////////////////////////add 是 打开已经存在的 文件, 如何 不用模板 创建新的文件呢? 谢谢

解决方案 »

  1.   

    DWORD WINAPI CExportXLSDlg::_ExportThread(LPVOID pParameter)
    {
    CExportXLSDlg *pDlg = (CExportXLSDlg*)pParameter; _Application *pExcelApp;
    Workbooks objExcelBooks;
    Sheets objExcelSheets;
    _Workbook objWorkBook;
    _Worksheet objWorkSheet;
    Range objRange; COleVariant varOptional(DISP_E_PARAMNOTFOUND, VT_ERROR);
    COleVariant varTrue((short)TRUE);
    COleVariant varFalse((short)FALSE);
    MSG msg;
    CString strText;

    int nMilliTime = 0;
    int nSequence = 0, nLastSequence = -1;
    int nRow = 1;
    int i;
    double dblValue[TOTAL_CHANNEL_NUM]; CoInitialize(NULL); // ÒòΪ¶ÔÓÚCOM²Ù×÷±ØÐë¶Ôÿ¸öÏß³ÌÒýÓàCOM ¿â pDlg->m_staticProgress.SetWindowText(_T("ÕýÔÚ´´½¨ExcelʵÀý..."));

    pExcelApp = new _Application;
    pExcelApp->CreateDispatch(_T("Excel.Application")); // ´´½¨ Excel Ó¦ÓóÌÐò

    objExcelBooks = pExcelApp->GetWorkbooks();
    objExcelSheets = objExcelBooks.Add(varOptional);
    objWorkBook.AttachDispatch(pExcelApp->GetApplication());
    objExcelSheets = objWorkBook.GetSheets();
    // Ïȵ¼³öÀúÊ·Êý¾ÝÏà¹ØÐÅÏ¢
    pDlg->m_staticProgress.SetWindowText(_T("ÕýÔÚµ¼³öÀúÊ·Êý¾ÝÐÅÏ¢..."));
    objWorkSheet = objExcelSheets.GetItem(COleVariant((short)1));
    objWorkSheet.SetName(_T("ÀúÊ·Êý¾ÝÐÅÏ¢"));
    objWorkSheet.Activate();

    __ExportInfomation(objWorkSheet);

    // µ¼³öÀúÊ·ÊÔÑéÊý¾Ý
    pDlg->m_staticProgress.SetWindowText(_T("ÕýÔÚµ¼³öÀúÊ·ÊÔÑéÊý¾Ý..."));
    objWorkSheet = objExcelSheets.GetItem(COleVariant((short)2));
    objWorkSheet.SetName(_T("ÊÔÑéÊý¾Ý"));
    objWorkSheet.Activate();
    __ExportDataHead(objWorkSheet); nSequence = 0;
    while (nSequence < g_nHistoryCount)
    {
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
    if (msg.message == WM_QUIT)
    break;
    }

    if (nSequence > nLastSequence)
    {
    nRow ++; __GetTimeString(nSequence, strText);
    __SetExcelCellText(objWorkSheet, nRow, 1, strText, 1, 2); __GetHistoryRecord(nSequence, dblValue);
    strText.Format(_T("%.3f"), dblValue[BASEID_COOL]);
    __SetExcelCellText(objWorkSheet, nRow, 3, strText);
    for (i=0; i<T_CHANNEL_NUM; i++)
    {
    strText.Format(_T("%.3f"), dblValue[BASEID_T + i]);
    __SetExcelCellText(objWorkSheet, nRow, 4+i, strText);
    }
    for (i=0; i<K_CHANNEL_NUM; i++)
    {
    strText.Format(_T("%.3f"), dblValue[BASEID_K + i]);
    __SetExcelCellText(objWorkSheet, nRow, 50+i, strText);
    }
    for (i=0; i<A_CHANNEL_NUM; i++)
    {
    strText.Format(_T("%.3f"), dblValue[BASEID_A + i]);
    __SetExcelCellText(objWorkSheet, nRow, 58+i, strText);
    }
    strText.Format(_T("%d / %d"), nSequence, g_nHistoryCount);
    pDlg->m_staticProgress.SetWindowText(strText);
    pDlg->m_slidProgress.Value = nSequence * 100.0 / g_nHistoryCount; nLastSequence = nSequence;
    }

    nMilliTime += pDlg->m_nMilliSecond;
    nSequence = nMilliTime * (g_sHistoryConfig.dblHighSampleRate / PCI9114_CHANNELS / 1000.0);
    } // &Ccedil;&ETH;&raquo;&raquo;&raquo;&Oslash;&micro;&frac12;&Agrave;ú&Ecirc;·&ETH;&Aring;&Iuml;&cent;&Ograve;&sup3;
    objWorkSheet = objExcelSheets.GetItem(COleVariant((short)1));
    objWorkSheet.Activate(); pExcelApp->SetVisible(TRUE); // &Iuml;&Ocirc;&Ecirc;&frac34; Excel &Oacute;&brvbar;&Oacute;&Atilde;&sup3;&Igrave;&ETH;ò
    delete pExcelApp; CoUninitialize(); // &Ograve;ò&Icirc;&ordf;&para;&Ocirc;&Oacute;&Uacute;COM&sup2;&Ugrave;×÷±&Oslash;&ETH;&euml;&para;&Ocirc;&Atilde;&iquest;&cedil;&ouml;&Iuml;&szlig;&sup3;&Igrave;&Ograve;&yacute;&Oacute;&Atilde; COM &iquest;&acirc;

    pDlg->m_hThread = NULL;
    ::PostMessage(pDlg->m_hWnd, WM_CLOSE, 0, 0); return 0;
    }
      

  2.   

    void CExportXLSDlg::__ExportDataHead(_Worksheet &objWorkSheet)
    {
    CString strText;
    int i; __SetExcelCellText(objWorkSheet, 1, 3, (CString)_T("&Agrave;&auml;&para;&Euml;")); __SetExcelCellText(objWorkSheet, 1, 1, (CString)_T("&Ecirc;±&frac14;&auml;"), 1, 2); for (i=0; i<T_CHANNEL_NUM; i++)
    {
    strText.Format(_T("T%d"), i+1);
    __SetExcelCellText(objWorkSheet, 1, 4 + i, strText);
    } for (i=0; i<K_CHANNEL_NUM; i++)
    {
    strText.Format(_T("K%d"), i+1);
    __SetExcelCellText(objWorkSheet, 1, 50 + i, strText);
    } for (i=0; i<A_CHANNEL_NUM; i++)
    {
    strText.Format(_T("A%d"), i+1);
    __SetExcelCellText(objWorkSheet, 1, 58 + i, strText);
    }
    }
      

  3.   

    void CExportXLSDlg::__SetExcelCellText(_Worksheet &objWorkSheet, int nRow, int nCol, CString &strText, int nHeight, int nWidth, int nAlignment)
    {
    Range objRange;
    CString strCellFrom;
    CString strCellTo;
    COleVariant varOptional(DISP_E_PARAMNOTFOUND, VT_ERROR); int nColFrom, nRowFrom;
    int nColTo, nRowTo; nRowFrom = nRow;
    nRowTo = nRowFrom + nHeight - 1; nColFrom = nCol;
    nColTo = nColFrom + nWidth - 1;

    ASSERT(nRowFrom > 0 && nRowTo >= nRow);
    ASSERT(nColFrom > 0 && nColTo >= nCol);

    __GetCellName(nRowFrom, nColFrom, strCellFrom);
    __GetCellName(nRowTo, nColTo, strCellTo);

    objRange = objWorkSheet.GetRange(COleVariant(strCellFrom), COleVariant(strCellTo));
    if (nWidth>1 || nHeight>1)
    objRange.Merge(COleVariant((short)0));
    objRange.SetValue(COleVariant(strText));
    objRange.BorderAround(COleVariant((short)1), (long)2, (long)1, varOptional);
    objRange.SetHorizontalAlignment(COleVariant((short)nAlignment));
    objRange.SetVerticalAlignment(COleVariant((short)2));
    objRange.SetColumnWidth(COleVariant((short)10));
    }