Export Data to Excel File

stevenjscn 2004-03-16 06:05:10
I want to export data to Excel File and I wrote codes below
void __fastcall TMainForm::ToolButton2Click(ToolButton2)
{
Variant ExApp,Wbs,Wb,Ws;

try
{
ExApp=CreateOleObject("Excel.App");
}
catch(...)
{
MessageDlg("Excel can not run normally",mtError,TMsgDlgButtons()<<mbOK,0);
}

* Wbs=ExApp.OlePropertyGet("WorkBooks");
Procedure Open("Open");
Wbs.Exec(Open<<sFileName);
ExApp.OlePropertySet("Visible",(Variant)true);
Wb=Wbs.OleFunction("Add");
Wb=ExApp.OlePropertyGet("ActiveWorkbook");
Ws=Wb.OlePropertyGet("ActiveSheet");
}
While the code runs to *, it shows me problems
"Project Export.exe raised exception class EAccessViolation with message
'Access Violation at address 004FD9B in module'Export.exe',Read of address 00000800'Process stopped. Use step or Run to continue"

I dont know whats matter with it, and pls help me to solve it. I am waiting on line.
...全文
332 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
stevenjscn 2004-03-19
  • 打赏
  • 举报
回复
Sorry, I can not type Chinese in my OS, only English. While i compile the program fully. I can not export the data to excel correctly. when the program runs to OlePropertySet() function, it shows the critical error "Project Export.exe raised exception class EAccessViolation with message'Access Violation at address 004FD9B in module'Export.exe',Read of address 00000800'Process stopped. Use step or Run to continue", Otherwise, i select the select "Build with runtime packages" in packages Page, it can run normally, but while packing, it has problems
zzhong2 2004-03-19
  • 打赏
  • 举报
回复
一:
用installshield develop 9或者installshield express v5.0打包
在http://www.51delphi.com/delphi/上,或www.ttdown.com可以下载,搜一下“installshield”就能找到。它有一步Additional Tools->Dependenciy Scanners(develop 9)其中头两项就是搜索你的exe还需要哪些库文件的,第一项是静态搜索:Perform Static Scanning第二项是动态搜索:Perform Dynamic Scanning,动态搜索比较全,可以先试静态搜索,不行再用动态搜索.

二:
要想实现全编译,在project(工程)->options(选项)里设置以下项目:

1、在linker页里:取消use dynamic RTL的选项。

2、在packages页里:取消build with runtime packages的选项。

3、以上两个步骤已经可以实现全编译了,但有一个选项是建议的:在compiler页里,按一下release按纽(上有绿色小人),它的意义在于编译时将程序的一些信息不要编译进程序,这样可能会减少程序的大小。

用installshield develop 9或者installshield express v5.0打包
在http://www.51delphi.com/delphi/上,或www.ttdown.com可以下载,搜一下“installshield”就能找到。它有一步Additional Tools->Dependenciy Scanners(develop 9)其中头两项就是搜索你的exe还需要哪些库文件的,第一项是静态搜索:Perform Static Scanning第二项是动态搜索:Perform Dynamic Scanning,动态搜索比较全,可以先试静态搜索,不行再用动态搜索.
TR@SOE 2004-03-19
  • 打赏
  • 举报
回复
I am always using Server object to write Office applications. My email is tr@netease.com. I have some samples and can share with you if you want.
JetKingLau 2004-03-18
  • 打赏
  • 举报
回复
只是不选 Build with runtime packages 项还不行,还需要不选 Linker 页的 Use dynamic RTL 项。
lovebcb 2004-03-18
  • 打赏
  • 举报
回复
出现什么错误?
-----------
楼主的英文不错,很标准啊~
stevenjscn 2004-03-18
  • 打赏
  • 举报
回复
I know what caused this problem, but i dont know how to solve. In project options, I didnt select "Build with runtime packages" in packages Page. But when i select it, i use installshield to pack it . it can not run normally in other client's computer. How can I do?, please, please, please.... WAIT ON LINE. Score will be sent once the problem was solved.
JetKingLau 2004-03-16
  • 打赏
  • 举报
回复
看看偶的例子
//---------------------------------------------------------------------------
bool GridToXLS(TStringGrid *grid, AnsiString file, AnsiString title, AnsiString inscribe)//将表格内容保存到Excel文档
/*
功能:
将TStringGrid内容保存到Excel文档
参数:
grid - TStringGrid 控件
file - 输出文件名
title - 标题
inscribe - 落款
返回值:
true - 成功
false - 不成功
*/
{
bool suc = false;
Variant ExcelApp;
try {
ExcelApp = Variant::CreateObject ("Excel.Application");
}
catch(...) {
return(false);
}
try {
ExcelApp.OlePropertySet("Visible",(Variant)false);//使Excel程序不可见
Variant WorkBook1;
WorkBook1 = ExcelApp.OlePropertyGet("WorkBooks").OleFunction("Add"); //默认工作簿
Variant Sheet1;
Sheet1 = WorkBook1.OlePropertyGet("ActiveSheet");
String strValue="abcdefg";
int row = 4;
Variant Range1;
Range1 = Sheet1.OlePropertyGet("Range", (AnsiString("A2:")+AnsiString::StringOfChar('A'+grid->ColCount-1,1)+"2").c_str()); //单元格合并
Range1.OleFunction("Merge",false);//合并单元格
Range1.OlePropertyGet("Cells").OlePropertySet("Value",title.c_str());//设置标题

for(int c=0;c<grid->ColCount;c++)
for(int r=0;r<grid->RowCount;r++) {//逐行设置表格数据
Sheet1.OlePropertyGet("Cells",r+row,c+1).OlePropertySet("Value",grid->Cells[c][r].c_str());
}
Range1 = Sheet1.OlePropertyGet("Range", (AnsiString("A")+IntToStr(grid->RowCount+5)+":"+AnsiString::StringOfChar('A'+grid->ColCount-1,1)+IntToStr(grid->RowCount+5)).c_str()); //单元格合并
Range1.OleFunction("Merge",false);//合并单元格
Range1.OlePropertyGet("Cells").OlePropertySet("Value",inscribe.c_str());//设置落款

Range1 = Sheet1.OlePropertyGet("Range", (AnsiString("A1:")+AnsiString::StringOfChar('A'+grid->ColCount-1,1)+IntToStr(grid->RowCount+6)).c_str()); //选择单元格
Range1.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Name","Times New Roman");
Range1.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Size",12);

if( !FileExists(file) || DeleteFile(file) ) {
WorkBook1.OleFunction("SaveAs",file.c_str());
suc = FileExists(file);
}
ExcelApp.OleFunction ("Quit");
ExcelApp =Unassigned;
return(suc);
}
catch(...) {
return(false);
}
}
//---------------------------------------------------------------------------
stevenjscn 2004-03-16
  • 打赏
  • 举报
回复
I create a new project and use the same code, it can run normally. I dont know why
stevenjscn 2004-03-16
  • 打赏
  • 举报
回复
I only get the workbooks from application
olony 2004-03-16
  • 打赏
  • 举报
回复
仅仅创建了application但是没有创建workbook对象
就是说你需要执行一下workbook.add

不知道是不是这么会儿事儿,我外国话特差
http://expert.csdn.net/Expert/topic/2805/2805151.xml?temp=.8808863
【前言】 工作或学习中可能需要实现基于VC读\写Excel文件的功能,本人最近也遇到了该问题。中间虽经波折,但是最终还是找到了解决问题的办法。 在此跟大家分享,希望对跟我同样迷茫过的同学们有所帮助。 1、程序功能 1)打开一个excel文件; 2)显示到CListCtrl上; 3)新建一个Excel文件。 以上均在对话框中实现。 2、平台 VC++2010 3、实现方法 常用的Excel打开方式有两种 1)通过数据库打开; 2)OLE方式打开。 由于方式1)操作繁琐,经常出现莫名的错误,这里选用方式2). 4、准备步骤 首先新建一个Dialog窗体程序,添加list control和两个按钮 1)将ExcelLib文件夹拷贝到程序目录下; 2)将Export2Excel.h,Export2Excel.cpp两个文件添加到项目; 3)包含头文件,#include "ExcelLib/Export2Excel.h" 通过以上步骤在程序中引入了可以读取Excle文件的CExport2Excel类; 5、打开excel文件 通过按钮点击打开 void CExcelTestDlg::OnBnClickedButtonOpenExcel() { //获取文件路径 CFileDialog* lpszOpenFile; CString szGetName; lpszOpenFile = new CFileDialog(TRUE,"","",OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,"Excel File(*.xlsx;*.xls)|*.xls;*.xlsx",NULL); if (lpszOpenFile->DoModal()==IDOK) { szGetName = lpszOpenFile->GetPathName(); SetWindowText(szGetName); delete lpszOpenFile; } else return; //打开文件 //文件中包含多个sheet时,默认打开第一个sheet CExport2Excel Excel_example; Excel_example.OpenExcel(szGetName); //获取sheet个数 int iSheetNum = Excel_example.GetSheetsNumber(); //获取已使用表格行列数 int iRows = Excel_example.GetRowCount(); int iCols = Excel_example.GetColCount(); //获取单元格的内容 CString cs_temp = Excel_example.GetText(1,1); //AfxMessageBox(cs_temp); //List control上显示 //获取工作表列名(第一行) CStringArray m_HeadName; m_HeadName.Add(_T("ID")); for (int i=1;iGetItemCount()>0) { m_list.DeleteColumn(0); } //初始化ClistCtrl,加入列名 InitList(m_list,m_HeadName); //填入内容 //第一行是标题,所以从第2行开始 CString num; int pos; for (int row = 2;row<=iRows; row++) { pos = m_list.GetItemCount(); num.Format(_T("%d"),pos +1); m_list.InsertItem(pos,num); for (int colum=1;columDoModal()==IDOK) { szGetName = lpszOpenFile->GetPathName(); SetWindowText(szGetName); delete lpszOpenFile; } else return; //文件全名称 CString csFileName = szGetName; //需要添加的两个sheet的名称 CString csSheetName = "newSheet"; CString csSheetName2 = "newSheet2"; // 新建一个excel文件,自己写入文字 CExport2Excel Excel_example; //新建excel文件 Excel_example.CreateExcel(csFileName); //添加sheet,新加的sheet在前,也就是序号为1 Excel_example.CreateSheet(csSheetName); Excel_example.CreateSheet(csSheetName2); //操作最开始添加的sheet:(newSheet) Excel_example.SetSheet(2); //添加表头 Excel_example.WriteHeader(1,"第一列"); Excel_example.WriteHeader(2,"第二列"); //添加核心数据 Excel_example.WriteData(1,1,"数据1"); Excel_example.WriteData(1,2,"数据2"); //保存文件 Excel_example.Save(); //关闭文件 Excel_example.Close(); } 7、注意事项 1)一般单个Excel文件包含多个sheet,程序默认打开第一个; 2)指定操作sheet,使用Excel_example.SetSheet(2)函数; 3)打开文件时最左侧的sheet序号为1,新建excel时最新添加的sheet序号为1. 【后记】 本程序主要基于网络CSDN中---“Excel封装库V2.0”---完成,下载地址是:http://download.csdn.net/detail/yeah2000/3576494,在此表示感谢!同时, 1)在其基础上作了小改动,改正了几个小错误,添加了几个小接口; 2)添加了如何使用的例子,原程序是没有的; 3)详细的注释 发现不足之处,还请大家多多指教!

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧