通过C#.NET在Word中创建动态表 [英] Creating Dynamic Tables in Word by C#.NET
问题描述
这里是将datagridview复制到单词表的代码:
参考是Microsoft.Office.Interop.Word
C:\Program文件(x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\ Microsoft.Office.Interop.Word.dll
使用word = Microsoft.Office.Interop.Word;
public static void ExportToWord(DataGridView dgv)
{
SendMessage(Opening Word);
word.ApplicationClass word = null;
word.Document doc = null;
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc =\\endofdoc; / * \endofdoc是一个预定义的书签* /
try
{
word = new word.ApplicationClass();
word.Visible = true;
doc = word.Documents.Add(ref oMissing,ref oMissing,ref oMissing,ref oMissing);
}
catch(Exception ex)
{
ErrorLog(ex);
}
finally
{
}
if(word!= null&& doc!= null)
{
word。表newTable;
word.Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
newTable = doc.Tables.Add(wrdRng,1,dgv.Columns.Count-1,ref oMissing,ref oMissing);
newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable.AllowAutoFit = true;
foreach(dgv.Rows [0]中的DataGridViewCell单元格.Cells)
{
newTable.Cell(newTable.Rows.Count,cell.ColumnIndex).Range.Text = dgv.Columns [cell.ColumnIndex]请将.Name;
}
newTable.Rows.Add();
foreach(dgv.Rows中的DataGridViewRow行)
{
foreach(row.Cells中的DataGridViewCell单元格)
{
newTable.Cell(newTable。 Rows.Count,cell.ColumnIndex).Range.Text = cell.Value.ToString();
}
newTable.Rows.Add();
}
}
}
I have a C# application where i want to implement a logic for a programm which will open the word document and go to a certain place in the page and create a Table and put values in that. Can any one tell me how to implement this. I am using Visual studio 2005
Here is code to copy datagridview to a word table:
Reference is Microsoft.Office.Interop.Word C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Word.dll
using word = Microsoft.Office.Interop.Word;
public static void ExportToWord(DataGridView dgv)
{
SendMessage("Opening Word");
word.ApplicationClass word = null;
word.Document doc = null;
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
try
{
word = new word.ApplicationClass();
word.Visible = true;
doc = word.Documents.Add(ref oMissing, ref oMissing,ref oMissing, ref oMissing);
}
catch (Exception ex)
{
ErrorLog(ex);
}
finally
{
}
if (word != null && doc != null)
{
word.Table newTable;
word.Range wrdRng = doc.Bookmarks.get_Item(ref oEndOfDoc).Range;
newTable = doc.Tables.Add(wrdRng, 1, dgv.Columns.Count-1, ref oMissing, ref oMissing);
newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable.AllowAutoFit = true;
foreach (DataGridViewCell cell in dgv.Rows[0].Cells)
{
newTable.Cell(newTable.Rows.Count, cell.ColumnIndex).Range.Text = dgv.Columns[cell.ColumnIndex].Name;
}
newTable.Rows.Add();
foreach (DataGridViewRow row in dgv.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
newTable.Cell(newTable.Rows.Count, cell.ColumnIndex).Range.Text = cell.Value.ToString();
}
newTable.Rows.Add();
}
}
}
这篇关于通过C#.NET在Word中创建动态表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!