|
#ifndef Unit1H
#define Unit1H
#include <Classes.hpp>
#include <ComCtrls.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
// ------------------------------------------------ ---------------------------
class TForm1: public TForm
{
__published:
TButton * Button1;
TListView * ListView1;
void __fastcall Button1Click (TObject * Sender);
public:
__fastcall TForm1 (TComponent * Owner);
};
// ------------------------------------------------ ---------------------------
extern PACKAGE TForm1 * Form1;
// ------------------------------------------------ ---------------------------
#endif
////////////////////////////////////////////////// //////////////////////////////////
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <Excel_2k.h>
#pragma package (smart_init)
#pragma resource "* .dfm"
TForm1 * Form1;
// ------------------------------------------------ ---------------------------
__fastcall TForm1 :: TForm1 (TComponent * Owner): TForm (Owner)
{
ListView1-> ViewStyle = vsReport;
}
// ------------------------------------------------ ---------------------------
void __fastcall TForm1 :: Button1Click (TObject * Sender)
{
ListView1-> Clear ();
ListView1-> Columns-> Clear ();
WideString theXls;
theXls = ExtractFilePath (Application-> ExeName) + "yourExcel.xls";
if (! FileExists (theXls)) {ShowMessage ("There is no file in yourExcel.xls in the current directory"); return;}
TCOM_Application iExcelApp;
try {iExcelApp = CoExcelApplication :: Create ();}
catch (...) {ShowMessage ("Error starting Excel, probably not installed Excel"); return;}
// iExcelApp-> set_Visible (0, true);
Workbooks * ibooksPtr;
iExcelApp-> get_Workbooks (&ibooksPtr);
ibooksPtr-> Open (theXls.c_bstr (),
TNoParam (), TNoParam (), TNoParam (), TNoParam (),
TNoParam (), TNoParam (), TNoParam (), TNoParam (),
TNoParam (), TNoParam (), TNoParam (), TNoParam (),
0);
_WorksheetPtr isheet;
isheet = iExcelApp-> get_ActiveWorkbook ()-> get_ActiveSheet ();
long RowCount = isheet-> get_UsedRange (1)-> Rows-> get_Count ();
long ColCount = isheet-> get_UsedRange (1)-> Columns-> get_Count ();
Range * iRangePtr;
Variant Cell1, Cell2, v;
AnsiString s;
for (int k = 0; k <ColCount; k ++)
{
TListColumn * NewColumn;
NewColumn = ListView1-> Columns-> Add ();
NewColumn-> Caption = "第" + IntToStr (k) + "Column";
}
for (int m = 0; m <RowCount; m ++)
{
TListItem * item;
item = ListView1-> Items-> Add ();
for (int n = 0; n <ColCount; n ++)
{
Cell1 = isheet-> Cells-> get_Item (TVariant (m + 1), TVariant (n + 1));
Cell2 = isheet-> Cells-> get_Item (TVariant (m + 1), TVariant (n + 1));
isheet-> get_Range (Cell1, Cell2,&iRangePtr);
iRangePtr-> get_Item (TVariant (m + 1), TVariant (n + 1));
iRangePtr-> Activate ();
v = iRangePtr-> get_Value2 ();
s = v.operator AnsiString ();
if (n == 0) item-> Caption = s;
else item-> SubItems-> Add (s);
}
}
iRangePtr-> Release ();
isheet-> Release ();
ibooksPtr-> Release ();
iExcelApp-> Quit ();
}
// ------------------------------------------------ --------------------------- |
|