请高手帮忙解决下面的VB.NET问题!!

egg_fairy 2010-06-24 09:18:24
用VB.NET程序:如何使用word显示datagrid控件中的数据内容???

请高手详细点解,赋上实现代码哦。积分属于认真而细心的高手!
...全文
174 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlh0053 2010-06-28
  • 打赏
  • 举报
回复
private void ExportWord_Click(object sender, System.EventArgs e)
{
try
{
string savepath = "";
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
savepath = folderBrowserDialog1.SelectedPath;
}

Object Nothing=System.Reflection.Missing.Value;
object filename = savepath + "/" + StoreNum.Text + "库存报表";
Word.Application WordApp=new Word.ApplicationClass();
Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);

WordDoc.Paragraphs.First.Range.Text = "库存报表 [共有:" + storedt.Rows.Count.ToString() + "本书]";
WordDoc.Paragraphs.First.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,storedt.Rows.Count+1,6,ref Nothing,ref Nothing);
table.Cell(1,1).Range.Text="ISBN号";
table.Cell(1,2).Range.Text="书名";
table.Cell(1,3).Range.Text="总库存";
table.Cell(1,4).Range.Text="借书库存";
table.Cell(1,5).Range.Text="可借数量";
table.Cell(1,6).Range.Text="已借数量";

for ( int i=0;i<storedt.Rows.Count;i++ )
{
table.Cell(i+2,1).Range.Text = storedt.Rows[i]["Book_ISBN"].ToString();
table.Cell(i+2,2).Range.Text = storedt.Rows[i]["Book_Name"].ToString();
table.Cell(i+2,3).Range.Text = storedt.Rows[i]["Store_Num"].ToString();
table.Cell(i+2,4).Range.Text = storedt.Rows[i]["CanBorrow_Num"].ToString();
table.Cell(i+2,5).Range.Text = storedt.Rows[i]["InShop_Num"].ToString();
table.Cell(i+2,6).Range.Text = storedt.Rows[i]["OutShop_Num"].ToString();
}

WordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
parent.statusBar1.Panels[0].Text = "操作提示:库存报表导出成功";
}
catch
{
parent.statusBar1.Panels[0].Text = "操作提示:库存报表导出失败";
}
}

注意要引用Interop.Word.dll,并且在机器上安装Word2002,否则导出将失败
egg_fairy 2010-06-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 machong8183 的回复:]
就你这种态度,估计没几个人想教你,你以为这个分是RMB?
就是给100块RMB看谁愿意教你这样的
[/Quote]

不清楚你的水平有多少,但很明显看得出来你就不是个教导者!积分当然不是钱!你用钱还买不到!积分换来的也许只是求学与教学的过程!就你【 machong8183 】 这小样,帮没帮人就先鄙视人,你以为自己很牛呀!无知最是如此!除了会说些空寂无益于解决问题的话,你还能做什么!也许你的有限水平就只会烂在自己肚子里!
egg_fairy 2010-06-28
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 xlh0053 的回复:]
private void ExportWord_Click(object sender, System.EventArgs e)
{
try
{
string savepath = "";
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
if (f……
[/Quote]

非常感谢您!
KevinChen026 2010-06-27
  • 打赏
  • 举报
回复
Firefly
egg_fairy 2010-06-26
  • 打赏
  • 举报
回复
来个高手教教我:如何实现VB.NET中通过button1按钮控件点击后,将datagrid控件中的数据集添加到指定的一个名为“资料”的word文件中吗?该word文件已经存在数据,就是实现将datagrid中的数据集添加进该word文件中。
水猿兵团五哥 2010-06-26
  • 打赏
  • 举报
回复
就你这种态度,估计没几个人想教你,你以为这个分是RMB?
就是给100块RMB看谁愿意教你这样的
threenewbee 2010-06-25
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 egg_fairy 的回复:]
引用 9 楼 caozhy 的回复:
引用 7 楼 egg_fairy 的回复:
引用 5 楼 wuyazhe 的回复:
VB.NET code

Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc")
Response.Charset = ""
R……
[/Quote]
我有点搞不明白了,你到底是asp.net还是winforms?
egg_fairy 2010-06-25
  • 打赏
  • 举报
回复
是winforms呀
egg_fairy 2010-06-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 caozhy 的回复:]
引用 7 楼 egg_fairy 的回复:
引用 5 楼 wuyazhe 的回复:
VB.NET code

Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc")
Response.Charset = ""
Response.Cache.SetCac……
[/Quote]

还是初学有很多不懂,你说“导出文档和按钮什么没有关系”,可是我希望实现的功能为:单击按钮来促发将数据库表内容输出到word文档中哦,请问能否更深层详细的说下如何实现呢?愿听指教呀。
threenewbee 2010-06-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 egg_fairy 的回复:]
引用 5 楼 wuyazhe 的回复:
VB.NET code

Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheabil……
[/Quote]

你可以从codeproject下载完整的源代码。
首先把概念搞清楚,导出文档和按钮什么没有关系,和水果苹果保质期也没有关系。
threenewbee 2010-06-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 egg_fairy 的回复:]
引用 3 楼 caozhy 的回复:
导出为word文档。


C# code
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(H……
[/Quote]

VB和C#是同一套语言的两种写法,两者的语句几乎一一对应,如同繁体中文和简体中文的关系。
作为一个合格的.NET程序员,应该熟悉C#的语法。很多例子都是C#写的。
egg_fairy 2010-06-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyazhe 的回复:]
VB.NET code

Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Respons……
[/Quote]

您说“只是使用Convert C# to VB.net网站 把caozhy的代码转换成vb.net的”,我试了转换失败,弹出个警告然后网页错误了!

我想再描述下我需要的结果哦:我在VB.NET窗体中设置了datagrid控件和button按钮,已经用程序将名为:商品.mdb数据库进行连接,在datagrid中显示出商品.mdb数据库中的“水果”表内容,当然该表内容有很多项,如:苹果、价格、保质期等等,我希望单击按钮button后,能将datagrid中显示的"水果"表内容通过word文本输出。所以需要高手们的详细指点!!
古今多少事 2010-06-24
  • 打赏
  • 举报
回复
http://dotnet.51aspx.com/article/6afbf00b-459d-4642-ad14-8a4765ffafcc/read.aspx
兔子-顾问 2010-06-24
  • 打赏
  • 举报
回复

Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.word"

Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)

myDataGrid.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()

对不对就不知道了。你试试把。我也不懂web。
只是使用Convert C# to VB.net网站 把caozhy的代码转换成vb.net的。
egg_fairy 2010-06-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 caozhy 的回复:]
导出为word文档。


C# code
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCa……
[/Quote]

您怎么说个C# code?我需要是VB.NET呀!在VB.NET能用吗?随便粘来的资料?
threenewbee 2010-06-24
  • 打赏
  • 举报
回复
导出为word文档。

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

myDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();


参考:http://www.codeproject.com/KB/webforms/DAtaGridExportToExcel.aspx
egg_fairy 2010-06-24
  • 打赏
  • 举报
回复
最好就是生成导出一个word文件,前者也行。能两个说说更好哦
chinayeren 2010-06-24
  • 打赏
  • 举报
回复
office中的word? 是让程序打开一个word然后将数据复制过去?还是生成导出一个word文件?

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧