winform导出word文档

chenrong980 2012-11-30 01:10:16

winform界面上的内容怎么导出word啊!word格式可以根据代码的变动更改
我在网上找了一下代码,可是都不能用,总有点错误(向word文件中写入数据出错:被呼叫方拒绝被呼叫)
不知道怎么弄啊,请各位大侠指点一二。有源码最好了
...全文
389 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenrong980 2013-01-09
  • 打赏
  • 举报
回复
谢谢各位哦!已经搞定了 string filePath = "D:\\管理文档"; if (!Directory.Exists(filePath))//若文件夹不存在则新建文件夹 { Directory.CreateDirectory(filePath); //新建文件夹 } object path = filePath + "\\" + deviceName + "内部电子工单" + Times;//导出地址 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); //建立表格 int rows=6;//建立表格的行数 int colunms=4;//建立表格的列数 //标题 wordApp.Application.Selection.TypeText(deviceName+"内部电子工单");//标题名称 wordApp.Application.Selection.TypeParagraph(); Table table = document.Tables.Add(document.Paragraphs.Last.Range, rows,colunms, ref none, ref none); table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphThaiJustify;//设置对其方式 table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式 table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置表格边框样式 try { table.Cell(1, 1).Range.Text = "发单员"; table.Cell(1, 1).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(1, 2).Range.Text = txtDeviceName.Text; table.Cell(1, 3).Range.Text = "工单录入时间"; table.Cell(1, 3).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(1, 4).Range.Text = txtConstrucTwarlord.Text; table.Cell(2, 1).Range.Text = "放松部门"; table.Cell(2, 1).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(2, 2).Range.Text = ""; table.Cell(2, 3).Range.Text = "工单级别"; table.Cell(2, 3).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(2, 4).Range.Text = ""; table.Cell(3, 1).Range.Text = "联系方式"; table.Cell(3, 1).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(3, 2).Range.Text = ""; table.Cell(3, 3).Range.Text = "完成时限"; table.Cell(3, 3).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(3, 4).Range.Text = "" table.Cell(4, 1).Range.Text = "工单主题"; table.Cell(4, 1).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(4, 1).Merge(table.Cell(4, 4));//合并列 table.Cell(4, 2).Range.Text = ""; table.Cell(5, 1).Range.Text = "工单内容"; table.Cell(5, 1).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(5, 1).Merge(table.Cell(5, 4));//合并列 table.Cell(5, 2).Range.Text = ""; table.Cell(6, 1).Range.Text = "附件模板"; table.Cell(6, 1).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(6, 1).Merge(table.Cell(6, 4));//合并列 table.Cell(6, 2).Range.Text = ""; table.Cell(7, 1).Range.Text = "回复模板"; table.Cell(7, 1).Range.Font.Bold = 1;//1为粗体0为常规 table.Cell(7, 1).Merge(table.Cell(7, 4));//合并列 table.Cell(7, 2).Range.Text = ""; 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); document.Close(ref none, ref none, ref none); progreesBar.Value = 100; MessageBox.Show("数据已经成功导出到:" + filePath, "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "温馨提示", MessageBoxButtons.OK); } finally { wordApp.Quit(ref none, ref none, ref none); }
chenrong980 2013-01-09
  • 打赏
  • 举报
回复
引用 10 楼 vbmanager 的回复:
最简单的实现方法,就是使用水晶报表的导出功能,可能首次导出会有点性能问题,不过还是可以接受的。
用水晶报表需要填写数据时是一个界面,查看和导出又是一个界面,太麻烦了,不过我已经搞定了
vbmanager 2013-01-06
  • 打赏
  • 举报
回复
最简单的实现方法,就是使用水晶报表的导出功能,可能首次导出会有点性能问题,不过还是可以接受的。
枫c_2012 2013-01-06
  • 打赏
  • 举报
回复
这个学习的时候 用过、、现在、、忘了、、
波西米亚人生 2013-01-05
  • 打赏
  • 举报
回复
去百度 吧 “c# word书签” ..
chenrong980 2013-01-05
  • 打赏
  • 举报
回复
引用 5 楼 BohemianLife 的回复:
建一个word的模板,在关键的程序写入点添加书签、或者域(根据需求),然后将对应的字符写入对应的标签(或者域)
没用过模板,怎么创建word模板啊,创建好了,又怎么应用呢?
波西米亚人生 2013-01-01
  • 打赏
  • 举报
回复
建一个word的模板,在关键的程序写入点添加书签、或者域(根据需求),然后将对应的字符写入对应的标签(或者域)
mailofzw 2012-12-31
  • 打赏
  • 举报
回复
我也在学这么,关注
jglie 2012-12-26
  • 打赏
  • 举报
回复
被呼叫方拒绝被呼叫,我之前也碰到过。因为是你写入到word中的数据格式估计有问题,word默认启动了一部分的数据校验。如单词拼写校验、语法校验等。 你可以去掉相关的校验试试。
wuchangjiuyou 2012-12-09
  • 打赏
  • 举报
回复
同求 123951494@qq.com
飒飒望月 2012-11-30
  • 打赏
  • 举报
回复
有的也发我一份,我现在也在做这个。 27278282@qq.com

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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