乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > delphi 怎样把DBgridEH数据导入到已建好的exc...

delphi 怎样把DBgridEH数据导入到已建好的exc...

作者:乔山办公网日期:

返回目录:excel表格制作


您好,很高兴为您解答。

采用DBGridEh控件,TDBGridEhImport.ImportFromFile

如若满意,请点zd右侧【采纳答案】,如若还有问题,请点击【追问】

希望我的回答对您有所帮助,望采纳!

~ O(∩_∩)O~

GridEh可以导出到Excel,但是不能导入到模板,只能直接操作e69da5e6ba907a686964616f334Excel了,可以循环数据集,然后把数据集的数据插入到Excel里,下面是我原来程序里操作Excel的一段代码:
function TFrmRptStaff.OutData(AFileName: string): Boolean;
var
ExcelApp: Variant;
DataCount, i: Integer;
function GetRepRange(x, y: Integer): string;
var
fX, fY: string;
begin
if y <= 0 then
fX := 'A';
if y <= 26 then
fX := chr(64 + y);
if y > 26 then
fX := chr(64 + (y div 26)) + chr(64 + (y mod 26));

fY := IntToStr(x);
Result := fX + fY;
end;

begin
try
Result := False;
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.Visible := False;
ExcelApp.WorkBooks.Add;
ExcelApp.WorkBooks.Open(AFileName);
ExcelApp.WorkSheets[1].Activate;
ExcelApp.WorkSheets[1].Name := '人员报表';
// 插入报表日期
ExcelApp.ActiveSheet.Cells[1, 2].Value := FormatDateTime('YYYY年MM月DD日',
dtpDate.Date);
adsMain.DisableControls;
adsMain.First;
DataCount := adsMain.RecordCount;
for i := 0 to DataCount - 1 do
begin
// 插入记录
ExcelApp.ActiveSheet.Rows[5 + i].Insert;
ExcelApp.ActiveSheet.Cells[5 + i, 1].Value := SQLGetValueStr(adsMain,
'DepartmentName');
ExcelApp.ActiveSheet.Cells[5 + i, 2].Value := SQLGetValueStr(adsMain,
'StaffName');
ExcelApp.ActiveSheet.Cells[5 + i, 3].Value := SQLGetValueStr(adsMain,
'CarDay');
ExcelApp.ActiveSheet.Cells[5 + i, 4].Value := SQLGetValueStr(adsMain,
'CarMonth');
ExcelApp.ActiveSheet.Cells[5 + i, 5].Value := SQLGetValueStr(adsMain,
'CarYear');
ExcelApp.ActiveSheet.Cells[5 + i, 6].Value := SQLGetValueStr(adsMain,
'PropertyDay');
ExcelApp.ActiveSheet.Cells[5 + i, 7].Value := SQLGetValueStr(adsMain,
'PropertyMonth');
ExcelApp.ActiveSheet.Cells[5 + i, 8].Value := SQLGetValueStr(adsMain,
'PropertyYear');
ExcelApp.ActiveSheet.Cells[5 + i, 9].Value := SQLGetValueStr(adsMain,
'AccidentDay');
ExcelApp.ActiveSheet.Cells[5 + i, 10].Value := SQLGetValueStr(adsMain,
'AccidentMonth');
ExcelApp.ActiveSheet.Cells[5 + i, 11].Value := SQLGetValueStr(adsMain,
'AccidentYear');
ExcelApp.ActiveSheet.Cells[5 + i, 12].Value := SQLGetValueStr(adsMain,
'TotalDay');
ExcelApp.ActiveSheet.Cells[5 + i, 13].Value := SQLGetValueStr(adsMain,
'TotalMonth');
ExcelApp.ActiveSheet.Cells[5 + i, 14].Value := SQLGetValueStr(adsMain,
'TotalYear');
ExcelApp.ActiveSheet.Cells[5 + i, 15].Value := SQLGetValueStr(adsMain,
'WorkProgress');
ExcelApp.ActiveSheet.Cells[5 + i, 16].Value := SQLGetValueStr(adsMain,
'PropertyYearPaid');
adsMain.Next;
FrmMain.RzProgressStatus1.Percent := i * 100 div adsMain.RecordCount;
end;
adsMain.First;
adsMain.EnableControls;
ExcelApp.ActiveSheet.Rows[4].Delete;
FrmMain.RzProgressStatus1.Percent := 0;
// 合并单元格
ExcelApp.Range[GetRepRange(4 + DataCount, 1) + ':' + GetRepRange
(4 + DataCount, 2)].Select;
ExcelApp.Selection.Merge;
ExcelApp.Selection.HorizontalAlignment := -4108; // xlHAlignCenter
// 插入合计值
ExcelApp.ActiveSheet.Cells[4 + DataCount, 3].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[3]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 4].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[4]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 5].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[5]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 6].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[6]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 7].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[7]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 8].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[8]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 9].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[9]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 10].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[10]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 11].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[11]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 12].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[12]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 13].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[13]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 14].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[14]);
ExcelApp.ActiveSheet.Cells[4 + DataCount, 16].Value :=
gridMain.GetFooterValue(0, gridMain.Columns[17]);
// 保存并退出
ExcelApp.ActiveWorkBook.Save;
ExcelApp.WorkBooks.Close;
ExcelApp.Quit;
ExcelApp := unassigned;
Result := True;
except
on e: Exception do
begin
AddError(ClassName + '-' + '导出数据' + '-' + e.Message);
end;
end;
end;
关于Txt文件转化成电子表格:法copy3种,复制、导入和直接打开:1.正常情况下经过整理的数据不会进入到一个单元格里,若干进入一个单元格,一定是双击后产生这种现象,正确的方法是单击,确定复制的开始位置;2.Txt原来的回车后的部分进入下一行,回车与回车之间的部分进入一个或若干个单元格内,可能需要进一步加工整理;3.原稿的数据行越强,越简化下一步加工难度,操作方式因其具体情况不同而不同,甚至还要借助其他软件,不可一概而论。

DbGridEh 控件是需要与数据源相联的,这种情况下,建议你可以用个AdoConnection 连接 excel文件,再用个 query 组件取得数据,将 DbGridEh 控件数据源与 query 的 Source 组件连接。

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网