我准备用Excel做一个报表,涉及到合并Excel单元格的问题,怎么样通过输入左上和右下的单元格的编号把他们中间的格子合并为一个格子???
解决方案 »
- 如何在登陆时显示上一次登陆的用户名?
- TClientSocket和TServerSocket的问题:《比猛料更猛的技巧集》中的例子运行时出错??救命!
- 如何把指针类型转化为字符串
- ★★★★★★★★急!急!有点难度的问题:如何获得DBGridEh网格中正在编辑的字段值★★★★★★★★
- 请各位帮忙,我是新手,关于类的建立和引用。
- 用什么方法可以判断一个文件是否存在?
- [统计]大家做软件(例如进销存、物资管理)时录入单、打印时,都用什么控件啊?
- 请问多个线程如何同时访问数据库
- formatdatetime('yyyy-mm-dd','2002-06-02'),为什么报错?
- delphi版很活跃的几位兄弟为什么不支持开辟borland技术专区?怪怪怪
- 图片保存问题
- 100分求救!!!!!!!QuickReport数据不显示
var ExcelApp: Variant;
begin
ExcelApp := CreateOleObject( 'Excel.Application' );
数据填写完成之后
ExcelApp.ActiveSheet.Range['A1','C5'].Merge(true);
上面这句话的功能就是将A1到C5的区域合并起来end;
EXC:=CreateOleObject('Excel.Application');
Exc.Visible:=False;
Exc.Caption := '成绩查询系统调用 Microsoft Excel';
Exc.workBooks.add; //添加工作簿
exc.WorkSheets[1].Activate;
aSheet:=EXC.workSheets.item[1];
except
ShowMessage('启动EXCEL失败'+#13+#10+'可能是未安装Microsoft EXCEL!');
end;
exc2:=exc.activesheet.Range['A1','D1']; //指定合并范围
Exc2.MERGE; //合并
EXC2.value:='学生成绩表'; //合并单元格的内容
Begin
CapStr:=trim(exApp.Cells[Row,1].value);
Col1:=2;
Col2:=FldCount;
For Col1:=2 to Col2 Do
begin
NewCapStr:=trim(exApp.Cells[Row,Col1].value);
if (NewCapStr=CapStr) then
Begin
Cell1:=exApp.Cells.Item[Row,Col1-1];
Cell2:=exApp.Cells.Item[Row,Col1];
exApp.Cells[Row,Col1].value:='';
exApp.Range[Cell1,Cell2].Merge(True);
end
else
begin
CapStr:=NewCapStr;
end;
end;
end;
var
num1,num2: Integer;
ls_ColCaption: String;
begin
//取excel的列标签,用于建立range
num1 := ColNum div 26;
num2 := ColNum mod 26;
if(num1 > 0) then
ls_ColCaption := String(char(64 +num1))+String(char(64 +num2))
else
ls_ColCaption := String(char(64 +num2)); Result := ls_ColCaption;
end;//---------------------------------------------------------------------------
procedure T_App.ExportToExcel(CurDataGrid: TDBGridEh; ls_Title, ls_Select: String; Total: Boolean);
var
ExcelApp, WorkBook,Range: Variant;
i, j: Integer;
ls_Str: String;
li_RowNo: Integer; nRowCount,nColCount: Integer;
nColWidth : Integer;
ls_FileName,ls_TitleName,ls_FieldName,ls_Value: String;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := CreateOleObject('Excel.Sheet');
except
//"运行Excel出错,请确认安装了Office"
SetMDsuiStop(LoadStr(10009),LoadStr(10005),1);
Exit;
end; try
SaveDialog1.Filter := 'Excel files (*.xls)|*.xls';
SaveDialog1.DefaultExt := '.xls';
if (SaveDialog1.Execute) then
begin
ls_FileName := SaveDialog1.FileName;
if (ExtractFileExt(ls_FileName) = '') then
ls_FileName := ls_FileName + SaveDialog1.DefaultExt;
if (UpperCase(ExtractFileExt(ls_FileName)) <> '.XLS') then
begin
//"导出文件选择错误!"
SetMDsuiStop(LoadStr(10010),LoadStr(10005),1);
suiMessageDialog1.ShowModal;
Exit;
end;
end
else
begin
Exit;
end;
if (FileExists(ls_FileName)) then
begin
//"文件已存在,是否覆盖!","提示"
SetMDsuiInformation(LoadStr(10011),LoadStr(10006),2);
if (suiMessageDialog1.ShowModal <> mrOk) then
begin
Exit;
end;
end;//if fileexists(sfilename) then Application.ProcessMessages;
ExcelApp.Visible := True;
WorkBook := ExcelApp.WorkBooks.Add;
ExcelApp.WorkSheets[1].Activate; // 表格的行数
nRowCount := CurDataGrid.DataSource.DataSet.RecordCount + 1;
if nRowCount < 2 then
nRowCount := 2;
// 表格的列数
nColCount := CurDataGrid.Columns.Count;
if nColCount < 1 then
nColCount := 1; //写报表标题
if(ls_Title <> '') then //A1
begin
ExcelApp.Rows[1].RowHeight := 25;
ExcelApp.Cells(1, 1) := ls_Title;
ls_Str := 'A1:'+Excel_ColTag(nColCount)+'1';
ExcelApp.Range[ls_Str].Merge(true);
ExcelApp.Range[ls_Str].HorizontalAlignment := $FFFFEFF4;
ExcelApp.Range[ls_Str].VerticalAlignment := $FFFFEFF4;
ExcelApp.Rows[1].Font.Bold := true;
ExcelApp.Rows[1].Font.Size := 20; nRowCount := nRowCount + 1;
li_RowNo := li_RowNo + 1;
end;
//加一个查询条件 A2
if (ls_Select <> '') then
begin
for i :=0 to nColCount do
begin
//字段名是可视的
if (CurDataGrid.Columns.Items[i].Visible) then
begin
ExcelApp.Cells(2, i+1) := ls_Select;
ls_Str := 'A2:'+Excel_ColTag(nColCount)+'2';
ExcelApp.Range[ls_Str].Merge(true);
Break;
end
end; nRowCount := nRowCount + 1;
li_RowNo := li_RowNo + 1;
end; // 设置单元格的宽度
for i := 0 to nColCount - 1 do
begin
//字段名是可视的
if (CurDataGrid.Columns.Items[i].Visible) then
begin
nColWidth := CurDataGrid.Columns.Items[i].Width;
//vExcelApp.OlePropertyGet('Columns', i + 1).OlePropertySet('ColumnWidth', (nColWidth div 7)+2);
ExcelApp.Columns[i+1].ColumnWidth := (nColWidth div 7)+2;
end
else
begin
ExcelApp.Columns[i+1].ColumnWidth := 0;
end
end; // 先将列名写入Excel表格
for j := 0 to CurDataGrid.Columns.Count - 1 do
begin
if CurDataGrid.Columns.Items[j].Visible then
begin
//设置标题行的字体
ExcelApp.Rows[li_RowNo+1].Font.Bold := true;
//设置字体居中
ExcelApp.Rows[li_RowNo+1].HorizontalAlignment := $FFFFEFF4; ls_TitleName := CurDataGrid.Columns.Items[j].Title.Caption;
ExcelApp.Cells(li_RowNo+1, j+1) := ls_TitleName;
end;
end; // 将DBGrid中的数据写入Excel表格
CurDataGrid.DataSource.DataSet.First;
for i := li_RowNo to nRowCount - 2 do
begin
// 普通数据行的行高16
//vExcelApp.OlePropertyGet('Rows', i + 2).OlePropertySet('RowHeight', 16);
// 63 63 72 75 6E 2E 63 6F 6D
for j := 0 to CurDataGrid.Columns.Count - 1 do
begin
//字段名是可视的
if (CurDataGrid.Columns.Items[j].Visible) then
begin
ls_FieldName := CurDataGrid.Columns.Items[j].FieldName; if (CurDataGrid.DataSource.DataSet.FieldByName(ls_FieldName).IsNull) then
begin
Continue;
end; ls_Value := CurDataGrid.DataSource.DataSet.FieldByName(ls_FieldName).Value;
ExcelApp.Cells(i+2, j+1) := ls_Value;
end; end;
CurDataGrid.DataSource.DataSet.Next;
end; //设置边框
//ls_Str = "A3:"+Excel_ColTag(nColCount)+IntToStr(nRowCount);
ls_Str := 'A'+IntToStr(li_RowNo+1)+':'+Excel_ColTag(nColCount)+IntToStr(nRowCount);
ExcelApp.Range[ls_Str].Borders.LineStyle := 1;
//ExcelApp.Range[ls_Str].Borders[2].Weight := 3; {
if Total then
begin
Col := 1;
for J := 0 to DBGrid.Columns.Count - 1 do
begin
S := Char(64 + ((J+1) mod 26));
if (J+1) > 26 then
begin
S := Char(65+(((J+1)-26) div 26)) + S;
end;
if J = 0 then
begin
ExcelApp.Cells(Row, Col) := '合计';
end
else if DBGrid.Columns[J].Field.DataType in [ftInteger, ftSmallint, ftFloat, ftBCD] then
begin
FieldName := DBGrid.Columns[J].FieldName;
ExcelApp.Cells(Row, Col) := '=SUM('+S+'4:'+S+IntToStr(Row-1)+')';
end;
Col := Col + 1;
end;
end;
}
try
WorkBook.SaveAs(ls_FileName);
except
SetMDsuiStop('要保存的文件不能与已打开的文件重名',LoadStr(10005),1);
suiMessageDialog1.ShowModal;
end;
except
SetMDsuiStop(LoadStr(10012),LoadStr(10005),1);
suiMessageDialog1.ShowModal();
end; ExcelApp := Unassigned;
WorkBook := Unassigned;
end;