用BCB将EXCEL导入数据库问题
#include <vcl.h>
#pragma hdrstop
#include "Unit3.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm3 *Form3;
//---------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm3::Button1Click(TObject *Sender)
{
AnsiString ExcelName;
Variant vExcelApp,vSheet,vRange;
TOpenDialog *pOD=new TOpenDialog(this);
pOD->Filter="MS Office Excel(*.xls)|*xls";
pOD->Title="Open Excel File";
pOD->DefaultExt="xls";
do
{
if(!pOD->Execute())
{
delete pOD ;
pOD=0;
return;
}
ExcelName=pOD->FileName;
}
while(!FileExists(ExcelName));
delete pOD ;
pOD=0;
try
{
vExcelApp = Variant::CreateObject("Excel.Application");
}
catch(...)
{
Application->MessageBox("运行Excel出错,请确认安装了Office","错误",MB_ICONSTOP|MB_OK);
vExcelApp=Unassigned;
return;
}
// 隐藏Excel界面
vExcelApp.OlePropertySet("Visible",false);
vExcelApp.OlePropertySet("DisplayAlerts", false); //不显示Excel提示框
// 打开一个工作表
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Open", ExcelName.c_str()); // 工作表
// 操作这个工作表
vSheet=vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets",1);
// Range对象
vRange = vExcelApp.OlePropertyGet("Selection");
int nColCount = vSheet.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("Count");
vSheet.OlePropertyGet("UsedRange").OleFunction("Find","CF18");
for(int i=0;i<nColCount-1;i++) // 遍历所有行
{
ListView1->Items->Add();
Form3->ListView1->Items->Item[i]->Caption="No"+IntToStr(i);
ListView1->Items->Item[i]->SubItems->Add(vSheet.OlePropertyGet("Cells",i+1,1).OlePropertyGet("Value"));
ListView1->Items->Item[i]->SubItems->Add(vSheet.OlePropertyGet("Cells",i+1,2).OlePropertyGet("Value"));
ListView1->Items->Item[i]->SubItems->Add(vSheet.OlePropertyGet("Cells",i+1,3).OlePropertyGet("Value"));
ListView1->Items->Item[i]->SubItems->Add(vSheet.OlePropertyGet("Cells",i+1,4).OlePropertyGet("Value"));
ListView1->Items->Item[i]->SubItems->Add("否");
}
// 保存Excel文档并退出
try
{
vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("SaveAs",ExcelName.c_str());
}
catch(...)
{
Application->MessageBox("请不要导入正在编辑的Execel文件","警告",MB_ICONWARNING|MB_OK);
Form3->ListView1->Items->Clear();
vExcelApp.OleFunction("Quit");
vSheet=Unassigned;
vExcelApp=Unassigned;
return;
}
vExcelApp.OleFunction("Quit");
vSheet=Unassigned;
vExcelApp=Unassigned;
Application->MessageBox("导入Excel文件完成!","信息提示",MB_ICONINFORMATION|MB_OK);
int k = ListView1->Items->Count;//总行数
String strTemp = "总的行数是:"+ IntToStr(k);
Edit1->Text = strTemp.c_str();
}
这是在网上找到代码,,做的实验,,为什么在导入LISTVIEW里,,显示的是一排排数字,,都是NO1,NO2这样的形式,,不是EXCEL里面的内容,,求教
[解决办法]
和excel无关,这段代码有问题,这样写也是NO1,NO2这样的形式
ListView1->Clear();
for(int i=0;i<10;i++) // 遍历所有行
{
ListView1->Items->Add();
ListView1->Items->Item[i]->Caption="No"+IntToStr(i);
ListView1->Items->Item[i]->SubItems->Add("a");
ListView1->Items->Item[i]->SubItems->Add("b");
ListView1->Items->Item[i]->SubItems->Add("c");
ListView1->Items->Item[i]->SubItems->Add("d");
ListView1->Items->Item[i]->SubItems->Add("否");
}