如何从.txt文件中读出数据然后写入到xls文件中大约是这样的,一个文件夹下面有多个.txt文件,每个文件的格式为
432432
431455
7654
8765875
8758
有很多行数字现读出这些数字然后写到xls文件中的指定的一列中(一个.txt文件对应一个.xls文件)

解决方案 »

  1.   

    文本文件一次读一行了,再写入xls了,不过没有试过,呵呵
      

  2.   

    利用EXCEL自动化写入.xls文件
      

  3.   

    怎么没有人哦,好像到过年还有好久吧!!!写xls文件时,怎么样能精确到那一列那一行呢?怎么样读取一个txt文件的一行?如有写好的示例代码请发到[email protected],有重分
      

  4.   

    参考
    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=193263
      

  5.   

    用逗號分隔開每一行中的數字,將這個文件命名為sth.csv(csv 格式的文件).用逗號分開的每一段就寫到excel中的一列,每一行對應excel中的一行.
    eg:
    1122,2222,333
    55,22,33333
    ....
    另存為    "a.csv"
    打開a.csv看,這樣可以 嗎?
      

  6.   

    读文体文件不说了,写XLS使用ODBC操作excel文件写进去就可以了
      

  7.   

    同意 yinzhaohui(努力) .读txt文件就用CStdioFile一次读一行吧
      

  8.   

    用自动化。 www.vckbase.com上面有篇文章的。
      

  9.   

    有个取巧的办法:txt文件里面排好行和列,每个字段用tab分隔,这样的txt直接改后缀名为xls,可以用excel直接打开的,另存一下就是excel格式了。
      

  10.   

    CFileDialog txtFileDlg(....);
    CFile m_txtfile;strFileName=txtFileDlg.GetPathName();
    OpenTxtFile(strFileName);//Custom
    int i=m_txtfile.GetLength();
    m_txtfile.SeekToBegin();
    int len = m_txtfile.Read(strData.GetBuffer(12408),12408);
    strData.ReleaseBuffer();
    nPos=0;
    int nLength=0;
    nLength=strData.GetLength();
    strData=strData.Left(nLength);
    for(i=0;;i++)
    {
    nLength=strData.GetLength();
    nPos=strData.Find('\n',0);
    str=strData.Left(nPos-1);
    strData=strData.Right(nLength-nPos-1);
    SaveDataToXls(str+'\n');//Custom
    if(nPos<0)
    break;
    }
    m_txtfile.Close();我是这样实现的,可能这里写的不全,
    但是好像还是可以的
      

  11.   

    读txt文件我知道了
    现在想请各位教我如何写xls文件,写操作的格式是这样的:
    第一列   第二列   第三列   第四列   ........
    43232    543254   54325    344444   ........
    43232    543254   54325    344444   ........
    43232    543254   54325    344444   ........
    43232    543254   54325    344444   ........
    当然上面的数字是不一样的,现在要做的操作就是在原有的基础上再加列数,我现在怎么写数据到指定的第五列,第六列.....
      

  12.   

    有结果的请发到[email protected]
      

  13.   

    jazy() 的方法最简单啦,我常用的
      

  14.   

    是这样的,我的一个txt文件只有一列,也就是下面这样的
    5423
    5432
    5542
    5466
    7654
    .
    .
    .现在要把这一列读出然后写到一个xls文件的指定一列上去
    读出已经有了
    现在只要写到一个xls文件的指定一列上去的方法请大家关注......
      

  15.   

    我说个很好的解决方案,使用SQL Server带的DTS来做转换,很方便,不过还有更方便的,那就是使用我写的一个转换工具,更可实现定时/条件转换,等等功能。
      

  16.   

    ximenying(西门)
    不知能否让我分享一下你的成果呢!!!
      

  17.   

    用ODBC 也行啊,装Exel 后就有ODBC 驱动的
      

  18.   

    MSDN中的代码,Automate Excel to Add Data from a DAO Recordset to a Workbook,稍作修改即可。
    MFC example
    Start a new dialog-based MFC AppWizard EXE project named "ExcelData." 
    Using ClassWizard, add the wrapper classes for the Excel type library. 
    Add a button to the dialog resource IDD_EXCELDATA_DIALOG and add the following code to the button's handler in ExcelDataDlg.cpp: 
    //For optional arguments
        COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);    CDaoDatabase db;
        CDaoRecordset rs;
        long lNumCols;    //Get a recordset that represents all the records in the Products 
        //table of the sample Northwind database
        db.Open("C:\\Program Files\\Microsoft Office\\Office" \
                "\\Samples\\Northwind.mdb", FALSE, FALSE);
        rs.m_pDatabase = &db;    
        rs.Open(AFX_DAO_USE_DEFAULT_TYPE, "Select * From Products", 0);
        lNumCols = rs.GetFieldCount();    //Start a new workbook in Excel
        _Application oApp;
        oApp.CreateDispatch("Excel.Application");
        if (!oApp)
        {
            AfxMessageBox("Cannot start Excel");
            return;
        }
        Workbooks oBooks = oApp.GetWorkbooks();
        _Workbook oBook = oBooks.Add(vOpt);
        Worksheets oSheets = oBook.GetWorksheets();
        _Worksheet oSheet = oSheets.GetItem(COleVariant((short)1));
        Range oRange;    //Transfer the data in the recordset to the worksheet
        COleDispatchDriver rs2;
        rs2.AttachDispatch((LPDISPATCH) rs.m_pDAORecordset);
        oRange = oSheet.GetRange(COleVariant("A2"), vOpt);
        oRange.CopyFromRecordset((LPUNKNOWN) rs2.m_lpDispatch,
                                 vOpt, vOpt);
        rs2.DetachDispatch();
        rs2.ReleaseDispatch();    //Add the field names to row 1
        CDaoFieldInfo FieldInfo;
        for(long i=0; i<=lNumCols-1;i++)
        {
            oRange = oSheet.GetRange(COleVariant("A1"), vOpt);
            oRange = oRange.GetOffset(vOpt, COleVariant(i));
            rs.GetFieldInfo(i, FieldInfo, AFX_DAO_PRIMARY_INFO);
            oRange.SetValue(COleVariant(FieldInfo.m_strName));
        }    //Format the worksheet
        oRange = oSheet.GetRange(COleVariant("A1"), vOpt);
        oRange = oRange.GetResize(COleVariant((short)1), 
                                  COleVariant(lNumCols));
        Font oFont = oRange.GetFont();
        oFont.SetBold(COleVariant((short)TRUE));
        oRange = oRange.GetEntireColumn();
        oRange.AutoFit();    //Make Excel visible and give the user control
        oApp.SetVisible(TRUE);
        oApp.SetUserControl(TRUE);
        
    Add the following includes to ExcelDataDlg.cpp: 
    #include "Excel8.h" //or "Excel9.h" for Excel 2000
    #include <afxdao.h>Modify CExcelDataApp::InitInstance() in ExcelData.cpp to start COM services: 
        if(!AfxOleInit())
        {
            AfxMessageBox("Cannot initialize COM services.");
            return FALSE;
        }Build and run the application. Click the button you added to the dialog box. When the Automation code finishes running, you see the contents of the Products table in a new worksheet in Microsoft Excel. 
    Additional notes
    If you are using Microsoft Excel 2000, you can use either a DAO or ADO recordset with the CopyFromRecordset method. The Excel 97 CopyFromRecordset method supports only DAO recordsets.For additional information on using an ADO recordset with the CopyFromRecordset method, please see the following Microsoft Knowledge Base article:
      

  19.   

    jeapvan(多多):
    你的邮件我已收到,谢谢你我现在是想写数据到一个我指定的文件里面怎么弄呢?
      

  20.   

    多多:
    你好,我建了一个单文档的程序(视类是基于CFormView),然后和你一样的加一个菜单,在这个菜单响应函数里加上你的以下代码,为什么运行时会出错呢?
    请帮我,要是白天发给我的话,请发到我给你的邮箱,晚上在这里角答就可,下面是你的代码:
    void CACDTranView::OnViewExcel() 
    {
    COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,
    VT_ERROR),vtTrue((short)TRUE),vtFalse((short)FALSE);
    _Application *ExcelApp = new _Application; ExcelApp->CreateDispatch("excel.application"); //设置为显示
    ExcelApp->SetVisible(TRUE);
    //得到WorkBooks
    .......
    CString str;
    for(int i=3;i<=5;i++)
    {
    str.Format("A%d",i);
    myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
    .......
    }
    delete ExcelApp;
    }
      

  21.   

    既然用office automation,偶问你个问题先。你用的excel啥子版本的?
      

  22.   

    我有程序,邮箱是[email protected]
      

  23.   

    monstersky() :XP weiyongzhao(好人):能发给我吗?
      

  24.   

    对于不同的excel,调用方式也不同。所使用的库文件也不同。
    比如2k使用的tlb文件和2003使用的olb文件。而有些函数的调用参数也不尽相同。比如2k使用的多个参数和2003使用的较少的参数,所以要根据不同的版本来测试。将相应的(比如2003 的XL5CHS32.OLB)通过ClassWizard的automation导入之后,添加所有的类,然后看看office目录下关于VBA的帮助,自己就可以猜出来怎么使用。
    olb文件导出也能看到对应的enum定义
      

  25.   

    monstersky:
    上面的jeapvan(多多)兄弟给我发了一个例子,他只加了excel9.h,excel9.cpp然后就可以用了我和他一样的另建一个工程时编译能通过,运行时,会出错误!我觉得我和他做的一样的,可是就是不知那里了了问题!!!请jeapvan(多多)兄出来帮我解答
      

  26.   

    weiyongzhao(好人),你好:
    能不能发给我呢,,[email protected]
      

  27.   

    在APP的類中的InitInstance()函數中加入語句AfxOleInit();就可以了
      

  28.   

    jeapvan(多多):我想打开指定的xls文件,然后写入数据,我该怎么弄呢?
      

  29.   

    还没有解决吗?看这里:http://www.vckbase.com/document/viewdoc/?id=693有源码,里面写了个类CSpreadSheet可以访问、操作.xls和.csv文件。我看了一下,源码很容易看明白的^_^