之前一個 bcb5開發的程式 移植到 bcb6中 需添加 匯出excel 功能。 現在的問題是 用ole 方法 程式報錯
access violation at address 005AE277 .之前一個專案 用bcb6 開發的 同樣代碼 就沒有報錯。 剛開始學bcb 完善找不到方向 。求各位大神幫幫忙
uj5u.com熱心網友回復:
求原始碼.(..........)uj5u.com熱心網友回復:
使用ole需要電腦中必須先安裝了office,不然就出錯在妖哥網站有幾篇相關文章,你可以參考一下
http://www.ccrun.com/page.asp?c=5&s=13
uj5u.com熱心網友回復:
完整代碼。#define PG OlePropertyGet
#define PS OlePropertySet
#define OP OleProcedure
#define OF OleFunction
Variant Ex, Wb, Sheet;
AnsiString FN = "";
AnsiString row = "";
try
{
FN = GetCurrentDir()+"\\Excel\\CuStyle.xls";
if(!FileExists(FN))
{
Application->MessageBox(L"報表模板檔案不存在,無法打開!",L"錯誤",MB_ICONSTOP|MB_OK);
return;
}
try
{
Ex = Variant::CreateObject("Excel.Application");
}
catch(...)
{
Application->MessageBox(L"無法啟動Excel",L"錯誤",MB_ICONSTOP|MB_OK);
return;
}
Ex.PS("Visible",false);
Ex.PG("WorkBooks").OP("Open",FN.c_str());
Wb = Ex.PG("ActiveWorkBook");
AnsiString shname = "Style";
Sheet = Wb.PG("sheets", shname.c_str());
// Sheet.PG("Range","A2:I3000").OP("Clear");
int iRows, LI;
LI = OrdHis->RecordCount;
OrdHis->First();
for(iRows=2;iRows<LI+2;iRows++)
{
Sheet.PG("Cells",iRows,1).PS("Value",OrdHis->Fields->Fields[0]->AsAnsiString.c_str());
Sheet.PG("Cells",iRows,2).PS("Value",OrdHis->Fields->Fields[1]->AsAnsiString.c_str());
Sheet.PG("Cells",iRows,3).PS("Value",OrdHis->Fields->Fields[2]->AsFloat);
Sheet.PG("Cells",iRows,4).PS("Value",OrdHis->Fields->Fields[3]->AsFloat);
Sheet.PG("Cells",iRows,5).PS("Value",OrdHis->Fields->Fields[4]->AsFloat);
Sheet.PG("Cells",iRows,6).PS("Value",OrdHis->Fields->Fields[5]->AsFloat);
Sheet.PG("Cells",iRows,7).PS("Value",OrdHis->Fields->Fields[6]->AsAnsiString.c_str());
Sheet.PG("Cells",iRows,8).PS("Value",OrdHis->Fields->Fields[7]->AsAnsiString.c_str());
OrdHis->Next();
}
row = AnsiString().sprintf("%d:5000", iRows);
Sheet.PG("Rows", WideString(row)).OP("delete");
Wb.OP("Save");
Wb.OP("Close");
Ex.OF("Quit");
Ex = Unassigned;
Wb = Unassigned;
Sheet = Unassigned;
}
catch(...)
{
Application->MessageBox(L"操作Excel表格失敗!",L"錯誤",MB_ICONSTOP|MB_OK);
Wb.OP("Save");
Wb.OP("Close");
Ex.OF("Quit");
Ex = Unassigned;
Wb = Unassigned;
Sheet = Unassigned;
}
ShowMessage("資料成功匯出...");
uj5u.com熱心網友回復:
客戶端程式主機先要安裝office。uj5u.com熱心網友回復:
//---------------------------------------------------------------------------
//功能: 匯出EXCEL ,保存路徑 ,是否預覽
//---------------------------------------------------------------------------
void __fastcall TfrmMain::DBGToExcel(TADOQuery * ADOQuery, TDBGrid * DBGrid, String XlsFile, bool PreView)
{
ADOQuery->First();
if (ADOQuery->RecordCount == 0)
{
MessageBox(NULL, "無匯出內容,操作取消!", "提示", MB_OK | MB_ICONINFORMATION);
return;
}
TFileStream *fs=new TFileStream(XlsFile,fmCreate);
String lf = "\r\n"; // 換行符
String tab = char(9); // 換列符
String v;
//匯出欄位名
for (int i = 0; i < DBGrid->Columns->Count; i++)
{
if (DBGrid->Columns->Items[i]->Visible == true)
{
v = DBGrid->Columns->Items[i]->Title->Caption;// FieldName;
v = v + tab; // 下一列
fs->Write((void *)v.c_str(),v.Length());
}
}
//匯出欄位內容
fs->Write((void *)lf.c_str(),lf.Length()); //下一行
for (int j = 0;j < ADOQuery->RecordCount; j++)
{
for (int i = 0;i < DBGrid->Columns->Count; i++)
{
if (DBGrid->Columns->Items[i]->Visible == true)
{
v = ADOQuery->Fields->Fields[i]->AsString.Trim();
v = v + tab; // 下一列
fs->Write((void *)v.c_str(),v.Length());
}
}
fs->Write((void *)lf.c_str(),lf.Length()); //下一行
ADOQuery->Next();
}
delete fs;
if (PreView)
ShellExecute(NULL,"open",XlsFile.c_str(),NULL,NULL,SW_SHOW);
else
MessageBox(NULL, "匯出完畢!", "提示", MB_OK | MB_ICONINFORMATION);
}
uj5u.com熱心網友回復:
用第三方插件 DevExpress ,里面的CGrid控制元件帶匯出成excel功能uj5u.com熱心網友回復:
是的,grideh也帶匯出功能uj5u.com熱心網友回復:
ehlibCGrid
都自帶的匯出功能
uj5u.com熱心網友回復:
Sheet.h:
#ifndef SheetH
#define SheetH
class CWorkSheet
{
public:
CWorkSheet();
~CWorkSheet();
Variant *pSheet;
Variant __fastcall GetCells(int i, int j);
void __fastcall SetCells(int i, int j, int Value);
void __fastcall SetCells(int i, int j, WideString Value);
void __fastcall SetCells(int i, int j, WideString Value, int Width);
int iRows;
int iColumns;
WideString __fastcall GetName();
void __fastcall SetName(WideString Name);
__property WideString Name = {read = GetName, write = SetName};
};
// ---------------------------------------------------------------------------
#endif
Sheet.cpp
#include <vcl.h>
#pragma hdrstop
#include <utilcls.h>
#include "Sheet.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
CWorkSheet::CWorkSheet()
{
}
CWorkSheet::~CWorkSheet()
{
}
Variant __fastcall CWorkSheet::GetCells(int i, int j)
{
return pSheet->OlePropertyGet("Cells", i, j).OlePropertyGet("value");
}
void __fastcall CWorkSheet::SetCells(int i, int j, int Value)
{
pSheet->OlePropertyGet("Cells", i, j).OlePropertySet("value", Value);
}
void __fastcall CWorkSheet::SetCells(int i, int j, WideString Value)
{
pSheet->OlePropertyGet("Cells", i, j).OlePropertySet("value", Value);
}
// ---------------------------------------------------------------------
void __fastcall CWorkSheet::SetCells(int i, int j, WideString Value, int Width)
{
pSheet->OlePropertyGet("Cells", i, j).OlePropertySet("value", Value);
pSheet->OlePropertyGet("Columns",j).OlePropertySet("ColumnWidth", Width);
}
// -----------------------------------------------------------------------
WideString __fastcall CWorkSheet::GetName()
{
return VarToWideStr(pSheet->OlePropertyGet("name"));
}
void __fastcall CWorkSheet::SetName(WideString Name)
{
pSheet->OlePropertySet("name", Name);
}
Excel.h
// ---------------------------------------------------------------------------
#ifndef ExcelH
#define ExcelH
// ---------------------------------------------------------------------------
#include "Sheet.h"
class CExcel
{
private:
bool __fastcall OpenExcel();
bool __fastcall NewExcel();
int iCurrSheet;
WideString _FileName;
Variant vEx;
Variant vWb;
Variant vSh;
CWorkSheet *pWorkSheet;
public:
CExcel();
CExcel(WideString FileName);
~CExcel();
bool __fastcall SaveExcel();
bool __fastcall SaveAsExcel(WideString Name);
void __fastcall CloseExcel();
int iSheetCount;
CWorkSheet* __fastcall GetSheet(int i);
};
#endif
excel.cpp
// ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Excel.h"
#include <utilcls.h>
#include <ComObj.hpp>
// ---------------------------------------------------------------------------
#pragma package(smart_init)
CExcel::CExcel()
{
iCurrSheet = 0;
pWorkSheet = new CWorkSheet();
NewExcel();
}
CExcel::CExcel(WideString FileName)
{
_FileName = FileName;
pWorkSheet = new CWorkSheet();
OpenExcel();
}
CExcel::~CExcel()
{
CloseExcel();
}
bool __fastcall CExcel::OpenExcel()
{
try
{
CoInitialize(NULL);
vEx = CreateOleObject("Excel.Application");
vEx.OlePropertySet("Visible", false);
vEx.OlePropertySet("DisplayAlerts", false);
vEx.OlePropertyGet("WorkBooks").OleProcedure("Open",_FileName.c_bstr());
vWb = vEx.OlePropertyGet("ActiveWorkBook");
vSh = vWb.OlePropertyGet("ActiveSheet");
pWorkSheet->pSheet = &vSh;
iSheetCount = vWb.OlePropertyGet("sheets").OlePropertyGet("count");
return true;
}
catch (...)
{
return false;
}
}
bool __fastcall CExcel::NewExcel()
{
try
{
CoInitialize(NULL);
vEx = Variant::CreateObject("Excel.Application");
vEx.OlePropertySet("Visible", false);
vEx.OlePropertyGet("WorkBooks").OleFunction("Add");
vWb = vEx.OlePropertyGet("ActiveWorkBook");
vSh = vWb.OlePropertyGet("ActiveSheet");
pWorkSheet->pSheet = &vSh;
iSheetCount = vWb.OlePropertyGet("sheets").OlePropertyGet("count");
return true;
}catch (...)
{
return false;
}
}
// ---------------------------------------------------------------------
bool __fastcall CExcel::SaveExcel()
{
try
{
vWb.OleFunction("save");
return true;
}
catch (...)
{
return false;
}
}
bool __fastcall CExcel::SaveAsExcel(WideString Name)
{
AnsiString sFileName;
sFileName = Name;
if (FileExists(Name))
DeleteFile(sFileName);
try
{
vWb.OleFunction("SaveAs", Name.c_bstr());
_FileName = Name;
return true;
}catch (...)
{
return false;
}
}
void __fastcall CExcel::CloseExcel()
{
if( NULL != pWorkSheet )
{
delete pWorkSheet;
pWorkSheet = NULL ;
}
vWb.OleFunction("close");
vEx.OleFunction("quit");
vWb = Unassigned;
vEx = Unassigned;
CoUninitialize();
}
// ---------------------------------------------------------------------
CWorkSheet *__fastcall CExcel::GetSheet(int i)
{
if (i == iCurrSheet)
{
return pWorkSheet;
}
else if (i < 1 || i > iSheetCount)
{
return NULL;
}
else
{
iCurrSheet = i;
}
vSh = vWb.OlePropertyGet("Sheets", i);
pWorkSheet->pSheet = &vSh;
pWorkSheet->iRows = vSh.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("count");
pWorkSheet->iColumns = vSh.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("count");
return pWorkSheet;
}
沒注釋,請將就著看吧
uj5u.com熱心網友回復:
我去。。。挖墳貼轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/81029.html
標籤:VCL組件使用和開發
