实例介绍
第四步,运行程序生成文档,并查看生成的文档
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MSWord = Microsoft.Office.Interop.Word; namespace ExportWordTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } MSWord.Application wordApp = null; MSWord.Document wordDoc = null; object nothing = System.Reflection.Missing.Value; private void button1_Click(object sender, EventArgs e) { doExport(); } private void doExport() { try { wordApp = new MSWord.ApplicationClass(); wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing); doPrintTable("shili.jpg"); doPrintTable("shili1.jpg"); //doPrintTable(); //wordApp.Visible = true; object filename = System.IO.Directory.GetCurrentDirectory() @"\测试.docx"; wordDoc.SaveAs2(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, ref nothing); wordDoc.Close(ref nothing, ref nothing, ref nothing); wordApp.Quit(ref nothing, ref nothing, ref nothing); MessageBox.Show("导出Word成功"); } catch (Exception ex) { MessageBox.Show("导出Word错误" ex.ToString()); } } private void doPrintTable(string t) { try { wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; //wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//设置字体居中 wordApp.Selection.Font.Size = 20;//设置字体大小 wordApp.Selection.Font.Bold = 2;//设置字体为粗体 wordApp.Selection.TypeText("设备(设施)标识牌"); wordDoc.Paragraphs.Add(ref nothing); wordDoc.Paragraphs.Last.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight; wordApp.Selection.Font.Size = 15; wordApp.Selection.Font.Bold = 0; wordApp.Selection.TypeText("南通帝人"); wordApp.Selection.TypeParagraph();//换行 wordApp.Selection.Font.Size = 12; wordApp.Selection.Font.Bold = 0; //8表示8行,3表示3列 MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range, 8, 3, ref nothing, ref nothing); table.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;//设置表格边框样式 table.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle; table.Columns[1].Width = 80f; table.Columns[2].Width = 200f; table.Columns[3].Width = 120f; table.Rows.Alignment = MSWord.WdRowAlignment.wdAlignRowCenter;//表格整体居中 wordApp.Selection.Cells.VerticalAlignment = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalCenter; //第一行 table.Cell(1, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(1, 1).Range.Text = "设备名称"; table.Cell(1, 2).Merge(table.Cell(1, 3));//合并单元格 table.Cell(1, 2).Range.Text = "ABC干粉灭火器SB03324"; table.Cell(1, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft; //第二行 table.Cell(2, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(2, 1).Range.Text = "使用位置"; table.Cell(2, 2).Merge(table.Cell(2, 3)); table.Cell(2, 2).Range.Text = "帝人宿舍区六楼楼梯口"; table.Cell(2, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft; //第三行 table.Cell(3, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(3, 1).Range.Text = "型号规格"; table.Cell(3, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft; table.Cell(3, 2).Range.Text = ""; table.Cell(3, 3).Merge(table.Cell(8, 3)); table.Cell(3, 3).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(3, 3).Select();//纵向合并单元格 object LinkOfFile = false; object SaveDocument = true; object range = wordApp.Selection.Range; string pictureFileName = System.IO.Directory.GetCurrentDirectory() @"\" t; table.Cell(3, 3).Range.InlineShapes.AddPicture(pictureFileName, ref LinkOfFile, ref SaveDocument, ref range); table.Cell(3, 3).Range.InlineShapes[1].Width = 100f; table.Cell(3, 3).Range.InlineShapes[1].Height = 100f; //MSWord.Shape s = table.Cell(3, 3).Range.InlineShapes[1].ConvertToShape(); //s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare;//四周环绕 //第四行 table.Cell(4, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(4, 1).Range.Text = "启用日期"; table.Cell(4, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft; table.Cell(4, 2).Range.Text = ""; //第四行 table.Cell(5, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(5, 1).Range.Text = "物理编码"; table.Cell(5, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft; table.Cell(5, 2).Range.Text = ""; //第四行 table.Cell(6, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(6, 1).Range.Text = "产权单位"; table.Cell(6, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft; table.Cell(6, 2).Range.Text = "帝人"; //第四行 table.Cell(7, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(7, 1).Range.Text = "维保单位"; table.Cell(7, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft; table.Cell(7, 2).Range.Text = "江苏万友消防工程有限公司"; //第四行 table.Cell(8, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(8, 1).Range.Text = "维修电话"; table.Cell(8, 2).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft; table.Cell(8, 2).Range.Text = "400-0820-119"; wordApp.Selection.EndKey(MSWord.WdUnits.wdStory); wordApp.Selection.TypeParagraph();//换行 } catch (Exception ex) { MessageBox.Show("在word文档中生成表格异常:" ex.ToString()); } } } }
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)