.net生成word的问题

chenhaoying chenhaoying 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
.net生成word,使用的是下面的代码:
 StringBuilder sb = new StringBuilder();
sb.AppendLine("<html xmlns:v=\"urn:schemas-microsoft-com:vml\"");
sb.AppendLine("xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sb.AppendLine("xmlns:w=\"urn:schemas-microsoft-com:office:word\"");
sb.AppendLine("xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"");//ns0
sb.AppendLine("xmlns=\"http://www.w3.org/TR/REC-html40\">");
// html代码
sb.AppendLine("<head>");
sb.AppendLine("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
sb.AppendLine("<meta name=ProgId content=Word.Document>");
sb.AppendLine("<meta name=Generator content=\"Microsoft Word 11\">");
sb.AppendLine("<meta name=Originator content=\"Microsoft Word 11\">");
sb.AppendLine("<title></title>");
sb.AppendLine("<!--[if !mso]>");
sb.AppendLine("<style>");
sb.AppendLine("v\\:* {behavior:url(#default#VML);}");
sb.AppendLine("o\\:* {behavior:url(#default#VML);}");
sb.AppendLine("w\\:* {behavior:url(#default#VML);}");
sb.AppendLine(".shape {behavior:url(#default#VML);}");
sb.AppendLine("</style type=\"text/css\">");
sb.AppendLine("<![endif]-->");
//使导出的word以页面模式显示

sb.AppendLine("<!--[if gte mso 9]><xml>");
sb.AppendLine(" <w:WordDocument>");
sb.AppendLine(" <w:View>Print</w:View>");
sb.AppendLine(" <w:SpellingState>Clean</w:SpellingState>");
sb.AppendLine(" <w:GrammarState>Clean</w:GrammarState>");
sb.AppendLine(" <w:ValidateAgainstSchemas/>");
sb.AppendLine(" <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>");
sb.AppendLine(" <w:IgnoreMixedContent>false</w:IgnoreMixedContent>");
sb.AppendLine(" <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>");
sb.AppendLine(" <w:Compatibility>");
sb.AppendLine(" <w:UseFELayout/>");
sb.AppendLine(" </w:Compatibility>");
sb.AppendLine(" <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>");
sb.AppendLine(" </w:WordDocument>");
sb.AppendLine("</xml>");
sb.AppendLine("<![endif]-->");
sb.AppendLine("<!--[if gte mso 9]>");
sb.AppendLine("<xml>");
sb.AppendLine(" <w:LatentStyles DefLockedState=\"false\" LatentStyleCount=\"156\">");
sb.AppendLine(" </w:LatentStyles>");
sb.AppendLine("</xml>");
sb.AppendLine("<![endif]-->");
//样式
sb.AppendLine("<style type=\"text/css\">");

sb.AppendLine(" html, body{font-size: 12px;padding: 0;margin: 0;border: 0;height: 100%;}");
//sb.AppendLine(" .showtable {border-left:1px #000000 solid;border-top:1px #000000 solid;}");
//sb.AppendLine(" .showtable td{border-right:1px #000000 solid;border-bottom:1px #000000 solid;}");
sb.AppendLine(" .showtable,.showtable td,.showtable th{border:1px solid #000000;border-collapse:collapse;}");
sb.AppendLine(" .showtable tr{height:19px;}");
sb.AppendLine(" .showtable tb{border:1px solid #000000;border-collapse:collapse;font-size: 14px;}");

sb.AppendLine(" .showtable2,.showtable2 td,.showtable2 th{border:1px solid #000000;border-collapse:collapse;}");
sb.AppendLine(" .showtable2 tr{height:22px;}");
sb.AppendLine(" .showtable2 tb{border:1px solid #000000;border-collapse:collapse;font-size: 17px;}");

sb.AppendLine(" td{text-align:left;}");

//WORD页面设置
sb.AppendLine(" @page Section1");
sb.AppendLine(" {size:595.3pt 841.9pt;");
sb.AppendLine(" margin:72.0pt 70.9pt 72.0pt 70.9pt;");
sb.AppendLine(" mso-header-margin:42.55pt;");
sb.AppendLine(" mso-footer-margin:49.6pt;");
sb.AppendLine(" mso-paper-source:0;}");
sb.AppendLine(" div.Section1");
sb.AppendLine(" {page:Section1;}");
sb.AppendLine("</style>");

sb.AppendLine("<!--[if gte mso 9]>");
sb.AppendLine("<xml>");
sb.AppendLine(" <o:shapedefaults v:ext=\"edit\" spidmax=\"2050\"/>");
sb.AppendLine("</xml>");
sb.AppendLine("<![endif]-->");
sb.AppendLine("<!--[if gte mso 9]>");
sb.AppendLine("<xml>");
sb.AppendLine(" <o:shapelayout v:ext=\"edit\">");
sb.AppendLine(" <o:idmap v:ext=\"edit\" data=\"1\"/>");
sb.AppendLine(" </o:shapelayout>");
sb.AppendLine("</xml>");
sb.AppendLine("<![endif]-->");
sb.AppendLine("</head>");
//文档内容开始

sb.AppendLine("<body lang=ZH-CN style='tab-interval:21.0pt'>");
sb.AppendLine("<div class=Section1>"); //页面设置样式
sb.AppendLine(htmltext);
sb.AppendLine("</div>");
sb.AppendLine("</body>");
sb.AppendLine("</html>");


Encoding encoding = Encoding.GetEncoding("utf-8");//
StreamWriter sw = null;


string path = "/uploads/swfupload/" + fileName + ".doc";
string OutPutPath = HttpContext.Current.Server.MapPath(path);

if (System.IO.File.Exists(OutPutPath))
{
System.IO.File.Delete(OutPutPath);
}
sw = new StreamWriter(OutPutPath, false, encoding);
sw.Write(sb);
sw.Flush();
sw.Close();


这个代码生成word是没问题,显示也没问题,但是如果htmltext里有带url的图片的话,那打开这个word编辑保存的时候,word就会生成一个file的文件夹,造成那个word不再是一个单独的word了。必须要编辑后选择另存为来存一个doc文件覆盖它,可是客户觉得这样很不方便,他们习惯了直接保存。能不能在生成word的时候图片是做生成编码放入word中呢?就像htm的data:image/png;base64这样呢?高手指点一下
...全文

相关推荐

ASP.NET生成WORD文档服务器部署注意事项

ASP.NET生成WORD文档服务器部署注意事项 配置详情请下载附件图解 1、Asp.net 2.0在配置Microsoft Excel、Microsoft Word应用程序权限时 error: 80070005 和8000401a 的解决总 2007-11-01 11:30 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。 控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档 之后 单击属性打开此应用程序的属性对话框。 单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后 自定义->编辑->添加ASP.NET账户和IUSER_计算机名 * 这些帐户仅在计算机上安装有 IIS 的情况下才存在。 在标识选项卡选择下列用户,输入用户名密码 13. 确保允许每个用户访问,然后单击确定。 14. 单击确定关闭 DCOMCNFG。 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 8000401a 。 运行dcomcnfg打开组件服务, 依次展开"组件服务"->"计算机"->"我的电脑"->"DCOM配置" 找到"Microsoft Excel应用程序"或"Microsoft Word应用程序", 右键打开属性对话框,点击"标识"选项卡, 点"下列用户",把管理员的用户密码正确填写进去... 点击"安全"选项卡, 依次把"启动和激活权限","访问权限","配置权限",都选择为自定义, 然后依次点击它们的编辑,把everyone添加进去,并加入所有的权限... OK,解决此问题! 2、请设置web.config中的帐号和密码,否则会提示检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。 例如

kings2015

VB.NET实现调用WORD模板写入数据并生成新的WORD文档

VB.NET实现将数据导入WORD模板中,并生成新的WORD文档,需要在WORD中设置书签,用数据替换即可。可以将图片导入WORD,但是无法控制图片位置。

东海凌波

用C#.NET实现生成PDF文档和将WORD转换为PDF

用C#.NET实现生成PDF文档和将WORD转换为PDF

lqa475936988

asp.net导出word c#生成word

源码 有1:asp.net 网页下载的word 给html串 生成相应的word下载到本地。 2:winform 生成word 按自己的格式 生成(需要按自己的格式写生成规则)

qqqq5701

asp.net 利用word模板导出word文件

asp.net 利用word模板导出word文件 实例

望梵
评论 6
收藏 已收藏
分享
返回顶部
chenhaoying
.NET社区 62,275 成员 668,968 内容