用vc转换excell文件为文本文件 如何用vc将excell文件(.csv,.xls)转换为文本文件(.txt),该文本文件可以自由设定分隔符(空格,逗号或者“|”等)。不用打开文件,给出选择框,直接转换,转换后的文件名为filename.txt。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我写了一个,加了些注释,把关键代码给你贴出来了:#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(); } #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",表示第二行第三个单元格 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);} 谢谢各位的热情支持!我只是想直接转换。我的大概思路是有一个文件浏览选择框,可以选择文件。选中文件后直接按“转换”按钮就进行转换,不需要打开文件的。因为本人没有弄过vc,所以只好向各位求救了,如能将实现的project打包给我,感激不尽!email: [email protected], TCP/IP连接失败,10060错误。本来一直是好的。 关于vc,java,MySQL开发 关于管道的异步通信模式 用AfxBeginThread(fun1,(LPVOID &)pram,0,NULL)启动一个线程时的参数传递问题。 CHtmlView的问题 MFC 如何调用文本编辑器打开文件????? In-proc 线程模型 多次切换窗口为什么会导致机器速度越来越慢? 有高手吗,能否回答关于EDIT控件赋值的问题? 小问题两个~~~~~~~~~~~~~ 请问为什么输出的会是? 问一个并口的问题(急)小妹谢谢!!!!
#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();
}
{
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",表示第二行第三个单元格
{
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);
}
我只是想直接转换。我的大概思路是有一个文件浏览选择框,可以选择文件。选中文件后直接按“转换”按钮就进行转换,不需要打开文件的。因为本人没有弄过vc,所以只好向各位求救了,如能将实现的project打包给我,感激不尽!
email: [email protected],