delphi6 如何访问EXCEL,如何控制EXCEL里的每行每列的数据?
解决方案 »
- 高手请进:纯winsocket api多线程文件传输设计思路?
- 上个月得分888,放分庆祝一下
- 初级问题
- 简单问题啊..老大门来看(登陆窗体问题)
- 我想实现在程序中,打开dos窗口,自动输入命令,如"dir"等,请问该怎样实现呢?
- 请问这两行有什么错误。在线等待!谢谢!!!
- 我正在使用HTML help workshop制作帮助文件,请帮忙。
- 很菜的问题:关于var。查HELP只给很少的信息所以只好来问
- 提醒一下,顺便送分。
- 谁能跟我讲解一下两线程如何设置临界区以保证线程互斥访问同一个公共变量??
- 给第三方控件添加属性后,在Object Inspector不显示是什么原因?
- 求delphi7简介,急
可以用控件完成,如advstringgrid等表格控件。
也可以自己写:
如
void __fastcall TfrmMain::mnuOpenClick(TObject *Sender)
{
//open
//load cell file
//Excel
OpenExcel->DefaultExt = "xls";
OpenExcel->FileName = "*.xls";
OpenExcel->Filter = "Excel files (*.xls)|*.xls";
OpenExcel->FilterIndex = 1; //
if (OpenExcel->Execute() == true)
{
sInfo = "Try to open Excel file... " + DateToStr(Date()) + TimeToStr(Time());
Memo1->Lines->Add(sInfo);
Variant ExcelApp;
try
{
ExcelApp = Variant::CreateObject("Excel.Application");
}
catch (...)
{
sInfo = "Create Excel object failed!";
Memo1->Lines->Add(sInfo);
return ;
}
//if (!LoadExcelData(ExcelApp, OpenExcel->Files->Strings[0]))
if (!LoadExcelData(ExcelApp, OpenExcel->FileName))
{
sInfo = "Open Excel file unknown error!";
Memo1->Lines->Add(sInfo);
ExcelApp.OleFunction("Quit");
}
ExcelApp.Clear();
}
}
bool __fastcall TfrmMain::LoadExcelData(Variant ExcelAppp, AnsiString sFileName)
{
Variant ExcelWorkbooks, ActiveWorkBook, WorkSheet, varCell, ExcWindows;
AnsiString str;
//打开指定文件
try
{
ExcelWorkbooks = ExcelAppp.OlePropertyGet("Workbooks");
ActiveWorkBook = ExcelWorkbooks.OleFunction("Open", sFileName.c_str());
}
catch (...)
{
sInfo = AnsiString("Open \"") + sFileName + AnsiString("\" failed!");
Memo1->Lines->Add(sInfo);
return false;
}
//取得第一个sheet
WorkSheet = ActiveWorkBook.OlePropertyGet("WorkSheets", 1);
//
int colCount, rowCount;
colCount = WorkSheet.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("Count");
rowCount = WorkSheet.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("Count");
sInfo = "FileName:" + sFileName;
Memo1->Lines->Add(sInfo);
sInfo = "Col:" + IntToStr(colCount);
Memo1->Lines->Add(sInfo);
sInfo = "Row:" + IntToStr(rowCount);
Memo1->Lines->Add(sInfo);
//whaway:--------------------------------------------------------------
bool bCheck;
bCheck = true;
if (7 != colCount || rowCount < 11)
{
sInfo = "Number of Column error,or file is empty!";
bCheck = false;
}
if (!bCheck)
{
Memo1->Lines->Add(sInfo);
return false;
}
//whaway:--------------------------------------------------------------
//whaway:--------------------------------------------------------------
String iFields[8];
int irow = 11;
ProgressBar1->Max = rowCount - 1;
AdvColumnGrid1->RowCount = m_Count + 2;
//whaway:--------------------------------------------------------------
try
{
for (irow = 11; irow <= rowCount; irow++)
{
varCell = WorkSheet.OlePropertyGet("Cells", irow, 2);
iFields[2] = varCell.OlePropertyGet("Value");
// Lon lat
varCell = WorkSheet.OlePropertyGet("Cells", irow, 3);
iFields[3] = varCell.OlePropertyGet("Value");
varCell = WorkSheet.OlePropertyGet("Cells", irow, 4);
iFields[4] = varCell.OlePropertyGet("Value");
//Bearing HBWD PN
varCell = WorkSheet.OlePropertyGet("Cells", irow, 5);
iFields[5] = varCell.OlePropertyGet("Value");
varCell = WorkSheet.OlePropertyGet("Cells", irow, 6);
iFields[6] = varCell.OlePropertyGet("Value");
varCell = WorkSheet.OlePropertyGet("Cells", irow, 7);
iFields[7] = varCell.OlePropertyGet("Value");