winform datatable 如何导出到word中,求代码 http://www.cnblogs.com/lingyuan/archive/2011/01/24/1943440.htmlhttp://blog.csdn.net/tiangaojie123abc/article/details/7900594 C# Winform datatable 导出Word 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public void ExportDataGridViewToWord(DataTable srcDgv, SaveFileDialog sfile) { if (srcDgv.Rows.Count == 0) { MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { sfile.AddExtension = true; sfile.DefaultExt = ".doc"; sfile.Filter = "(*.doc)|*.doc"; if (sfile.ShowDialog() == DialogResult.OK) { object path = sfile.FileName; Object none = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none); //建立表格 Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count, srcDgv.Columns.Count, ref none, ref none); try { for (int i = 0; i < srcDgv.Columns.Count; i++)//输出标题 { table.Cell(1, i + 1).Range.InsertAfter(srcDgv.Columns[i].ColumnName); } //输出控件中的记录 for (int i = 0; i < srcDgv.Rows.Count; i++) { for (int j = 0; j < srcDgv.Columns.Count; j++) { table.Cell(i + 2, j + 1).Range.InsertAfter(srcDgv.Rows[i][j].ToString()); } } table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none); MessageBox.Show("数据已经成功导出到:" + sfile.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception e) { MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK); } } } },不知道为什么最后输出的表格中最后一行和倒数第二行合在一起了 DataTable 到 Word文档控件,http://www.e-iceblue.com/Introduce/free-dataexport-component.html 请问我在类库中加入一个XML文件,如何能读取到 如何取出dataGridView的第n行的某个字段值? C#关于数据库的问题 About ComboBox 文本编辑器 插入图片 报服务器故障(有图) 十万火急!!在线等! 高手请进啊小弟感激不尽~~ using 问题。 DataGrid中合并列显示问题。 remoting调用出错的问题!!请入内,在线等!! 求解:OutlookBar这个控件 VS 2010 出错关闭 C#双线程处理问题,跪求~!
{
if (srcDgv.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
sfile.AddExtension = true;
sfile.DefaultExt = ".doc";
sfile.Filter = "(*.doc)|*.doc";
if (sfile.ShowDialog() == DialogResult.OK)
{
object path = sfile.FileName;
Object none = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
//建立表格
Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count, srcDgv.Columns.Count, ref none, ref none);
try
{
for (int i = 0; i < srcDgv.Columns.Count; i++)//输出标题
{ table.Cell(1, i + 1).Range.InsertAfter(srcDgv.Columns[i].ColumnName); } //输出控件中的记录 for (int i = 0; i < srcDgv.Rows.Count; i++)
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{
table.Cell(i + 2, j + 1).Range.InsertAfter(srcDgv.Rows[i][j].ToString());
} }
table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
MessageBox.Show("数据已经成功导出到:" + sfile.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK);
}
}
} },不知道为什么最后输出的表格中最后一行和倒数第二行合在一起了