如何从.txt文件中读出数据然后写入到xls文件中大约是这样的,一个文件夹下面有多个.txt文件,每个文件的格式为
432432
431455
7654
8765875
8758
有很多行数字现读出这些数字然后写到xls文件中的指定的一列中(一个.txt文件对应一个.xls文件)
432432
431455
7654
8765875
8758
有很多行数字现读出这些数字然后写到xls文件中的指定的一列中(一个.txt文件对应一个.xls文件)
解决方案 »
- 用VS2005写的win32串口通讯动态库,交给客户的时候要不要给 MSVCR80D.DLL 这库?
- 编程问题(数据输出),请大家指点一下。
- 实现视频播放时,声音控制条的透明问题(不惜重分),由请高手!急!
- 谁能提供对C/C++源程序按一定格式自动排版的工具?
- 简单问题请教:关于newPen的创建
- 图形的旋转问题
- win32做的dll库
- 我在一个对话框里 按确定 进入下一个对话框 出问题
- 我在程序中需要一个循环,但我又想在外部通过单击按钮或窗体来终止循环,请问如何实现?谢谢!!!
- 我在api中想建立一个对话框为基础的应用程序
- 如何修改一个现有画笔对象的宽度????
- 寻求24X24及32X32的图标-用来自制Toolbar
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=193263
eg:
1122,2222,333
55,22,33333
....
另存為 "a.csv"
打開a.csv看,這樣可以 嗎?
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();我是这样实现的,可能这里写的不全,
但是好像还是可以的
现在想请各位教我如何写xls文件,写操作的格式是这样的:
第一列 第二列 第三列 第四列 ........
43232 543254 54325 344444 ........
43232 543254 54325 344444 ........
43232 543254 54325 344444 ........
43232 543254 54325 344444 ........
当然上面的数字是不一样的,现在要做的操作就是在原有的基础上再加列数,我现在怎么写数据到指定的第五列,第六列.....
5423
5432
5542
5466
7654
.
.
.现在要把这一列读出然后写到一个xls文件的指定一列上去
读出已经有了
现在只要写到一个xls文件的指定一列上去的方法请大家关注......
不知能否让我分享一下你的成果呢!!!
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:
你的邮件我已收到,谢谢你我现在是想写数据到一个我指定的文件里面怎么弄呢?
你好,我建了一个单文档的程序(视类是基于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;
}
比如2k使用的tlb文件和2003使用的olb文件。而有些函数的调用参数也不尽相同。比如2k使用的多个参数和2003使用的较少的参数,所以要根据不同的版本来测试。将相应的(比如2003 的XL5CHS32.OLB)通过ClassWizard的automation导入之后,添加所有的类,然后看看office目录下关于VBA的帮助,自己就可以猜出来怎么使用。
olb文件导出也能看到对应的enum定义
上面的jeapvan(多多)兄弟给我发了一个例子,他只加了excel9.h,excel9.cpp然后就可以用了我和他一样的另建一个工程时编译能通过,运行时,会出错误!我觉得我和他做的一样的,可是就是不知那里了了问题!!!请jeapvan(多多)兄出来帮我解答
能不能发给我呢,,[email protected]