2017-03-25 7 views
0

C#でMicrosoft Word文書を作成しようとしていますが、データベースから取得したデータをMicrosoft Wordでテーブルに変換して保存しています。ドキュメントが作成されたときの問題は、コードの範囲内で検出する範囲だけでなく、データベースから接続するテーブルの行数と等しい行数のテーブルを作成することです。 Ex:Examsテーブルはまったく7rows、私の条件はちょうど4行を取得し、コードは7行でドキュメントを作成し、4はデータベースからの情報を持ち、3はnull行です。 と私のコードその:C#でMicrosoft Wordにエクスポート

public void create_Document() 
    { 
     try 
      { 
       //Create an instance for word app 
       Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application(); 

       //Set animation status for word application 
       winword.ShowAnimation = false; 

       //Set status for word application is to be visible or not. 
       winword.Visible = false; 

       //Create a missing variable for missing value 
       object missing = System.Reflection.Missing.Value; 

       //Create a new document 
       Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing); 

       //Add header into the document 
       foreach (Microsoft.Office.Interop.Word.Section section in document.Sections) 
       { 
        //Get the header range and add the header details. 
        Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; 
        headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage); 
        headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; 
        headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue; 
        headerRange.Font.Size = 30; 
        headerRange.Text = "Exam Dates for the "+SelectCourse2.SelectedItem.ToString(); 
       } 



       //adding text to document 
       document.Content.SetRange(0, 0); 
       // document.Content.Text = "Exam Dates :" + Environment.NewLine; 

       //Add paragraph with Heading 1 style 
       Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing); 
       object styleHeading1 = "Heading 1"; 
       para1.Range.set_Style(ref styleHeading1); 
       para1.Range.Text = "Exam Dates"; 
       para1.Range.InsertParagraphAfter(); 



       //Create a 5X5 table and insert some dummy record 
       // Microsoft.Office.Interop.Word.Table firstTable = document.Tables.Add(para1.Range, 4,2, ref missing, ref missing); 
       Microsoft.Office.Interop.Word.Table firstTable = document.Tables.Add(para1.Range, 4,2, ref missing, ref missing); 

       firstTable.Borders.Enable = 1; 

       firstTable.Cell(1, 1).Range.Text = "Exam Name"; 
       firstTable.Cell(1, 2).Range.Text = "Exam Date"; 

       connection.Open(); 
       SqlCommand cmd = new SqlCommand("select ExamName,Date from Exam where CourseNum='"+SelectCourse2.SelectedValue+"'", connection); 
       SqlDataReader reader = cmd.ExecuteReader(); 

      int intRow = 2; 

      // Retrieve the data and insert into new rows. 
      Object beforeRow = Type.Missing; 
      while (reader.Read()) 
      { 
       firstTable.Rows.Add(ref beforeRow); 
       firstTable.Cell(intRow, 1).Range.Text = reader[0].ToString(); 
       firstTable.Cell(intRow, 2).Range.Text = reader[1].ToString(); 
       // firstTable.Cell(intRow, 3).Range.Text = reader[2].ToString(); 
       intRow += 1; 
      } 




        // Word.WdParagraphAlignment.wdAlignParagraphRight; 
       //Save the document 
       object filename = @"C:\\Users\\Rawan Mansour\\Desktop\\temp1.docx"; 
       document.SaveAs2(ref filename); 
       ((Microsoft.Office.Interop.Word._Document)document).Close(ref missing, ref missing, ref missing); 
       ((Microsoft.Office.Interop.Word._Application)winword).Quit(ref missing, ref missing, ref missing); 


       MessageBox.Show("Document created successfully !"); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 

     } 
+0

を置くことにより、あなたドン場合、あなたはおそらく答えを得ることはありませんそれをもっと明確にする。あなたが望むものの例がここで助けになるでしょう。 –

+0

私はそれを編集しましたが、誰も私にお答えできませんか? –

答えて

0

が、私は解決策を見つけた:最後のフレーズの要件はかなり不明である範囲(1,2)

関連する問題