Windows应用程序中的DataGridView
问题描述:
如何使用边框线,列标题和正确的数据格式将datagridview的内容导出到Microsoft Word.期待您的正面答复
How can i export the content of a datagridview to Microsoft Word with the Borderlines, column headers, correct data format. Looking forward to your favorable reply
答
也许此链接将DataGrid导出到Excel,Word和文本文件 [ ^ ]将为您提供帮助!
对于Winforms,到目前为止,我所看到的最好的事情是ComponentOne Studio for Winforms的True DB Grid Exporting示例.
我现在刚刚尝试过,它甚至可以导出图像和背景色.
http://www.componentone.com/SuperProducts/StudioWinForms/ [
Maybe this link Exporting DataGrid to Excel, Word and Text Files[^] will help you!
For winforms, the best thing I have seen to date is the True DB Grid Exporting example from ComponentOne Studio for Winforms.
I just tried it out now, and it even exports the images and background colours.
http://www.componentone.com/SuperProducts/StudioWinForms/[^]
Downfall is cost. As it is a 3rd Party Control.
Standard version is around
895左右!
另请参见 http://www.eggheadcafe.com/community/aspnet/2/10225945/export-datagridview-to-word.aspx [ http://www.completit.com/Portfolio/DGVE/Features.aspx [ ^ ]
895!!
Also see http://www.eggheadcafe.com/community/aspnet/2/10225945/export-datagridview-to-word.aspx[^]
Another 3rd party control is available that does to excel and pdf
http://www.completit.com/Portfolio/DGVE/Features.aspx[^]
您好,
将代码粘贴在按钮中,您可以满足要求.不要忘了标记为答案.编码愉快.
Hi,
Paste code in button click you get your requirement.Don''t forget mark as answer.Happy coding.
Object oMissing = System.Reflection.Missing.Value;
//OBJECTS OF FALSE AND TRUE
Object oTrue = true;
Object oFalse = false;
//CREATING OBJECTS OF WORD AND DOCUMENT
Microsoft.Office.Interop .Word.Application oWord = new Microsoft.Office.Interop .Word.Application();
Microsoft.Office.Interop .Word.Document oWordDoc = new Microsoft.Office.Interop .Word.Document();
//MAKING THE APPLICATION VISIBLE
oWord.Visible = true;
//ADDING A NEW DOCUMENT TO THE APPLICATION
oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//SETTING THE RANGE ON THE BOOKMARK
Object oBookMarkName = "My_Inserted_Bookmark_On_Template";
oWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop .Word.WdParagraphAlignment.wdAlignParagraphLeft;
oWord.Selection.Font.Bold = (int)Microsoft.Office.Interop .Word.WdConstants.wdToggle;
oWord.Selection.TypeText("Set Code:" + " "+ "Set test:");
oWord.Selection.TypeParagraph();
oWord.Selection.TypeText("Set Description" + " "+ " test Description ");
oWord.Selection.TypeParagraph();
oWord.Selection.TypeParagraph();
Object start = Type.Missing;
Object end = Type.Missing;
Microsoft.Office.Interop.Word.Range rng = oWordDoc.Range(ref start, ref end);
//ADD TABLE
Microsoft.Office.Interop.Word.Table tbl = oWordDoc.Tables.Add(rng, dataGridView1.Rows.Count , dataGridView1 .Columns.Count , ref oMissing, ref oMissing);
//END ADD TABLE
Object defaultTableBehavior = Type.Missing;
Object autoFitBehavior = Type.Missing;
object missing = System.Type.Missing;
Microsoft.Office.Interop.Word.Row newRow = oWordDoc.Tables[1].Rows.Add(ref missing);
newRow.Range.Font.Bold = 0;
newRow.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i == dataGridView1.Rows.Count - 1)
MessageBox.Show("Successfully Exported");
else
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
tbl.Cell(i + 1, j + 1).Range.Text = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
}
oWord.Selection.TypeParagraph();