| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2138 人关注过本帖
标题:C#操作office时报异常。代码如下,恳请大神指教
只看楼主 加入收藏
米粒大小3
Rank: 1
等 级:新手上路
帖 子:40
专家分:7
注 册:2015-10-25
结帖率:66.67%
收藏
 问题点数:0 回复次数:1 
C#操作office时报异常。代码如下,恳请大神指教
报异常,说什么未处理comexception,数值超范围!!!
恳请大神指教!!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MSWord = Microsoft.Office.Interop.Word;
using
using System.Reflection;

namespace Addgeshihuawenben
{
    class Program
    {
        static void Main(string[] args)
        {
            object path;
            string strContent;
            MSWord.Application wordApp;
            MSWord.Document wordDoc;
            path = @"C:\Users\Administrator\Desktop\MyWord2.doc";
            wordApp = new MSWord.ApplicationClass();
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }
            Object Nothing = Missing.Value;
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            strContent = "普通文本普通文本普通文本普通文本普通文本\n";
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            strContent = "黑体文本黑体文本黑体文本黑体文本黑体文本\n";
            wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            strContent = "加粗文本加粗文本加粗文本加粗文本加粗文本\n";
            wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            wordDoc.Paragraphs.Last.Range.Font.Size = 15;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            strContent = "斜体文本斜体文本斜体文本斜体文本斜体文本\n";
            wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
            strContent = "蓝色文本蓝色文本蓝色文本蓝色文本蓝色文本\n";
            wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            strContent = "下划线文本下划线文本下划线文本下划线文本下划线文本\n";
            wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            strContent = "红色下划线文本红色下划线文本红色下划线文本红色下划线文本红色下划线文本\n";
            wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;
            wordDoc.SaveAs(ref path, ref format, 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);
            Console.WriteLine(path + "创建完毕!");
            Console.ReadKey();
        }
    }
}
搜索更多相关主题的帖子: office 
2016-09-23 11:44
bluesky2992
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2019-1-10
收藏
得分:0 
使用 Spire.Office for .NET,您可以创建大量的用于处理办公文档的应用程序。对文档的操作包括打开,创建,修改,转换,打印,浏览 Word、Excel、PowerPoint® 和 PDF 文档,以及将数据从数据源导出为常用的文档格式,例如:Word,Excel,RTF,Access,PowerPoint,PDF,XPS,HTML,XML,Text,CSV,DBF 和剪贴版等格式。例如:

//创建一个Document实例
Document doc = new Document();
//添加一个section
Section s = doc.AddSection();
//添加三个段落并写入文字
Paragraph para1 = s.AddParagraph();
para1.AppendText("欢迎使用Spire.Doc");            
Paragraph para2 = s.AddParagraph();
para2.AppendText("Spire.Doc for .NET是一款专门对Word文档进行操作的.NET类库。"+
   "这款控件的主要功能在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印Microsoft"+
    "Word文档。作为一款独立的Word .NET控件,Spire.Doc for .NET的运行系统(服务"+
    "器端或客户端)均无需安装 Microsoft Word,但是它却可以将 Microsoft Word 文档的操"+
    "作功能集成到任何开发人员的 .NET 应用程序中。");            
Paragraph para3 = s.AddParagraph();
para3.AppendText("Spire.Doc for .NET 能执行多种Microsoft Word文档处理任务的.NET "+
    "API。支持 Word97-2003,Word2007,Word2010 以及 Word2013。能在 Word 97/2003/2007/2010/2013"+
    "和XML、RTF、TXT、XPS、EPUB、EMF、HTML等格式文件之间进行双向转换,还能将Word文件高"+
    "质量地转换为PDF和SVG文件格式。");
//将第一段作为标题,设置标题格式
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.Name = "titleStyle";
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.TextColor = Color.Purple;
style1.CharacterFormat.FontName = "宋体";
style1.CharacterFormat.FontSize = 12f;
doc.Styles.Add(style1);
para1.ApplyStyle("titleStyle");
//设置其余两个段落的格式
ParagraphStyle style2 = new ParagraphStyle(doc);
style2.Name = "paraStyle";
style2.CharacterFormat.FontName = "宋体";
style2.CharacterFormat.FontSize = 11f;
doc.Styles.Add(style2);
para2.ApplyStyle("paraStyle");
para3.ApplyStyle("paraStyle");
//设置三个段落的对齐方式
para1.Format.HorizontalAlignment = HorizontalAlignment.Center;
para2.Format.HorizontalAlignment = HorizontalAlignment.Justify;
para3.Format.HorizontalAlignment = HorizontalAlignment.Justify;
//设置段首缩进
para2.Format.FirstLineIndent = 30f;
para3.Format.FirstLineIndent= 30f;
//设置段落之间的间距
para1.Format.AfterSpacing = 15f;
para2.Format.AfterSpacing = 10f;
//保存文档
doc.SaveToFile("First_Word_Document.docx", FileFormat.Docx2013);
2019-01-10 21:21
快速回复:C#操作office时报异常。代码如下,恳请大神指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015285 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved