用dbgrideh 导出 excel 文件后
原来的多表头变成了一行,怎么解决?
原来的多表头变成了一行,怎么解决?
解决方案 »
- Delphi 使用shockwaveflash控件的问题
- DBComboBox控件中的值的选择问题
- query控件问题 说找不到对应的字段
- 如何改造dbGrid,实现自动保存每列宽度的功能
- 关于开发Delphi的IDE插件的问题。
- 我的代码哪里错了?(关于adoquery查询)
- 使用Delphi和Sql Server存储图片,图象的大小不可大于32K
- 想结交一些Delphi大侠!
- 如何读主板唯一信息?9X/NT/2000下具体实现方法
- 在TreeView中如何实现用鼠标右键实现节点选中,执行同Click相同的操作,同时并可弹出快捷菜单?
- 一个困惑我一个晚上的问题,大家指导下啊
- DBGRIDEH能否保存的时候加上标题栏?比如时间等
ExcelApp: Variant;
dbgrideh2:Tdbgrideh;
query2:Tquery;
function tocell(i, j: integer): string; //把行列转换为EXCEL的单元格的格式
function power26(x: Integer): Integer; //26的x次方函数
function inttoz26(value: Integer): String; //整数转换为excel形式的26进制函数
function z26toint(value: string): Integer; //整数转换为excel形式的26进制的逆函数
function excel_open(filename,appCaption:string):integer;
function excel_merge(title_row,title_col,all_rowcount:integer):integer;
procedure outtoexcel(filename,appcaption:string;dbgrideh:Tdbgrideh;cs_jqdh:integer;cs_s1:string;cs_s2:string;cs_i1:integer;cs_i2:integer); { Public declarations }
end;var
Form1: TForm1;
implementation{$R *.DFM}
function TForm1.z26toint(value: string): Integer;
var
i: Integer;
begin
result := 0;
for i := 1 to length(value) do
begin
result := result + (ord(upcase(value[i])) - 64)*power26(length(value)-i);
end;
end;function TForm1.inttoz26(value: Integer): String;
var
left,d: Integer;
c: char;
begin
left := value;
result := '';
while left>0 do
begin
d := left mod 26;
left := left div 26;
if d = 0 then
begin
c := 'Z';
dec(left);
end
else
c := chr(d+64);
result := c + result;
end;
end;function TForm1.power26(x: Integer): Integer;
var
m: Integer;
begin
result := 1;
for m := 1 to x do
result := 26*result;
end;function TForm1.tocell(i, j: integer): string;
begin
result :=inttoz26(j)+inttostr(i);
end;function TForm1.excel_open(filename,appcaption:string):integer;
begin
try
result:=-1;
ExcelApp := CreateOleObject( 'Excel.Application' );
ExcelApp.Visible := True;
ExcelApp.Caption := appcaption;
ExcelApp.WorkBooks.Open( filename );
result:=1;
except
application.MessageBox('没有安装EXCEL!!','提示',64);
end;
end;function TForm1.excel_merge(title_row,title_col,all_rowcount:integer):integer;
var
str_title,str_all,str_data,excel_start,excel_end:string;
i,j,k,l,jl_col,jl_row : integer;
ls_row,ls_col,ls_rowcount:integer;
arr :array of array of integer;
arr_str:array of string;
begin
ls_row:=title_row;
ls_col:=title_col;
ls_rowcount:=all_rowcount;
setlength(arr,ls_row+1); //初始化
for i:=0 to ls_row do
begin
setlength(arr[i],ls_col+1);
end;
for i:=0 to length(arr)-1 do
begin
for j:=0 to length(arr[i])-1 do
arr[i][j]:=0;
end;
setlength(arr_str,0);
for i:=1 to ls_row do
begin
for j:=1 to ls_col do
begin
if trim(ExcelApp.Cells[i,j].Value)<>'' then
begin
excel_start:=tocell(i,j);
arr[i][j]:=1; //表示已经使用
jl_col:=j;
for l:=j+1 to ls_col do
begin
if (trim(ExcelApp.Cells[i,l].Value)='') and (arr[i][l]<>1) then
begin
jl_col:=l;
arr[i][l]:=1;
end else
break;
end;
jl_row:=i;
for l:=i+1 to ls_row do
begin
if (trim(ExcelApp.Cells[l,j].Value)='') and (arr[l][j]<>1) then
begin
jl_row:=l;
arr[l][j]:=1;
end else
begin
break;
end;
end;
excel_end:=tocell(jl_row,jl_col);
if excel_start<>excel_end then
begin
str_title:=excel_start+':'+excel_end;
setlength(arr_str,length(arr_str)+1) ;
arr_str[length(arr_str)-1]:=str_title;
end;
end;
end;
end; str_title:= 'A1:'+tocell(ls_row,ls_col);
for i:=0 to length(arr_str)-1 do
begin
ExcelApp.Range[arr_str[i]].Select;
ExcelApp.Selection.Merge;
end;
ExcelApp.ActiveSheet.range[str_title].Font.Bold := True;
ExcelApp.ActiveSheet.range[str_title].HorizontalAlignment:=3; //居中
ExcelApp.ActiveSheet.range[str_title].VerticalAlignment:=2; //居中
ExcelApp.ActiveSheet.range[str_title].Borders.LineStyle := 1; //加边框
ExcelApp.ActiveSheet.Range[str_title].Borders[1].Weight := 3;
ExcelApp.ActiveSheet.Range[str_title].Borders[2].Weight := 3;
ExcelApp.ActiveSheet.Range[str_title].Borders[3].Weight := 3;
ExcelApp.ActiveSheet.Range[str_title].Borders[4].Weight := 3;
ExcelApp.ActiveSheet.range[str_title].Interior.ColorIndex :=19 ; //鹅黄色
str_all:='A1:'+tocell(ls_rowcount+ls_row,ls_col);
str_data:=tocell(ls_row+1,1)+':'+tocell(ls_rowcount+ls_row,ls_col);
ExcelApp.ActiveSheet.range[str_data].Borders.LineStyle := 1;
ExcelApp.ActiveSheet.range[str_all].Columns.AutoFit; //ExcelApp.ActiveSheet.Range[ 'A1:D3' ].Borders[5].Weight := 3; //交叉
//ExcelApp.ActiveSheet.Range[ 'A1:D3' ].Borders[6].Weight := 3; //交叉
//ExcelApp.ActiveWorkBook.Saved := true; //放弃存盘
//ExcelApp.Cells[4,1].Value:='反复发';
if not ExcelApp.ActiveWorkBook.Saved then //保存
ExcelApp.ActiveWorkBook.Save;
ExcelApp:=Unassigned;
// ExcelApp.quit;
end;
procedure TForm1.outtoexcel(filename,appcaption:string;dbgrideh:Tdbgrideh;cs_jqdh:integer;cs_s1:string;cs_s2:string;cs_i1:integer;cs_i2:integer);
var
dbgridehexp :TDBGridEhExportAsXLS;
i : integer;
begin
dbgridehexp := TDBGridEhExportAsXLS.Create();
dbgridehexp.DBGridEh:=dbgrideh2;
dbgridehexp.ExportToFile(filename,TRUE); //直接导出文件
if excel_open(filename,appcaption)=-1 then //打开excel
exit;
excel_merge(dbgridehexp.MutiTitle_RowCount,dbgridehexp.MutiTitle_ColCount,dbgridehexp.DBGridEh.DataSource.DataSet.Recordcount);
//写数据格式
end;