2014-03-26 91 views
0

而從GridView的數據導出到Word文件,表格應在Word中創建,如何從GridView裏創建一個Word文檔中的表格與數據

表可能包含許多細胞,

在每個小區中,我需要存儲一個記錄從gridview的

I have exported the data from gridview to word, 


like this 


1  rasdf   sdf  
2  sdf   jlkj   
3  dfdf   dfdf   
4  dsaf   sdfdsfds   
5  king   mumbai 



but i need the output in table format in which Each record should be in one cell, second cell contains another records 
![OUTput view is attached with this link][1]  

enter image description here

出口這是代碼,我已經寫了

private void button5_Click(object sender, EventArgs e) 
     { 
         // Table table = new table(); 
      sfd.Filter = "Excel Documents (*.doc)|*.doc"; 

      if (sfd.ShowDialog() == DialogResult.OK) 
      { 
       ExportToWord(); 
       } 
     } 
private void ExportToWord() 
     { 
string strForPrint = ""; 

      //writing bill fields 
      // strForPrint += "Bill No : " + txtBillNo.Text + "\t"; 
      //strForPrint += "Date : " + dtpDate.Text + "\r\n\r\n"; 
      //strForPrint += "Customer Name : " + txtCustomer.Text + "\r\n\r\n"; 
      //strForPrint += "Remarks : " + txtRemarks.Text + "\r\n\r\n\r\n"; 
      strForPrint += "-----Bill Detail-----" + "\r\n\r\n\r\n"; 


       // writing datagridview column titles: 

       string strHeaderTitle = ""; 

       for (int j = 0; j < dataGridView1.Columns.Count; j++) 
       { 
        strHeaderTitle = strHeaderTitle.ToString() + Convert.ToString(dataGridView1.Columns[j].HeaderText) + "\t\t"; 
       } 

       //strForPrint += strHeaderTitle + "\r\n"; 

       // writing datagridview data. 

       for (int i = 0; i < dataGridView1.RowCount - 1; i++) 
       { 
        string strLineData = ""; 

        for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++) 
        { 
          strLineData = strLineData.ToString() + Convert.ToString(dataGridView1.Rows[i].Cells[j].Value); 
          if (j == 1) 
          { 
           strLineData = strLineData + "\t\t\t"; 
          } 
          else 
          { 
           strLineData = strLineData + "\t\t"; 
          } 
         } 


         strForPrint += strLineData + "\r\n"; 


       } 


      Encoding utf16 = Encoding.GetEncoding(1254); 


      byte[] output = utf16.GetBytes(strForPrint); 

      FileStream fs = new FileStream(sfd.FileName, FileMode.Create); 

      BinaryWriter bw = new BinaryWriter(fs); 

      bw.Write(output, 0, output.Length); //write data into file 
      MessageBox.Show("File Created....."); 

      bw.Flush(); 

      bw.Close(); 

      fs.Close(); 
     } 
+0

當心,這條線是錯誤的: 「sfd.Filter = 」Excel文檔格式(* .doc)|的* .doc「;」 Excel擴展名是xsl或xlsx,而不是doc – Oscar

+0

它是word文檔「sfd.Filter =」word文檔(.doc)| .doc「;」 – user3445382

回答

0

這是創建一個字的文件。即使它可以工作,你不會有格式的控制,你似乎需要一個真正醜陋的方式。我強烈建議使用一些庫來創建真正的Word文檔,如WordDocumentGenerator或類似文檔。

http://worddocgenerator.codeplex.com/

另一種方法可以是使用一個XSL模板來變換從數據庫獲得的XML數據。

http://securityroots.com/dradispro/support/guides/reports_xslt.html

http://msdn.microsoft.com/en-us/library/ee872374(v=office.12).aspx

+0

雅謝謝m檢查 – user3445382

+1

我喜歡[DocX](http://www.codeproject.com/Articles/660478/Csharp-Create-and-Manipulate-Word-Documents-Progra),通過NuGet提供,非常易於使用。 – mason

相關問題