📄 ucxlsviewer.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UCXlsViewer.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "UExcelAdapter"
#pragma link "UFlexCelGrid"
#pragma link "UFlexCelImport"
#pragma link "XLSAdapter"
#pragma resource "*.dfm"
TMain *Main;
//---------------------------------------------------------------------------
__fastcall TMain::TMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionReadOnlyExecute(TObject *Sender)
{
ActionReadOnly->Checked=!ActionReadOnly->Checked;
Data->ReadOnly=ActionReadOnly->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TMain::LoadFile()
{
DisableTabs=true;
__try
{
Caption="XlsViewer";
AnsiString Ext=UpperCase(ExtractFileExt(OpenDialog->FileName));
if (Ext==".CSV") FlexCelImport->OpenText(OpenDialog->FileName, ListSeparator); else //Note that ListSeparator mightbe other than "," (for example ";") so CSV might not be "comma" separated. This is the way excel handles it.
if (Ext==".TXT") FlexCelImport->OpenText(OpenDialog->FileName, char(9)); else
FlexCelImport->OpenFile(OpenDialog->FileName);
TabControl->Tabs->Clear();
for (int p=1;p<=FlexCelImport->SheetCount;p++)
{
FlexCelImport->ActiveSheet=p;
TabControl->Tabs->Add(FlexCelImport->ActiveSheetName);
};
FlexCelImport->ActiveSheet=1;
TabControl->TabIndex=FlexCelImport->ActiveSheet-1;
TrackBarZoom->Position=FlexCelImport->SheetZoom;
Data->LoadSheet();
Caption=Caption+": "+OpenDialog->FileName;
}
__finally
{
DisableTabs=false;
}
}
void __fastcall TMain::ActionOpenExecute(TObject *Sender)
{
if (!OpenDialog->Execute()) exit;
LoadFile();
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionSaveAsExecute(TObject *Sender)
{
if (!XlsSaveDialog->Execute()) return;
if (FileExists(XlsSaveDialog->FileName)) DeleteFile(XlsSaveDialog->FileName);
Data->ApplySheet();
AnsiString Ext=UpperCase(ExtractFileExt(XlsSaveDialog->FileName));
if (Ext==".CSV") FlexCelImport->SaveAsText(XlsSaveDialog->FileName, ListSeparator); else //Note that ListSeparator mightbe other than "," (for example ";") so CSV might not be "comma" separated. This is the way excel handles it.
if (Ext==".TXT") FlexCelImport->SaveAsText(XlsSaveDialog->FileName, char(9)); else
FlexCelImport->Save(XlsSaveDialog->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionCloseExecute(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionCopyFormatExecute(TObject *Sender)
{
ClipFormat=FlexCelImport->CellFormat[Data->Row][Data->Col];
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionPasteFormatExecute(TObject *Sender)
{
FlexCelImport->CellFormat[Data->Row][Data->Col]=ClipFormat;
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionCellFormatExecute(TObject *Sender)
{
// not implemented on C++ builder. Not very instructive either, I think
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionRowFormatExecute(TObject *Sender)
{
// not implemented on C++ builder. Not very instructive either, I think
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionColFormatExecute(TObject *Sender)
{
// not implemented on C++ builder. Not very instructive either, I think
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionZoomExecute(TObject *Sender)
{
if (TrackBarZoom->Position<25) TrackBarZoom->Position=25; else
if (TrackBarZoom->Position<50) TrackBarZoom->Position=50; else
if (TrackBarZoom->Position<100) TrackBarZoom->Position=100; else
if (TrackBarZoom->Position<200) TrackBarZoom->Position=200; else
if (TrackBarZoom->Position<400) TrackBarZoom->Position=400; else
TrackBarZoom->Position=10;
}
//---------------------------------------------------------------------------
void __fastcall TMain::Action10Execute(TObject *Sender)
{
TrackBarZoom->Position=10;
}
//---------------------------------------------------------------------------
void __fastcall TMain::Action25Execute(TObject *Sender)
{
TrackBarZoom->Position=25;
}
//---------------------------------------------------------------------------
void __fastcall TMain::Action50Execute(TObject *Sender)
{
TrackBarZoom->Position=50;
}
//---------------------------------------------------------------------------
void __fastcall TMain::Action100Execute(TObject *Sender)
{
TrackBarZoom->Position=100;
}
//---------------------------------------------------------------------------
void __fastcall TMain::Action200Execute(TObject *Sender)
{
TrackBarZoom->Position=200;
}
//---------------------------------------------------------------------------
void __fastcall TMain::Action400Execute(TObject *Sender)
{
TrackBarZoom->Position=400;
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionFullWorksheetExecute(TObject *Sender)
{
ActionFullWorksheet->Checked=!ActionFullWorksheet->Checked;
Data->FullWorksheet= ActionFullWorksheet->Checked;
Data->LoadSheet();
}
//---------------------------------------------------------------------------
void __fastcall TMain::TabControlChange(TObject *Sender)
{
if (DisableTabs) return;
Data->ApplySheet();
FlexCelImport->ActiveSheet= TabControl->TabIndex+1;
TrackBarZoom->Position=FlexCelImport->SheetZoom;
Data->LoadSheet();
}
//---------------------------------------------------------------------------
void __fastcall TMain::TrackBarZoomChange(TObject *Sender)
{
if (FlexCelImport->IsLoaded())
{
Data->Zoom=TrackBarZoom->Position;
FlexCelImport->SheetZoom=TrackBarZoom->Position;
}
PanelZoom->Caption=Format("%d%%",ARRAYOFCONST((TrackBarZoom->Position)));
}
//---------------------------------------------------------------------------
void __fastcall TMain::DataSelectCell(TObject *Sender, int ACol, int ARow,
bool &CanSelect)
{
LoadingEdCell=true; //So EdCellChange doesn't fire...
__try
{
if (FlexCelImport->IsFormula[ARow][ACol])
EdCell->Text=FlexCelImport->CellFormula[ARow][ACol]; else
EdCell->Text=FlexCelImport->CellValue[ARow][ACol];
}
__finally
{
LoadingEdCell=false;
}
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionCopyExecute(TObject *Sender)
{
Data->CopyToClipboard();
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionPasteExecute(TObject *Sender)
{
Data->PasteFromClipboard();
}
//---------------------------------------------------------------------------
void __fastcall TMain::ActionPrintExecute(TObject *Sender)
{
int FirstPage=1;
int LastPage=-1;
if (!FlxPreview) FlxPreview=new TFPreview(this);
TXlsCellRange PrintRange;
PrintRange.Left=1;
PrintRange.Top=1;
PrintRange.Right=Data->MaxVisibleCol;
PrintRange.Bottom=Data->MaxVisibleRow;
FlxPreview->FlexCelPreview1->FlexCelGrid=Data;
if (!PrintDialog) PrintDialog= new TPrintDialog(this);
PrintDialog->MinPage=1;
PrintDialog->FromPage=1;
int PageCount;
Data->CalcNumberOfPrintingPages(PageCount);
PrintDialog->Options<<poPageNums<<poSelection;
PrintDialog->MaxPage=PageCount;
PrintDialog->ToPage=PageCount;
if (PrintDialog->Execute())
{
if (PrintDialog->PrintRange==prPageNums)
{
FirstPage=PrintDialog->FromPage;
LastPage=PrintDialog->ToPage;
}
if (PrintDialog->PrintRange==prSelection)
{
PrintRange.Left=Data->Selection.Left;
PrintRange.Top=Data->Selection.Top;
PrintRange.Right=Data->Selection.Right;
PrintRange.Bottom=Data->Selection.Bottom;
}
Printer()->Title=ExtractFileName(OpenDialog->FileName);
FlxPreview->FlexCelPreview1->Init(PrintRange, FirstPage, LastPage);
bool ReallyPrint;
__try
{
ReallyPrint=FlxPreview->ShowModal()==mrOk;
}
__finally
{
FlxPreview->FlexCelPreview1->Done(); //Not really necessary, just to free resources
}
if (ReallyPrint) Data->Print(PrintRange, FirstPage, LastPage);
}
}
//---------------------------------------------------------------------------
void __fastcall TMain::DataSetEditText(TObject *Sender, int ACol, int ARow,
const WideString Value)
{
LoadingEdCell=true; //So EdCellChange doesn't fire...
__try
{
EdCell->Text=Value;
}
__finally
{
LoadingEdCell=false;
}
}
//---------------------------------------------------------------------------
void __fastcall TMain::EdCellChange(TObject *Sender)
{
if (!LoadingEdCell /*&& Data->CanEdit*/)
Data->SetCell(Data->Row, Data->Col, EdCell->Text);
}
//---------------------------------------------------------------------------
void __fastcall TMain::EdCellKeyPress(TObject *Sender, char &Key)
{
if (Key==char(13))
{
Data->SetFocus();
Key=char(0);
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -