winform如何同时导出4个datagridview到word [size=16px]同时导出4个datagridview到word中,要是表格形式的,和datagridview的样式差不多也可以,最好可以自己设计表格样式,在线等,急急急[/size] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你直接拼接成html里面就能用<table>这类的html元素,然后改成doc的扩展名, 别人word打开看不出html的 可以自己画word表格,做成word模板,然后向里面填充数据,我以前做过一个类似的东西。 你会向word里面写数据吗?跟那个类似。 你把你的doc保存为html就可以看到结构是如何的了,然后么你用如何写txt文件的方式把内容放到一个文件里,保存为。doc扩展名 //根据数据的行数插入表格行 object start = 0; object end = 0; Range tableLocation = doc.Range(ref start, ref end); //获得第十行表格并拆分成需要的行和列 Cell cell = doc.Tables[1].Cell(10, 1); object Rownum = null; if (dt3.Rows.Count > 0) { Rownum = dt3.Rows.Count; } else { Rownum = 1; } object Columnnum = 9; int num = 0; cell.Split(ref Rownum, ref Columnnum); if (Convert.ToInt32(Rownum) >= 1) { while (num < dt3.Rows.Count) { //循环插入产品数据 for (int i = 10; i < 10 + Convert.ToInt32(Rownum); i++) { doc.Tables[1].Cell(i, 1).Range.Text = "0" + (num + 1).ToString(); if (dt3.Rows[num][0] != null) { doc.Tables[1].Cell(i, 2).Range.Text = dt3.Rows[num][0].ToString(); } else { doc.Tables[1].Cell(i, 2).Range.Text = ""; } if (dt3.Rows[num][1] != null) { doc.Tables[1].Cell(i, 3).Range.Text = dt3.Rows[num][1].ToString(); } else { doc.Tables[1].Cell(i, 3).Range.Text = ""; } if (dt3.Rows[num][2] != null) { doc.Tables[1].Cell(i, 4).Range.Text = dt3.Rows[num][2].ToString(); } else { doc.Tables[1].Cell(i, 4).Range.Text = ""; } if (dt3.Rows[num][3] != null) { doc.Tables[1].Cell(i, 5).Range.Text = dt3.Rows[num][3].ToString(); } else { doc.Tables[1].Cell(i, 5).Range.Text = ""; } if (dt3.Rows[num][4] != null) { doc.Tables[1].Cell(i, 6).Range.Text = dt3.Rows[num][4].ToString(); } else { doc.Tables[1].Cell(i, 6).Range.Text = ""; } if (dt3.Rows[num][5] != null) { doc.Tables[1].Cell(i, 7).Range.Text = dt3.Rows[num][5].ToString(); } else { doc.Tables[1].Cell(i, 7).Range.Text = ""; } if (dt3.Rows[num][6] != null) { doc.Tables[1].Cell(i, 8).Range.Text = dt3.Rows[num][6].ToString(); } else { doc.Tables[1].Cell(i, 8).Range.Text = ""; } doc.Tables[1].Cell(i, 9).Range.Text = ""; num++; } } } //页面Normal.dot模板保存问题 doc.Application.NormalTemplate.Saved = true; //页眉 appWord.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;//显示页眉 appWord.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;//去掉页眉线 //生成word object filename = null; if (dt2.Rows.Count > 0) { if (dt2.Rows[0][3] != null) { if (isEnd) { filename = HostingEnvironment.ApplicationPhysicalPath + @"upload\word\" + @"" + number + ".doc"; } else { filename = HostingEnvironment.ApplicationPhysicalPath + @"\upload\word\" + @"" + number + ".doc"; } } else { if (isEnd) { filename = HostingEnvironment.ApplicationPhysicalPath + @"upload\word\" + @"送货单.doc"; } else { filename = HostingEnvironment.ApplicationPhysicalPath + @"\upload\word\" + @"送货单.doc"; } } } else { if (isEnd) { filename = HostingEnvironment.ApplicationPhysicalPath + @"upload\word\" + @"送货单.doc"; } else { filename = HostingEnvironment.ApplicationPhysicalPath + @"\upload\word\" + @"送货单.doc"; } } object miss = System.Reflection.Missing.Value; doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); object missingValue = System.Type.Missing; object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges; doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue); appWord.Application.Quit(ref miss, ref miss, ref miss); doc = null; appWord = null; //} //catch(Exception ex) //{ //} } #endregion 不知道你看不看得懂,首先新建一个word,在里面添加一个table,做成模板然后根据自己的需要插入数据,方法里你可以把四个datagridview用datatable作为参数传进去 楼上各位贴出了如何将dataGridView中的数据导出到word中,你同时导出4个dataGridView,只需要把4个datagridView的结果集放到一个数据集里,就可以了。 C# 结构体输出值的问题? 如何设置label行间距 连接excel的时候提示:不能更新,数据库或对象为只读,为什么啊? 线程本地存储区:线程相关的静态字段和数据槽,高手进来解释一下 如何读入excel模板,向这个excel模板中写入DataSet中的数据. 关于编译执行的问题? 高分求助 C#创建工具窗口 【跪求】1篇2w字以上的英文材料 一个和每个人都有关的问题 怎样通过写代码转换图像格式(比如把.bmp转换成.jpg) 时间间隔计算 ftp自动登录到远程,怎样判断文件夹是否有文件,有文件就自动下载并下载文件到本地?
//根据数据的行数插入表格行 object start = 0;
object end = 0;
Range tableLocation = doc.Range(ref start, ref end); //获得第十行表格并拆分成需要的行和列
Cell cell = doc.Tables[1].Cell(10, 1);
object Rownum = null;
if (dt3.Rows.Count > 0)
{
Rownum = dt3.Rows.Count;
}
else
{
Rownum = 1;
} object Columnnum = 9;
int num = 0; cell.Split(ref Rownum, ref Columnnum); if (Convert.ToInt32(Rownum) >= 1)
{
while (num < dt3.Rows.Count)
{ //循环插入产品数据
for (int i = 10; i < 10 + Convert.ToInt32(Rownum); i++)
{
doc.Tables[1].Cell(i, 1).Range.Text = "0" + (num + 1).ToString();
if (dt3.Rows[num][0] != null)
{
doc.Tables[1].Cell(i, 2).Range.Text = dt3.Rows[num][0].ToString();
}
else
{
doc.Tables[1].Cell(i, 2).Range.Text = "";
}
if (dt3.Rows[num][1] != null)
{
doc.Tables[1].Cell(i, 3).Range.Text = dt3.Rows[num][1].ToString();
}
else
{
doc.Tables[1].Cell(i, 3).Range.Text = "";
}
if (dt3.Rows[num][2] != null)
{
doc.Tables[1].Cell(i, 4).Range.Text = dt3.Rows[num][2].ToString();
}
else
{
doc.Tables[1].Cell(i, 4).Range.Text = "";
}
if (dt3.Rows[num][3] != null)
{
doc.Tables[1].Cell(i, 5).Range.Text = dt3.Rows[num][3].ToString();
}
else
{
doc.Tables[1].Cell(i, 5).Range.Text = "";
}
if (dt3.Rows[num][4] != null)
{
doc.Tables[1].Cell(i, 6).Range.Text = dt3.Rows[num][4].ToString();
}
else
{
doc.Tables[1].Cell(i, 6).Range.Text = "";
}
if (dt3.Rows[num][5] != null)
{
doc.Tables[1].Cell(i, 7).Range.Text = dt3.Rows[num][5].ToString();
}
else
{
doc.Tables[1].Cell(i, 7).Range.Text = "";
}
if (dt3.Rows[num][6] != null)
{
doc.Tables[1].Cell(i, 8).Range.Text = dt3.Rows[num][6].ToString();
}
else
{
doc.Tables[1].Cell(i, 8).Range.Text = "";
}
doc.Tables[1].Cell(i, 9).Range.Text = "";
num++;
}
}
}
//页面Normal.dot模板保存问题
doc.Application.NormalTemplate.Saved = true; //页眉
appWord.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;//显示页眉
appWord.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;//去掉页眉线
//生成word
object filename = null; if (dt2.Rows.Count > 0)
{
if (dt2.Rows[0][3] != null)
{
if (isEnd)
{
filename = HostingEnvironment.ApplicationPhysicalPath + @"upload\word\" + @"" + number + ".doc";
}
else
{
filename = HostingEnvironment.ApplicationPhysicalPath + @"\upload\word\" + @"" + number + ".doc";
}
}
else
{
if (isEnd)
{
filename = HostingEnvironment.ApplicationPhysicalPath + @"upload\word\" + @"送货单.doc";
}
else
{
filename = HostingEnvironment.ApplicationPhysicalPath + @"\upload\word\" + @"送货单.doc";
}
}
}
else
{
if (isEnd)
{
filename = HostingEnvironment.ApplicationPhysicalPath + @"upload\word\" + @"送货单.doc";
}
else
{
filename = HostingEnvironment.ApplicationPhysicalPath + @"\upload\word\" + @"送货单.doc";
}
}
object miss = System.Reflection.Missing.Value;
doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = System.Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null; //}
//catch(Exception ex)
//{ //}
}
#endregion
然后根据自己的需要插入数据,方法里你可以把四个datagridview用datatable作为参数传进去
楼上各位贴出了如何将dataGridView中的数据导出到word中,你同时导出4个dataGridView,只需要把4个datagridView的结果集放到一个数据集里,就可以了。