S
shadow_1980
Unregistered / Unconfirmed
GUEST, unregistred user!
如果电脑中只安装office 2007,则创建文件的时候无法创建。 有没有知道这个问题的人啊。 delphi7版本var ExcelApp, ExcelWorkSheet: Variant; i, j, k: integer; strFileName: string;begin if not dbGrid.DataSource.DataSet.Active then Exit; if dm.sdExcel.Execute then strFileName := dm.sdExcel.FileName else Exit; try ExcelApp := CreateOleObject('Excel.application'); ExcelApp.Visible := false; ExcelApp.DisplayAlerts := false; except ExcelApp := Unassigned; MessageBox(Application.Handle, 'Excel程序打开失败。','提示', MB_OK + MB_ICONWARNING); Exit; end; ExcelApp.WorkBooks.Add; ExcelWorkSheet := ExcelApp.WorkBooks[1].WorkSheets[1]; ExcelWorkSheet.Name := GetFileName(strFileName); //写标题 j := 1; for i := 0 to dbGrid.Columns.Count - 1 do begin if dbGrid.Columns.Visible then begin ExcelWorkSheet.Cells[1,j].Value := dbGrid.Columns.Title.Caption; //设置列宽 ExcelWorkSheet.Columns[GetExcelColumn(j)].ColumnWidth := 20; inc(j); end; end; //写明细数据 dbGrid.DataSource.DataSet.First; j := 2; while not dbGrid.DataSource.DataSet.Eof do begin k := 1; for i := 0 to dbGrid.Columns.Count - 1 do begin if dbGrid.Columns.Visible then begin if dbGrid.Columns.Field.DataType in [ftDate, ftTime, ftDateTime] then begin ExcelWorkSheet.Cells[j,k].WrapText := True; if not dbGrid.Columns.Field.IsNull then begin ExcelWorkSheet.Cells[j,k].Value := dbGrid.Columns.Field.AsDateTime; end; end else begin if dbGrid.Columns.Field.DataType in [ftString,ftWideString] then ExcelWorkSheet.Cells[j,k].NumberFormatLocal := '@'; if not dbGrid.Columns.Field.IsNull then begin ExcelWorkSheet.Cells[j,k].WrapText := True; ExcelWorkSheet.Cells[j,k].Value := dbGrid.Columns.Field.AsString; end; end; inc(k); end; end; ExcelWorkSheet.Rows[j].RowHeight := 13.5; inc(j); dbGrid.DataSource.DataSet.Next; end; ExcelApp.WorkBooks[1].SaveAs(strFileName); ExcelApp.Visible := true;