RT
谢谢

解决方案 »

  1.   

    理论上ListView和TreeView都是同宗,都可以导出为文件...........但是我没有找到ListView的方法...........于是乎,建议一行行读取也可行,只是效率低点...........
    还有一点,20分......这个分数也太高了点吧,呵呵
      

  2.   

    //导出ListView中的内容
    //****参数说明*********
    //OutPutList表示要导出的ListView
    //Selected 表示是否只导出被Checked = True的列
    //****返回值意义*******
    //导出成功则返回True,失败则为False
     
    function TFormMain.OutPutListView(OutPutList: TsuiListView;
        Selected: Boolean; ProgressBar1: TProgressBar): Boolean;
    var
        FileName: string;
        fileType: string;
        Addstr: string;
     
        i, tbcol, elrow: Integer; //tbcol列数, elrow行数
        ExcelApplication1: TExcelApplication;
        ExcelWorksheet1: TExcelWorksheet;
        ExcelWorkbook1: TExcelWorkbook;
        SaveDialog1: TSaveDialog;
     
        F: textfile;
     
    begin
        Result := False;
        SaveDialog1 := TSaveDialog.Create(Application);
        SaveDialog1.DefaultExt := 'xls';
        SaveDialog1.Filter :=
            'Excel文件(*.xls)|*.xls|CSV(逗号风隔)(*.csv)|*.csv|文本文档(*.txt)|*.txt';
        if SaveDialog1.Execute then
        begin
            FileName := SaveDialog1.FileName;
            fileType := Copy(FileName, Length(FileName) - 2, 3);
            if fileType = 'xls' then
            begin
                //------申明Excel对象
                try
                    ExcelApplication1 := TExcelApplication.Create(Application);
                    ExcelWorksheet1 := TExcelWorksheet.Create(Application);
                    ExcelWorkbook1 := TExcelWorkbook.Create(Application);
                    ExcelApplication1.Connect;
                except
                    Application.MessageBox('Excel没有安装!', 'Hello', MB_ICONERROR +
                        mb_Ok);
                    exit;
                end;
                //------实例化Excel对象
                try
                    ExcelApplication1.Workbooks.Add(EmptyParam, 0);
                    ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
                    ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as
                        _worksheet);
     
                    tbcol := OutPutList.Columns.Count - 1;
     
                    suiProgressBar1.Visible := True;
                    suiProgressBar1.Max := OutPutList.Items.Count;
                    suiProgressBar1.Min := 0;
                    suiProgressBar1.Position := 0;
     
                    for i := 0 to tbcol do
                    begin
                        ExcelWorksheet1.Cells.Item[1, i + 1] :=
                            OutPutList.Columns[ i ].Caption;
                    end;
     
                    for elrow := 0 to OutPutList.Items.Count - 1 do
                    begin
                        if (Selected = False) or (OutPutList.Items[elrow].Checked =
                            True)
                            then
                        begin
                            for i := 0 to tbcol do
                            begin
                                if i = 0 then
                                begin
                                    ExcelWorksheet1.Cells.Item[elrow + 2, i + 1] :=
                                        OutPutList.Items[elrow].Caption;
                                end
                                else
                                begin
                                    ExcelWorksheet1.Cells.Item[elrow + 2, i + 1] :=
                                        OutPutList.Items[elrow].SubItems[i - 1];
                                end;
                            end;
                        end;
                        //elrow := elrow + 1;
                        suiProgressBar1.Position := suiProgressBar1.Position + 1;
                    end;
                    suiProgressBar1.Hide;
     
                    ExcelWorksheet1.SaveAs(FileName);
                    Application.MessageBox('导出数据正确!', '提示信息', mb_Ok +
                        MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
                    ExcelApplication1.Disconnect;
                    ExcelApplication1.Quit;
                    ExcelApplication1.Free;
                    ExcelWorksheet1.Free;
                    ExcelWorkbook1.Free;
                except
                    Application.MessageBox('导入数据出错!请检查文件的格式是否正确!', '提示信息', mb_Ok +
     
                        MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
                    ExcelApplication1.Disconnect;
                    ExcelApplication1.Quit;
                    ExcelApplication1.Free;
                    ExcelWorksheet1.Free;
                    ExcelWorkbook1.Free;
                end;
            end
            else
            begin
                //声明一个文件
                try
                    AssignFile(F, FileName);
                    rewrite(F);
     
                    suiProgressBar1.Visible := True;
                    suiProgressBar1.Max := OutPutList.Items.Count;
                    suiProgressBar1.Min := 0;
                    suiProgressBar1.Position := 0;
     
                    tbcol := OutPutList.Columns.Count - 1;
                    Addstr := '';
                    for i := 0 to tbcol do
                    begin
                        Addstr := Addstr + OutPutList.Columns[ i ].Caption + ',';
                    end;
     
                    Addstr := Copy(Addstr, 0, Length(Addstr) - 1);
                    Addstr := Addstr;
                    Writeln(F, Addstr);
     
                    for elrow := 0 to OutPutList.Items.Count - 1 do
                    begin
                        Addstr := '';
                        if (Selected = False) or (OutPutList.Items[elrow].Checked =
                            True)
                            then
                        begin
                            for i := 0 to tbcol do
                            begin
                                if i = 0 then
                                begin
                                    Addstr := Addstr +
                                        OutPutList.Items[elrow].Caption +
                                        ',';
                                end
                                else
                                begin
                                    Addstr := Addstr +
                                        OutPutList.Items[elrow].SubItems[i
                                        - 1] +
                                        ',';
                                end;
                            end;
                            Addstr := Copy(Addstr, 0, Length(Addstr) - 1);
                            Writeln(F, Addstr);
                        end;
                        //elrow := elrow + 1;
                        suiProgressBar1.Position := suiProgressBar1.Position + 1;
                    end;
                    suiProgressBar1.Hide;
                    CloseFile(F);
                    Application.MessageBox('导出数据正确!', '提示信息', mb_Ok +
                        MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
                except
                    Application.MessageBox('导出数据出错!请检查文件的格式是否正确!', '提示信息', mb_Ok +
     
                        MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
                end;
            end;
        end;
    end;