如何用vc将excell文件(.csv,.xls)转换为文本文件(.txt),该文本文件可以自由设定分隔符(空格,逗号或者“|”等)。
不用打开文件,给出选择框,直接转换,转换后的文件名为filename.txt。

解决方案 »

  1.   

    我写了一个,加了些注释,把关键代码给你贴出来了:
    #include "excel9.h"void CExcelDlg::OnButton1() 
    {
    CoInitialize(NULL); //初始化COM对象
    CStdioFile file; //用来写入文本文件
    if(!file.Open("filename.txt",CFile::modeCreate|CFile::modeWrite|CFile::typeText)){
    return;
    }

    COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);
    //启动EXCEL
        _Application oApp;
        oApp.CreateDispatch("Excel.Application",NULL);
        if (!oApp)
        {
            AfxMessageBox("不能启动EXCEL");
            return;
        }
        //向工作簿中添加新工作表
    Workbooks oBooks = oApp.GetWorkbooks();
        _Workbook oBook = oBooks.Add(COleVariant("C:\\My Documents\\c++\\shi\\excel\\xxx.xls"));
        Worksheets oSheets = oBook.GetWorksheets();
        //获取第一张工作表
    _Worksheet oSheet = oSheets.GetItem(COleVariant((short)1));
    for(int ii=0;ii<4;ii++)//其中4表示有4行数
    {
    CString line;
    for(int i=1;i<=4;i++)//其中4表示有4列
    {
    char tmp='A'+i;
    CString str1;
    str1.Format("%c%d",tmp,i);//格式化后的格式比如:"C2",表示第二行第三个单元格
    Range r;
    r=oSheet.GetRange(COleVariant(str1),vOpt);//选中该单元格
    COleVariant temp=r.GetValue();
    line+=temp.bstrVal;
    line+=" || ";//每个单元格数据中间加一个这个分格开
    }
    line+="\r\n";//每输入一行加一个回车
    file.Write(line,line.GetLength());
    }
    file.Close();
    CoUninitialize();
        
    }
      

  2.   

    #include "excel9.h"void CExcelDlg::OnButton1() 
    {
    CoInitialize(NULL); //初始化COM对象
    CStdioFile file; //用来写入文本文件
    if(!file.Open("filename.txt",CFile::modeCreate|CFile::modeWrite|CFile::typeText)){
    return;
    }

    COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);
    //启动EXCEL
        _Application oApp;
        oApp.CreateDispatch("Excel.Application",NULL);
        if (!oApp)
        {
            AfxMessageBox("不能启动EXCEL");
            return;
        }
        //向工作簿中添加新工作表
    Workbooks oBooks = oApp.GetWorkbooks();
        _Workbook oBook = oBooks.Add(COleVariant("C:\\My Documents\\c++\\shi\\excel\\xxx.xls"));
            //获取第一张工作表
    _Worksheet oSheet = oSheets.GetItem(COleVariant((short)1));
    for(int ii=0;ii<4;ii++)//其中4表示有4行数
    {
    CString line;
    for(int i=1;i<=4;i++)//其中4表示有4列
    {
    char tmp='A'+i;
    CString str1;
    str1.Format("%c%d",tmp,i);//格式化后的格式比如:"C2",表示第二行第三个单元格
      

  3.   

    void ConvertXlsToTxt(LPCTSTR xlsFile, LPCTSTR txtFile, LPCTSTR spliter)
    {
    CString sScriptFile("c:\\tmp.vbs");
    CString sScript;
    sScript.Format(
    "convert \"%s\", \"%s\", \"%s\"\n"
    "sub convert(xlsFile, txtFile, spliter)\n"
    " dim x,fso,stream,s\n"
    " set fso = createobject(\"scripting.filesystemobject\")\n"
    " fso.deletefile \"%s\", true\n"
    " set x = createobject(\"excel.application\")\n"
    " x.DisplayAlerts = False\n"
    " With x.Workbooks.Add(xlsfile)\n"
    "     .ActiveSheet.SaveAs txtFile, &HFFFFEFC2\n"
    "     .Close\n"
    " End With\n"
    " x.Quit\n"
    " set stream = fso.OpenTextFile(txtFile, 1)\n"
    " s = replace(stream.ReadAll, vbtab, spliter)\n"
    " set stream = fso.OpenTextFile(txtFile, 2)\n"
    " stream.write s\n"
    "end sub",
    xlsFile, txtFile, spliter, sScriptFile);
    CStdioFile file;
    file.Open(sScriptFile, CStdioFile::modeCreate|CStdioFile::modeWrite);
    file.WriteString(sScript);
    file.Close();
    ShellExecute(NULL, "open", sScriptFile, NULL, "c:\\", SW_HIDE);
    }
      

  4.   

    谢谢各位的热情支持!
    我只是想直接转换。我的大概思路是有一个文件浏览选择框,可以选择文件。选中文件后直接按“转换”按钮就进行转换,不需要打开文件的。因为本人没有弄过vc,所以只好向各位求救了,如能将实现的project打包给我,感激不尽!
    email: [email protected]