English 中文(简体)
出口虚拟商品,以提炼,csv?
原标题:Export a VirtualStringTree to excel, csv?
最佳回答

我们有一个<条码>ExcelWriter求助类别,可向Excel(例如)倾倒各种物品。

  • TADOQuery
  • TListView
  • TVirtualStringTree

在此情况下,快速通道(在此易于复制-帕斯特)是可向Excel倾斜的超载:

class function TExcelWriter.ExportToExcelVariantArray(const VArray: Variant; const Title, SubTitle: WideString): Boolean;
var
    xl: OleVariant;
    workbook: OleVariant;
    worksheet: OleVariant;
    range: OleVariant;
    Rowcount, ColumnCount: Integer;
    HeaderRowIndex: Integer;
    s: WideString;
begin
    Result := False;

    if not VarIsArray(VArray) then
        raise EExcelWriterException.Create( ExportToExcelVariantArray: Supplied variant is not an array );

    if VarArrayDimCount(VArray) <> 2 then
        raise EExcelWriterException.Create( ExportToExcelEVariantArray: Supplied variant array does not have 2 dimensions ( +IntToStr(VarArrayDimCount(VArray))+ ) );

    ColumnCount := VarArrayHighBound(VArray, 2) - VarArrayLowBound(VArray, 2); //2 for "leftmost dimension"
    rowCount := VarArrayHighBound(VArray, 1) - VarArrayLowBound(VArray, 1); //1 for "leftmost dimension"

    try
        xl := CreateOleObject( Excel.Application );
    except
        on E:Exception do
        begin
            if (E is EOleSysError) then
            begin
                if EOleSysError(E).ErrorCode = CO_E_CLASSSTRING then
                    raise EExcelWriterException.Create( Excel is not installed. +CRLF+
                             Could not load "Excel.Application" object (Co_E_CLASSSTRING) )
                else
                    raise;
            end
            else
                raise;
        end;
    end;
    try
        xl.ScreenUpdating := False;
        xl.DisplayAlerts := False;  // Don t display dialogs such as "save changes to workbook".


        workbook := xl.Workbooks.Add;
        try
            Worksheet := Workbook.Worksheets[1];
            try
                Worksheet.Activate;
                Worksheet.Cells.WrapText := False;

                HeaderRowIndex := 1; //Rows&Columns in Excel start at one.
                range := TExcelWriter.GetRange(worksheet, HeaderRowIndex, 1, HeaderRowIndex+RowCount, ColumnCount);
                range.Value := VArray;

                //Bold the header row
                Worksheet.Rows[HeaderRowIndex].Font.Bold := True;
                Worksheet.Rows[HeaderRowIndex].Font.Underline := True;
                Worksheet.Columns.AutoFit;

                //Set printed header&footer
                if Copy(Title, 1, 2) =  @@  then
                    s := Copy(Title, 3, MaxInt)
                else
                    s := Title;
                if SubTitle <>    then
                begin
                    if s <>    then s := s+#13;
                    s := s + SubTitle;
                end;

                TExcelWriter.SetHeaderAndFooters(Worksheet,
                        s,   ,   ,
                         &D &T ,   ,  Page &P of &N );
            finally
                Worksheet := Unassigned;
            end;
        finally
            Workbook := Unassigned;
        end;

        //When all done
        xl.ScreenUpdating := True;
        xl.Visible := True;
        xl.UserControl := True; // Very important, prevents Excel from going
                  // away when we nil out our reference to it below.
    finally
        xl := Unassigned;
    end;

    Result := True;
end;

我们有<条码>TVirtualListView的后代 TVirtualStringTre,使之容易过渡到虚拟轨道(有<条码> TVirtualListItem等)。 然后,它有一套助手方法<代码>Content ToVariantArray,类似于Content ToHtml

function TVirtualListView.ContentToVariantArray: Variant;
var
    Columns: TColumnsArray;
    VArray: Variant;
    Node: PVirtualNode;
    ColumnCount: Integer;
    RowCount: Integer;
    nRow: Integer;
    i: Integer;
begin
    Columns := Self.Columns.GetVisibleColumns;

    ColumnCount := Length(Columns);
    RowCount := Self.Items.Count+1; //+1 for the heaader

    VArray := VarArrayCreate([0, RowCount-1, 0, ColumnCount-1], varOleStr); //Docs say cannot use varString, must be varOleStr (which is a BSTR i.e. WideString)

    nRow := 0;

    for i := 0 to ColumnCount-1 do
    begin
        VArray[nRow, i] := Self.Columns.Items[Columns[i].Index].Text;
    end;

    Node := Self.GetFirst;
    while Assigned(Node) do
    begin
        Inc(nRow);
        for i := 0 to ColumnCount-1 do
        begin
            VArray[nRow, i] := Self.Text[Node, Columns[i].Index];
        end;

        Node := Self.GetNextSibling(Node);
    end;

    Result := VArray;
end;

这里的主要倒数是,为了使用Excel,我们重新实现自动化。 这意味着,你的客户/服务器将需要安装Excel。

以上编码显示用户Excel(为只看一看一看便把档案保存到硬盘中是浪费的),而不是创建出口档案。 但很难打到<代码>。 Save or whatever the API is.

问题回答

CSV 比较容易。

你的数据是你自己的数据,因为虚拟数据库没有数据,因此它是一个虚拟集装箱。 因此,如果您的虚拟树体是TMyObjects(集装箱)清单的一种看法,那么它就应当比CSV的产出小一些,而如果你一栏可以按不同顺序排列,或者能够看到的一组子,那么虚拟树的含量将真正重要。

我建议你调查自由的JVCL JvCsvDataSet,这是撰写CSV档案的真正容易的方法。

如果你真的想要XLS产出的话,就有这方面的图书馆。

建立了CSV(与html)。

var ss : AnsiString;
...
 if ExtractFileExt(DestFileName)= .htm 
 then
     ss:=VST.ContentToHtml(tstAll,  Html exp )
 else
     ss:=VST.ContentToText(tstAll,  ; );

 with TFileStream.Create(DestFileName, fmCreate or fmShareDenyWrite) do
 begin
      Write(ss[1], length(ss));
      Free;
 end;




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签