2017-08-18 11 views
0

選択したDataGridview行の値をテーブルではなく通常の文書形式としてMS Wordにエクスポートしたいとします。以下のコードは動作していますが、テーブル形式にエクスポートしています。 助けてください。ありがとう!選択した行DataGridViewをvb.netのWord文書にエクスポートする

Private Sub BtnWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWord.Click 
    Try 

     'Private Sub BtnWord_Click(ByVal ParcelsDataGridView As DataGridView, ByVal filename As String) 
     Dim objWord As Word.Application 
     Dim objDoc As Word.Document 

     objWord = CreateObject("Word.Application") 
     objWord.Visible = True 
     objDoc = objWord.Documents.Add 

     Dim _RowCount As Integer = ParcelsDataGridView.Rows.Count - 1 
     Dim _ColCount As Integer = ParcelsDataGridView.Columns.Count - 1 

     Dim ht1 As Word.Table 

     ht1 = objDoc.Tables.Add(objDoc.Bookmarks.Item("\endofdoc").Range, _ 
           _RowCount + 1, _ColCount + 1) 
     ht1.Borders.OutsideColor = Word.WdColor.wdColorBlack 
     ht1.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle 
     ht1.Borders.InsideColor = Word.WdColor.wdColorBlack 
     ht1.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle 

     For i As Integer = 0 To _RowCount 
      ht1.Rows.Add() 
      For _col As Integer = 0 To _ColCount 
       Dim colType As Type = ParcelsDataGridView.Columns(_col).GetType 
       If colType.Name = "DataGridViewImageColumn" Then 
        Dim _image As Image = DirectCast(ParcelsDataGridView.Rows(i).Cells(_col).Value, Image) 
        Clipboard.SetImage(_image) 
        ht1.Cell(i + 1, _col + 1).Range.Paste() 
       Else 
        ht1.Cell(i + 1, _col + 1).Range.Text = _ 
        ParcelsDataGridView.Rows(i).Cells(_col).Value.ToString() 
       End If 
      Next 
     Next 
     objDoc.SaveAs2("C:/test.docx") 



    Catch ex As System.Exception 
     MsgBox(ex.Message) 
    End Try 

enter image description here

答えて

0

あなたは、Wordオブジェクトモデルがどのように動作するかを学ぶためにグーグルのビット、特にRangeオブジェクトを行う必要があります。次のようにテキストが行われる挿入

Dim r As Word.Range = d.Content 
r.InsertAfter("My heading: ") 
r.Bold = 1 
r.Collapse(Word.WdCollapseDirection.wdCollapseEnd) 
r.InsertAfter("my field value") 
r.Bold = 0 
r.InsertParagraphAfter() 
r.Collapse(Word.WdCollapseDirection.wdCollapseEnd) 
+0

こんにちはSSSは、私の質問に答えるために感謝します。これは一種複雑ですが、私はあなたの助言に従います。どうもありがとう。 – Facekianda

関連する問題