将数据网格中的数据保存到 Microsoft Word 文档中
Posted
技术标签:
【中文标题】将数据网格中的数据保存到 Microsoft Word 文档中【英文标题】:Save data from datagrid into Microsoft Word document 【发布时间】:2017-04-03 11:41:06 【问题描述】:我有 5 张桌子Tables
当我按下按钮时,来自 dataGrid 的数据必须保存为 Microsoft doc。我试试这段代码
public void Export_Data_To_Word(DataGridView DGV, string filename)
if (DGV.Rows.Count != 0)
int RowCount = DGV.Rows.Count;
int ColumnCount = DGV.Columns.Count;
Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1];
//add rows
int r = 0;
for (int c = 0; c <= ColumnCount - 1; c++)
for (r = 0; r <= RowCount - 1; r++)
DataArray[r, c] = DGV.Rows[r].Cells[c].Value;
//end row loop
//end column loop
Word.Document oDoc = new Word.Document();
oDoc.Application.Visible = true;
//page orintation
oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
dynamic oRange = oDoc.Content.Application.Selection.Range;
string oTemp = "";
for (r = 0; r <= RowCount - 1; r++)
for (int c = 0; c <= ColumnCount - 1; c++)
oTemp = oTemp + DataArray[r, c] + "\t";
//table format
oRange.Text = oTemp;
object Separator = Word.WdTableFieldSeparator.wdSeparateByTabs;
object ApplyBorders = true;
object AutoFit = true;
object AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
oRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount,
Type.Missing, Type.Missing, ref ApplyBorders,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing);
oRange.Select();
oDoc.Application.Selection.Tables[1].Select();
oDoc.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
oDoc.Application.Selection.Tables[1].Rows.Alignment = 0;
oDoc.Application.Selection.Tables[1].Rows[1].Select();
oDoc.Application.Selection.InsertRowsAbove(1);
oDoc.Application.Selection.Tables[1].Rows[1].Select();
//header row style
oDoc.Application.Selection.Tables[1].Rows[1].Range.Bold = 1;
oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Name = "Tahoma";
oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 14;
//add header row manually
for (int c = 0; c <= ColumnCount - 1; c++)
oDoc.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = DGV.Columns[c].HeaderText;
//table style
oDoc.Application.Selection.Tables[1].set_Style("Grid Table 4 - Accent 5");
oDoc.Application.Selection.Tables[1].Rows[1].Select();
oDoc.Application.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
//header text
foreach (Word.Section section in oDoc.Application.ActiveDocument.Sections)
Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
headerRange.Text = "your header text";
headerRange.Font.Size = 16;
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
//save the file
oDoc.SaveAs2(filename);
private void button1_Click(object sender, EventArgs e)
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Word Documents (*.docx)|*.docx";
sfd.FileName = "your_bisness.docx";
if (sfd.ShowDialog() == DialogResult.OK)
Export_Data_To_Word(dataGridView1, sfd.FileName);
当我按下按钮保存时,我得到了这个Result
程序运行时文档正在运行。我只需要保存文档(不要打开它)。如何将所有 5 个表格(dataGrid1、2、3、4、5)保存在一个文档中,带有表格边框,在没有边框的图像表格上。
请帮忙
【问题讨论】:
【参考方案1】:这需要一些努力,但最好将代码结构更好一些。问题是:Export_Data_To_Word
,导出完成的地方,只能用dataGrid1
调用。完成后,文件被保存。也许您可以尝试创建一个打开文档的方法,另一个添加数据网格的方法,以及一个保存文档的最终方法。
类似这样的:
一种准备出口文件的方法:
public Word.Document GetPreparedDocument()
Word.Document oDoc = new Word.Document();
oDoc.Application.Visible = true;
//page orintation
oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
//additional page setup
return oDoc;
一种插入表格的方法,注意它不使用文件名而是使用文档:
public void Export_Data_To_Word(DataGridView DGV, Word.Document oDoc)
//alter document here
//I don't have time to write this for you,
//Keep in mind that you'll need to keep track of the table indices.
保存程序:
public void Save(Word.Document oDoc, string fileName)
//extra finalizrs
oDoc.SaveAs2(fileName);
还有一种称呼它们的方法:
private void button1_Click(object sender, EventArgs e)
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Word Documents (*.docx)|*.docx";
sfd.FileName = "your_bisness.docx";
if (sfd.ShowDialog() == DialogResult.OK)
Word.Document oDoc = GetPreparedDocument();
Export_Data_To_Word(dataGridView1, oDoc );
Export_Data_To_Word(dataGridView2, oDoc );
Export_Data_To_Word(dataGridView3, oDoc );
Export_Data_To_Word(dataGridView4, oDoc );
Export_Data_To_Word(dataGridView5, oDoc );
Save(oDoc,sfd.FileName);
我希望这会有所帮助。
作为替代方案,快速修复脏东西
您也可以坚持使用您的原始代码,但更改 Export_Data_To_Word
以使其能够处理数据网格转换。
//note don't use DGV anymore, dataGridView1 till dataGridView5 are available
public void Export_Data_To_Word(Word.Document oDoc)
//your code (still sort on time)
//prepare doc like you did
//The difference is this:
//insert table for dataGridView1
//insert table for dataGridView2
//insert table for dataGridView3
//insert table for dataGridView4
//insert table for dataGridView5
//For every insert you'll have to do the selection,
//table conversion etc. so it's going to be a lot of code
//therefore it's best to try to study a bit about member
//variables first
//save file like you did
【讨论】:
对不起,我是编程菜鸟。如何在没有另存为的情况下将数据添加到 Export_Data_To_Word 中的 oDoc??? SaveAs 稍后执行。在第一步中,您准备文档,然后在Export_Data_To_Word
方法中向其中添加数据。这一切都发生在记忆中。一旦你告诉它SaveAs2
,它将被保存到磁盘。基本和以前一样,但是拆分成多个方法。
@Decard:也许您应该阅读有关变量、引用、类对象、方法和返回值的信息。我认为这将有助于您更好地理解。我会发布一个替代方案。以上是关于将数据网格中的数据保存到 Microsoft Word 文档中的主要内容,如果未能解决你的问题,请参考以下文章
在将编辑内容保存到数据库之前验证 Oracle Apex 可编辑交互式网格