用一个递归函数GetNodeString(TreeNode td)来将整个TreeView转化成代缩进的格式化字符串,当然,缩进的距离你可以自己控制:
private int SpaceNum=0;  //空格数,记录缩进
public string GetTreeViewString(TreeView treeV)
{
string str="";
foreach(TreeNode nd in treeV.Nodes)
{
str=GetNodeString(nd);
}
return str;
}private string GetSpaceStr(int num)
{
string str = "";
for(int i=0;i<num;i++)
{
str+=" ";
}
return str;
}private string GetNodeString(TreeNode td)
{
string str="";
i++;
str=GetSpaceStr(i)+td.Text + "\r\n";
foreach(TreeNode nd in td.Nodes)
{
str+=GetNodeString(nd);
}
i--;
return str;
}

解决方案 »

  1.   

    这是一人listViewVB例子,应该可以解决你的问题了
    新建一个项目,添加:
    一个Windows form
    一个ListView
    一个button
    一个PageSetupDialog
    一个PrintPreviewDialog
    一个PrintDocument然后添加如下代码:Public Class Form1
        Inherits System.Windows.Forms.Form    Dim tableFont, titleFont As Font    Dim X1, X2, X3 As Integer    Dim W1, W2, W3 As Integer    Dim Y As Integer    Dim itm As Integer
    ......Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings()        If PageSetupDialog1.ShowDialog() Then            PrintDocument1.DefaultPageSettings = _
                  PageSetupDialog1.PageSettings        End If        tableFont = New Font("Arial", 8)        titleFont = New Font("Arial", 12, FontStyle.Bold)        X1 = PrintDocument1.DefaultPageSettings.Margins.Left        Dim pageWidth As Integer        With PrintDocument1.DefaultPageSettings            pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right        End With        X2 = X1 + 100        X3 = X2 + pageWidth * 0.5        W1 = X2 - X1        W2 = X3 - X2        W3 = pageWidth - X3        PrintPreviewDialog1.Document = PrintDocument1        PrintPreviewDialog1.ShowDialog()        itm = 0
        End Sub    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim BookItem As New ListViewItem()        BookItem.Text = "0393049515"        BookItem.SubItems.Add("The Dream of Reason: " & "A History of Philosophy from the Greeks to the Renaissance")        BookItem.SubItems.Add("Anthony Gottlieb")        ListView1.Items.Add(BookItem)        BookItem = New ListViewItem()        BookItem.Text = "0156445085"        BookItem.SubItems.Add("In Search of the Miraculous: " & "Fragments of an Unknown Teaching")        BookItem.SubItems.Add("P. D. Ouspensky")        ListView1.Items.Add(BookItem)
        End Sub    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Y = PrintDocument1.DefaultPageSettings.Margins.Top + 20        e.Graphics.DrawString("ISBN", titleFont, Brushes.Black, X1, Y)        e.Graphics.DrawString("Title", titleFont, Brushes.Black, X2, Y)        e.Graphics.DrawString("Author(s)", titleFont, Brushes.Black, X3, Y)        Y = Y + 30        While itm < ListView1.Items.Count            Dim str As String            str = ListView1.Items(itm).Text            e.Graphics.DrawString(str, tableFont, Brushes.Black, X1, Y)            str = ListView1.Items(itm).SubItems(1).Text            Dim R As New RectangleF(X2, Y, W2, 80)            e.Graphics.DrawString(str, tableFont, Brushes.Black, R)            Dim lines, cols As Integer            e.Graphics.MeasureString(str, tableFont, New SizeF(W2, 50), New StringFormat(), cols, lines)            Dim subitm As Integer, Yc As Integer            Yc = Y            For subitm = 2 To ListView1.Items(itm).SubItems.Count - 1                str = ListView1.Items(itm).SubItems(subitm).Text                e.Graphics.DrawString(str, tableFont, _
                        Brushes.Black, X3, Yc)                Yc = Yc + tableFont.Height + 2            Next            Y = Y + lines * tableFont.Height + 5            Y = Math.Max(Y, Yc)            With PrintDocument1.DefaultPageSettings                e.Graphics.DrawLine(Pens.Black, .Margins.Left, Y, .PaperSize.Width - .Margins.Right, Y)                If Y > 0.95 * (.PaperSize.Height - .Margins.Bottom) Then                    e.HasMorePages = True                    Exit Sub                End If            End With            itm = itm + 1        End While        e.HasMorePages = False
        End Sub
      

  2.   

    修改一下:
    输入写错了,第一个函数改一下:
    public string GetTreeViewString(TreeView treeV)
    {
    string str="";
    foreach(TreeNode nd in treeV.Nodes)
    {
    str+=GetNodeString(nd);
        \\        -------^--------------这一行
    }
    return str;
    }