2013-08-17 30 views
3

我創建需要來自用戶的某些輸入,然後一個管理項目,正確的格式訪問寫字樓項目類形成C#應用程序

我創建了2個項目

  1. 窗口後打印出來申請表格(採取輸入字符串ñDataGridView中)

  2. 寫字樓項目(我的第一個項目的格式和打印數據)

我已經將office項目及其.dll文件導入到我的第一個項目 ,但問題是如何將參數(字符串n datagridview)傳遞給此office文檔的類項目它已經有一些參數,我不知道如何通過其內置的和新的參數,從1號項目

private void printButton_Click(object sender, EventArgs e) { 
dataGridView.Rows.Add("1", "a", "1", "1"); 
dataGridView.Rows.Add("2", "b", "2", "2"); 
dataGridView.Rows.Add("3", "c", "3", "3"); 
WordDocumentProject.ThisDocument = new ThisDocument(); 
} 
+0

我問前一陣子,如果你能告訴我們一些你的代碼,以更好地說明問題,這是一個非常活躍的網站,所以,當你問一個問題嘗試和周圍停留了一下。祝你好運! –

+0

//的ThisDocument(第2項目(辦公)類)的構造 公衆的ThisDocument(Microsoft.Office.Tools.Word.Factory工廠,全球:: System.IServiceProvider的ServiceProvider):基地(工廠的ServiceProvider 「的ThisDocument」,「的ThisDocument「) { Globals.Factory =工廠; } –

+0

//第一個項目(winForm)數據 private void printButton_Click(object sender,EventArgs e) { dataGridView.Rows.Add(「1」,「a」,「1」,「1」); dataGridView.Rows.Add( 「2」, 「B」, 「2」, 「2」); dataGridView.Rows.Add( 「3」, 「C」, 「3」, 「3」); //這裏是錯誤如何傳遞此文檔構造函數的參數(在上面給出的註釋中) WordDocumentProject.ThisDocument TDObj = new ThisDocument( } –

回答

0

其所有的記載,這裏是一個很好的例子:How to automate Microsoft Word to create a new document by using Visual C#

所以要通過在DataGridView字字符串來打印你這樣做像這樣:

//Start Word and create a new document. 
Word._Application oWord; 
Word._Document oDoc; 
oWord = new Word.Application(); 
oWord.Visible = true; 
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,ref oMissing, ref oMissing); 

//Insert a datagridview info into the document. 
DataTable dt = (DataTable)datagridview1.DataSource; 
foreach(DataRow dr in dt.Rows) 
{ 
Word.Paragraph oPara1; 
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); 
oPara1.Range.Text = dr[0].ToString(); 
oPara1.Range.Font.Bold = 1; 
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph. 
oPara1.Range.InsertParagraphAfter(); 
相關問題