using System.Collections.Generic; |
using System.Web.UI.WebControls; |
using Microsoft.Office.Interop.Word; |
public partial class Word相关_生成Word : System.Web.UI.Page |
protected void Page_Load( object sender, EventArgs e) |
public string CreateWordFile( string CheckedInfo) |
Object Nothing = System.Reflection.Missing.Value; |
Directory.CreateDirectory( "F:/项目代码/公用架构/PublicProjectSolution/PublicProjectWeb/Word相关" ); |
string name = "CNSI_" + CheckedInfo + ".doc" ; |
object filename = "F://项目代码//公用架构//PublicProjectSolution//PublicProjectWeb//Word相关//" + name; //文件保存路径 |
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); |
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add( ref Nothing, ref Nothing, ref Nothing, ref Nothing); |
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; |
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; |
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "[页眉内容]" ); |
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; |
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; |
WordApp.Selection.ParagraphFormat.LineSpacing = 15f; |
object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine; |
WordApp.Selection.MoveDown( ref WdLine, ref count, ref Nothing); |
WordApp.Selection.TypeParagraph(); |
Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing); |
newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap; |
newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; |
newTable.Columns[1].Width = 100f; |
newTable.Columns[2].Width = 220f; |
newTable.Columns[3].Width = 105f; |
newTable.Cell(1, 1).Range.Text = "产品详细信息表" ; |
newTable.Cell(1, 1).Range.Bold = 2; |
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3)); |
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; |
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; |
newTable.Cell(2, 1).Range.Text = "产品基本信息" ; |
newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue; |
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3)); |
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; |
newTable.Cell(3, 1).Range.Text = "品牌名称:" ; |
newTable.Cell(3, 2).Range.Text = "BrandName" ; |
newTable.Cell(3, 3).Select(); |
object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine; |
object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend; |
WordApp.Selection.MoveDown( ref moveUnit, ref moveCount, ref moveExtend); |
WordApp.Selection.Cells.Merge(); |
string FileName = @"c:\bg-code.jpg" ; |
object LinkToFile = false ; |
object SaveWithDocument = true ; |
object Anchor = WordDoc.Application.Selection.Range; |
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); |
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f; |
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f; |
Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape(); |
s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare; |
newTable.Cell(12, 1).Range.Text = "产品特殊属性" ; |
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3)); |
WordDoc.Content.Tables[1].Rows.Add( ref Nothing); |
WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString(); |
WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; |
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); |
message = name + "文档生成成功,以保存到C:CNSI下" ; |
在
执行上面的代码时有时会出现错误:如:<strong>检索 COM 类工厂中 CLSID 为
{000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005
</strong> 如果是这样你还需要做一下设置 操作如下 |
解决方法一:
控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档
之后
单击属性打开此应用程序的属性对话框。
2. 单击标识选项卡,然后选择交互式用户。
3.单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后
自定义->编辑->添加ASP.NET账户和IUSER_计算机名
* 这些帐户仅在计算机上安装有 IIS 的情况下才存在。
13. 确保允许每个用户访问,然后单击确定。
14. 单击确定关闭 DCOMCNFG。
解决方法二:
如果上述方法不能解决问题,就应该是权限问题,请尝试用下面的方法:
在web.config中使用身份模拟,在<system.web>节中加入 <identity impersonate="true" userName="你的用户名" password="密码"/>
</system.web>
还要注意:就是里面的路径问题,里面保存word文件的路径和word文档里所要用的图片的路径好要是本地的路径,如果你放的是项目的相对或绝对路径可能会出现错误!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗