Delphi-PRAXiS
Seite 1 von 3  1 23      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Excel Bordereinstellungen (https://www.delphipraxis.net/122515-excel-bordereinstellungen.html)

ErdNussLocke 17. Okt 2008 11:03


Excel Bordereinstellungen
 
Hi,

ich verwende diesen Code aus der DH, um die Daten eines Stringgrid nach Excel zu exportieren:

Delphi-Quellcode:
//StringGrid-Inhalt nach Excel exportieren
function StringGridToExcelSheet(Grid: TStringGrid; SheetName, FileName: string;
  ShowExcel: Boolean): Boolean;
const
  xlWBATWorksheet = -4167;
var
  SheetCount, SheetColCount, SheetRowCount, BookCount: Integer;
  XLApp, Sheet, Data: OLEVariant;
  I, J, N, M: Integer;
  SaveFileName : string;
begin
  //notwendige Sheetanzahl feststellen
  SheetCount := (Grid.ColCount div 256) + 1;
  if Grid.ColCount mod 256 = 0 then
     SheetCount := SheetCount - 1;
  //notwendige Bookanzahl feststellen
  BookCount := (Grid.RowCount div 65536) + 1;
  if Grid.RowCount mod 65536 = 0 then
     BookCount := BookCount - 1;

  //Create Excel-OLE Object
  Result := False;
  XLApp := CreateOleObject('Excel.Application');
  try
    //Excelsheet anzeigen
    if ShowExcel = false then
       XLApp.Visible := False
    else
       XLApp.Visible := True;
    //Workbook hinzufügen
    for M := 1 to BookCount do
    begin
        XLApp.Workbooks.Add(xlWBATWorksheet);
        //Sheets anlegen
        for N := 1 to SheetCount - 1 do
        begin
          XLApp.Worksheets.Add;
        end;
    end;
    //Sheet ColAnzahl feststellen
    if Grid.ColCount <= 256 then
       SheetColCount := Grid.ColCount
    else
       SheetColCount := 256;
    //Sheet RowAnzahl feststellen
    if Grid.RowCount <= 65536 then
       SheetRowCount := Grid.RowCount
    else
       SheetRowCount := 65536;

    //Sheets befüllen
    for M := 1 to BookCount do
    begin
        for N := 1 to SheetCount do
        begin
          //Daten aus Grid holen
          Data := VarArrayCreate([1, Grid.RowCount, 1, SheetColCount], varVariant);
          for I := 0 to SheetColCount - 1 do
            for J := 0 to SheetRowCount - 1 do
              if ((I+256*(N-1)) <= Grid.ColCount) and ((J+65536*(M-1)) <= Grid.RowCount) then
                Data[J + 1, I + 1] := Grid.Cells[I+256*(N-1), J+65536*(M-1)];

          XLApp.Worksheets[N].Select;
          XLApp.Workbooks[M].Worksheets[N].Name := SheetName + IntToStr(N);
          //Zellen als String Formatieren
          XLApp.Workbooks[M].Worksheets[N].Range[RefToCell(1, 1), RefToCell(SheetRowCount,
            SheetColCount)].Select;
          XLApp.Selection.NumberFormat := '@';
          XLApp.Workbooks[M].Worksheets[N].Range['A1'].Select;
          //Daten dem Excelsheet übergeben
          Sheet := XLApp.Workbooks[M].WorkSheets[N];
          Sheet.Range[RefToCell(1, 1), RefToCell(SheetRowCount,SheetColCount)].Value := Data;

        end;
    end;
    //Save Excel Worksheet
    try
      for M := 1 to BookCount do
      begin
          SaveFileName :=Copy(FileName,1,Pos('.',FileName)-1) +
          Copy(FileName,Pos('.',FileName),
          Length(FileName)-Pos('.',FileName)+1);
          if fileexists(pfadE+Form1.EExportname.text+'.xls')=true
           then deletefile(pfadE+Form1.EExportname.text+'.xls');
          XLApp.Workbooks[M].SaveAs(SaveFileName);
      end;                                          
      Result := True;
    except
      // Error?
    end;
  finally
    //Excel beenden
      XLApp.DisplayAlerts := False;
      XLApp.Quit;
      XLAPP := Unassigned;
      Sheet := Unassigned;
  end;
end;
Das funktoniert auch wunderbar, jetzt möchte ich allerdings, dass um jede Zelle ein vollständige Rahmen gezeichnet wird
(Es soll beim Ausdrucken eine schöne Tabelle sein)
Hab schon hier gelesen
http://www.delphipraxis.net/internal...t=excel+border
aber hab es nicht geschafft es zu übertragen (verwende Excel 2007)
Kann mir wer helfen? :)
Danke schonmal,

MfG
ErdNussLocke

ErdNussLocke 17. Okt 2008 11:43

Re: Excel Bordereinstellungen
 
Sorry, eigentlich sollte das in "Object-Pascal / Delphi-Language", vllt kann es ja wer verschieben :)

nahpets 17. Okt 2008 12:32

Re: Excel Bordereinstellungen
 
Hallo,

hab' mal mit Excel ein Makro aufgezeichnet.
Code:
Sub Makro1()
    Cells.Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
End Sub
Das solltest Du nach Delphi übersetzt bekommen.

ErdNussLocke 20. Okt 2008 09:26

Re: Excel Bordereinstellungen
 
Hi,

danke schonmal für die Antwort!
Aber muss ich dazu noch eine uses eintragen? Weil xldiagonal etc. kennt er nicht :)

MfG
ErdNussLocke

Reinhardtinho 20. Okt 2008 09:35

Re: Excel Bordereinstellungen
 
Bei mir stehen die Werte in der Excel97-Unit. Du musst mal gucken, was bei dir drauf ist, könnte auch Excel2000 oder ExcelXP heißen.

ErdNussLocke 20. Okt 2008 09:46

Re: Excel Bordereinstellungen
 
Gut :) Mit ExcelXP hat er schonmal die Befehle, allerdings sagt er nun beim Exportieren, dass
die Methode 'Selection' vom Automatisierungsobjekt nicht unterstützt wird. Das sagt mir leider
nicht so viel :-/ (verwende Delphi 7, Excel 2007)

MfG`
ErdNussLocke

Reinhardtinho 20. Okt 2008 09:52

Re: Excel Bordereinstellungen
 
Zeig mal deinen Code, das Makro muss ja nach Delphi übersetzt werden.

ErdNussLocke 20. Okt 2008 10:04

Re: Excel Bordereinstellungen
 
Inzwischen zeigt er "Mitglied nicht gefunden".

Delphi-Quellcode:
    //Sheets befüllen
    for M := 1 to BookCount do
    begin
        for N := 1 to SheetCount do
        begin
          //Daten aus Grid holen
          Data := VarArrayCreate([1, Grid.RowCount, 1, SheetColCount], varVariant);
          for I := 0 to SheetColCount - 1 do
            for J := 0 to SheetRowCount - 1 do
              if ((I+256*(N-1)) <= Grid.ColCount) and ((J+65536*(M-1)) <= Grid.RowCount) then
                Data[J + 1, I + 1] := Grid.Cells[I+256*(N-1), J+65536*(M-1)];

          XLApp.Worksheets[N].Select;
          XLApp.Workbooks[M].Worksheets[N].Name := SheetName + IntToStr(N);
          //Zellen als String Formatieren
          XLApp.Workbooks[M].Worksheets[N].Range[RefToCell(1, 1), RefToCell(SheetRowCount,
            SheetColCount)].Select;
          XLApp.Selection.NumberFormat := '@';
          XLApp.Workbooks[M].Worksheets[N].Range['A1'].Select;

//Macro

          XLApp.Selection.Borders(xlDiagonalDown).LineStyle := xlNone;
          XLApp.Selection.Borders(xlDiagonalUp).LineStyle := xlNone ;
          XLApp.Selection.Borders(xlEdgeLeft).LineStyle := xlContinuous;
          XLApp.Selection.Borders(xlEdgeLeft).Weight := xlThin;
          XLApp.Selection.Borders(xlEdgeLeft).ColorIndex := xlAutomatic;
          XLApp.Selection.Borders(xlEdgeTop).LineStyle := xlContinuous;
          XLApp.Selection.Borders(xlEdgeTop).Weight := xlThin;
          XLApp.Selection.Borders(xlEdgeTop).ColorIndex := xlAutomatic;
          XLApp.Selection.Borders(xlEdgeBottom).LineStyle := xlContinuous;
          XLApp.Selection.Borders(xlEdgeBottom).Weight := xlThin;
          XLApp.Selection.Borders(xlEdgeBottom).ColorIndex := xlAutomatic;
          XLApp.Selection.Borders(xlEdgeRight).LineStyle := xlContinuous;
          XLApp.Selection.Borders(xlEdgeRight).Weight := xlThin;
          XLApp.Selection.Borders(xlEdgeRight).ColorIndex := xlAutomatic;
          XLApp.Selection.Borders(xlInsideVertical).LineStyle := xlContinuous;
          XLApp.Selection.Borders(xlInsideVertical).Weight := xlThin;
          XLApp.Selection.Borders(xlInsideVertical).ColorIndex := xlAutomatic;
          XLApp.Selection.Borders(xlInsideHorizontal).LineStyle := xlContinuous;
          XLApp.Selection.Borders(xlInsideHorizontal).Weight := xlThin;
          XLApp.Selection.Borders(xlInsideHorizontal).ColorIndex := xlAutomatic;

//Macroende

          //Daten dem Excelsheet übergeben
          Sheet := XLApp.Workbooks[M].WorkSheets[N];
          Sheet.Range[RefToCell(1, 1), RefToCell(SheetRowCount,SheetColCount)].Value := Data;

        end;
    end;

ErdNussLocke 27. Okt 2008 11:41

Re: Excel Bordereinstellungen
 
Oje, war das jetzt so grob falsch, dass keiner mehr weiter mag :D
Sorry :-/ Aber vllt kann sich ja doch noch wer durchringen :)

Reinhardtinho 27. Okt 2008 11:43

Re: Excel Bordereinstellungen
 
An welcher Stelle kommt denn der Fehler?


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:32 Uhr.
Seite 1 von 3  1 23      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz