1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140:
| unit neuversuch;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids;
type TForm1 = class(TForm) Laden: TButton; LbxText: TListBox; odgdialoge: TOpenDialog; procedure LadenClick(Sender: TObject);
private function Loadlbformexcel(strPfad: string):string; public end;
var Form1: TForm1;
implementation
uses Unit4ABFRAGE, ComObj;
{$R *.dfm}
procedure TForm1.LadenClick(Sender: TObject);
var msLaden : TMemoryStream; ssText : TStringStream; slText: TStringList; strPfad : String; strText : String; begin
if odgdialoge.execute then begin strPfad := odgdialoge.FileName;
Application.CreateForm(TForm4, Form4); if Form4.ShowModal <> mrOk then begin exit; end; end else begin exit; end; msLaden := TMemoryStream.Create; ssText := TStringStream.Create(''); slText := TStringList.Create; try strText := Loadlbformexcel(strPfad);
msLaden.LoadFromFile(strPfad); msLaden.SaveToStream(ssText); slText.Text := ssText.DataString; if Form4.intAuswahl = 0 then begin if ExtractFileExt(strPfad) = '.txt' then begin lbxText.Items.Text := lbxText.Items.Text + ssText.DataString; end else begin lbxText.Items.Text := lbxText.Items.Text + strText; end; end else begin if ExtractFileExt (strPfad) = '.txt'then begin lbxText.Items.Text := ssText.DataString; end else lbxText.Items.Text := strText; end;
finally if Assigned(msLaden) then FreeAndNil(msLaden); if Assigned(ssText) then FreeAndNil(ssText); if Assigned(slText) then FreeAndNil(slText); end;
end;
function TForm1.Loadlbformexcel(strPfad: string): string;
const xlCellTypeLastCell = $0000000B; var XLApp, Sheet: OLEVariant; RangeMatrix: Variant; x, y, k :integer; strTmp: String;
begin Result := ''; XLApp := CreateOleObject('Excel.Application'); try XLApp.Visible := False; XLApp.Workbooks.Open(strPfad); Sheet := XLApp.Workbooks[ExtractFileName(strPfad)].WorkSheets[1]; Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate; x := XLApp.ActiveCell.Row; y := XLApp.ActiveCell.Column; RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]] .Value; strTmp := ''; for k := 1 to x do begin if strTmp <> '' then begin strTmp := strTmp + #13#10; end; strTmp := strTmp + String(sheet.cells[k,1]); end;
result := strTmp; finally if not VarIsEmpty(XLApp) then try XLApp.Visible := False; XLApp.Workbooks.Open(strPfad); Sheet := XLApp.Workbooks[ExtractFileName(strPfad)].WorkSheets[1]; Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate; x := XLApp.ActiveCell.Row; y := XLApp.ActiveCell.Column; RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]] .Value; strTmp := ''; for k := 1 to x do begin if strTmp <> '' then begin strTmp := strTmp + #13#10; end; strTmp := strTmp + String(sheet.cells[k,1]); end;
result := strTmp; finally if not VarIsEmpty(XLApp) then begin XLApp.Quit; XLAPP := Unassigned; Sheet := Unassigned; end; end; end; end; end. |