-1

我正在尝试从 vb.net 构建报告,这里是

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table

    For Each dc As BSPLib.Bills.BillDeliverychallan In Bill.BillDCs

        Dim dcpara As Word.Paragraph
        dcpara = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
        dcpara.Range.Font.Bold = 5
        dcpara.Range.Text = "Delivery Challan: " & dc.DCName
        dcpara.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
        dcpara.Format.SpaceBefore = 12
        dcpara.Format.SpaceAfter = 2   '24 pt spacing after paragraph.
        dcpara.Range.InsertParagraphAfter()

        Dim count As Integer = dc.BillItems.Count

        Dim r As Integer
        oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, count + 1, 7)
        oTable.Range.ParagraphFormat.SpaceBefore = 2
        oTable.Range.ParagraphFormat.SpaceAfter = 0
        oTable.Columns(1).Width = oWord.CentimetersToPoints(0.89)
        oTable.Columns(2).Width = oWord.CentimetersToPoints(5.58)
        oTable.Columns(3).Width = oWord.CentimetersToPoints(2)
        oTable.Columns(4).Width = oWord.CentimetersToPoints(0.6)
        oTable.Columns(5).Width = oWord.CentimetersToPoints(1.7)
        oTable.Columns(6).Width = oWord.CentimetersToPoints(0.5)
        oTable.Columns(7).Width = oWord.CentimetersToPoints(2.01)
        For r = 1 To count
            'BQT TABLES 15  NOS @   25.00   =   375.00
            oTable.Cell(r, 1).Range.Text = dc.BillItems(r - 1).ItemSerial & "."
            oTable.Cell(r, 1).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            oTable.Cell(r, 1).Range.Font.Bold = 0
            oTable.Cell(r, 2).Range.Text = dc.BillItems(r - 1).ItemName
            oTable.Cell(r, 2).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
            oTable.Cell(r, 2).Range.Font.Bold = 0
            oTable.Cell(r, 3).Range.Text = dc.BillItems(r - 1).Quantity & " " & dc.BillItems(r - 1).ItemMeasure
            oTable.Cell(r, 3).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            oTable.Cell(r, 3).Range.Font.Bold = 0

            oTable.Cell(r, 4).Range.Text = "@"
            oTable.Cell(r, 4).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            oTable.Cell(r, 4).Range.Font.Bold = 0
            oTable.Cell(r, 5).Range.Text = dc.BillItems(r - 1).PricePerUnit.ToString("F")
            oTable.Cell(r, 5).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            oTable.Cell(r, 5).Range.Font.Bold = 0
            oTable.Cell(r, 6).Range.Text = "="
            oTable.Cell(r, 6).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
            oTable.Cell(r, 6).Range.Font.Bold = 0
            oTable.Cell(r, 7).Range.Text = dc.BillItems(r - 1).TotalFinal.ToString("F")
            oTable.Cell(r, 7).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
            oTable.Cell(r, 7).Range.Font.Bold = 0
        Next
        oTable.Cell(r + 1, 7).Range.Text = (dc.Total / dc.Days).ToString("F")
        oTable.Cell(r + 1, 7).Range.Font.Bold = 0
        oTable.Cell(r, 7).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
        oTable.Rows.Alignment = Word.WdRowAlignment.wdAlignRowLeft

        'Using a line break before my next table
        Dim oLineBreak1 As Word.Paragraph
        oLineBreak1 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
        oLineBreak1.Format.SpaceAfter = 0
        oLineBreak1.Format.SpaceBefore = 0
        oLineBreak1.Range.Font.Size = 2

        oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 1, 2)
        oTable.Range.ParagraphFormat.SpaceAfter = 0
        oTable.Range.ParagraphFormat.SpaceBefore = 0
        If dc.Days > 1 Then
            oTable.Cell(1, 1).Range.Text = "    From " & dc.FromDate.ToShortDateString & " To " & dc.ToDate & " --- " & dc.Days & " days" & " For: " & dc.ForDate
        Else
            oTable.Cell(1, 1).Range.Text = "    From " & dc.FromDate.ToShortDateString & " To " & dc.ToDate & " --- " & dc.Days & " day" & " For:" & dc.ForDate
        End If
        oTable.Cell(1, 1).Width = oWord.CentimetersToPoints(12.44)
        oTable.Cell(1, 2).Width = oWord.CentimetersToPoints(2.67)
        oTable.Cell(1, 2).Range.Text = dc.Total.ToString("F")
        oTable.Cell(1, 2).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
        oTable.Rows.Alignment = Word.WdRowAlignment.wdAlignRowCenter




        'Dim break As Word.WdBreakType
        'End If
    Next

问题是,如果 for each 循环中的数据扩展到下一页,我希望 for 循环中的整个数据移动到下一页。我想添加一个页数整数并通过开始和结束来验证它以确定和更改页面。问题是我不知道如何在指定的行号处插入分页符。分页符是Word.WdBreakType.wdPageBreak但如何在行号处插入它?

谢谢你。

4

1 回答 1

2

您可以将段落格式设置为“保持在一起”:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.paragraph.keeptogether.aspx

确定 Microsoft Word 重新分页文档时指定段落中的所有行是否保持在同一页面上。

这可确保段落的各个行不会跨两页分开。

您还需要使用“ Keep with Next ”:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.paragraph.keepwithnext.aspx

确定当 Microsoft Word 重新分页文档时,指定的段落是否与后面的段落保持在同一页上。

这可确保各个段落不会在两页中彼此分开。

于 2012-05-09T17:08:05.940 回答