C++ builder 2010 操作Excel表格的编程实现

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop #include "ProcessBar.h"
#include <stdio.h> // For FILE, fopen, fstat, fileno, fread and fclose
#include <sys\stat.h> // For fstat and the stat struct
#include <Math.hpp> // For Min
#include <memory> //For STL auto_ptr class //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm4 *Form4;
//---------------------------------------------------------------------------
__fastcall TForm4::TForm4(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm4::btn1Click(TObject *Sender)
{
FILE *F;
char MyData[];
long BytesRead; F = fopen("../oui.txt", "r"); // Path relative to Debug
// Use a file larger than 2048 bytes to make it interesting.
if (F)
{
struct stat statbuf;
fstat(fileno(F), &statbuf);
pb1->Max = statbuf.st_size;
if (pb1->Max > )
{
pb1->Step = (pb1->Max)/;
pb1->Step = Min(pb1->Step, );
}
else
pb1->Step = pb1->Max;
std::auto_ptr<char> DataBuffer(new char[pb1->Step]);
for (pb1->Position = ;
pb1->Position < pb1->Max;
pb1->StepIt()) // Move the ProgressBar Position using StepIt.
{
fread(DataBuffer.get(), pb1->Step, , F);
// Do this or else the read wraps and starts over.
pb1->Step =
Min(pb1->Step, pb1->Max - pb1->Position);
} mmo1->Lines->Add(DataBuffer.get()); fclose(F);
DataBuffer.release();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm4::btn2Click(TObject *Sender)
{ Variant vExcelApp, vWorkbook, vRange, Sheet1;
vExcelApp = Variant::CreateObject("Excel.Application");
vExcelApp.OlePropertySet("Visible", false);
vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open",
dlgOpen1->FileName.c_str());
vWorkbook = vExcelApp.OlePropertyGet("ActiveWorkbook"); int BookCount = vWorkbook.OlePropertyGet("Sheets").OlePropertyGet
("Count");
for (int i = ; i < BookCount; i++)
{
AnsiString SheetName = vWorkbook.OlePropertyGet("Sheets", i + )
.OlePropertyGet("Name");
// ShowMessage(SheetName); } // Sheet1 = vWorkbook.OlePropertyGet( "ActiveSheet ");
// vExcelApp.Exec(PropertyGet( "Cells ") < <1 < <3).Exec(PropertySet( "Value ") < <15); //赋值
// Sheet1.OlePropertyGet( "Rows ", 1).OlePropertyGet( "Insert "); //插入
String kkl, hgf;
int it = , st = ; while (it > && BookCount >= st)
{
vWorkbook.OlePropertyGet("Sheets", st).OleProcedure("Select");
// 选择sheet
for (it = ; it <= ; it++)
{
kkl = vExcelApp.Exec(PropertyGet("Cells") << << it);
//if (kkl.Pos(qz1) > 0 || kkl.Pos(qz2) > 0) // 读取
break;
}
st++;
} int kk = ;
for (kk = ; kk <= ; kk++)
{
kkl = vExcelApp.Exec(PropertyGet("Cells") << kk << it);
//if (kkl.Pos(qz1) > 0 || kkl.Pos(qz2) > 0) // 读取
break;
} hgf = vExcelApp.Exec(PropertyGet("Cells") << kk << it + );
int ki = ;
while (hgf.Trim() != "")
{
if (ki == StringGrid2->RowCount - )
//StringGrid1->RowCount++;
// kkl==vExcelApp.Exec(PropertyGet( "Cells ") < <kk < <it); StringGrid2->Cells[][ki] = vExcelApp.Exec
(PropertyGet("Cells") << kk << it); // 2-4列的数据
StringGrid2->Cells[][ki] = vExcelApp.Exec
(PropertyGet("Cells") << kk << it + );
StringGrid2->Cells[][ki] = vExcelApp.Exec
(PropertyGet("Cells") << kk << it + );
kk++;
ki++;
hgf = vExcelApp.Exec(PropertyGet("Cells") << kk << it + );
} vExcelApp.OlePropertyGet("ActiveSheet").OlePropertyGet("Cells ", , )
.OleProcedure("Select");
vRange = vExcelApp.OlePropertyGet("Selection");
vRange.Exec(Function("Sort") << vExcelApp.OlePropertyGet("Selection")
<< ); // vWorkbook.OleProcedure( "Save");
vWorkbook.OleProcedure("Close");
vExcelApp.OleFunction("Quit");
vWorkbook = Unassigned;
vExcelApp = Unassigned;
}
// ---------------------------------------------------------------------------
上一篇:Android 中的AIDL,Parcelable和远程服务


下一篇:springmvc 关于controller的字符编码