如何用C#将html保存为word文档, 注:不是手工操作, 用代码来完成 不是读取, 是将html写入到word文档 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/educast/article/details/4420111希望对你有帮助。 没有html转word, 再来个高手吧! msdn search office automation 这个还不简单啊 你直接用C#读取html文件里面的内容然后在生成word文件不就好了,把读取的内容添加到要生成的文件里面去你生成文件的时候生成doc后缀的文件不就是word文档了吗 html格式无法直接用代码添加到word文件里的。如果你真正试过, 真正自己写过相关的代码就知道。 把代码贴给你吧/// <summary> /// 写入文件 /// </summary> /// <param name="Path">文件路径</param> /// <param name="Text">写入内容</param> /// <param name="Coding">文件编码</param> public static void Writer(string Path, string Text, string Coding) { if (File.Exists(Path)) { Delete(Path); } try { sw = new StreamWriter(Path, false, Encoding.GetEncoding(Coding)); sw.WriteLine(Text); sw.Flush(); sw.Close(); } catch (Exception e) { throw new Exception(e.Message, e); } }三个参数中路径是物理路径比如你要生成word文档就写d:/aaa.doc要写入的内容就是你要保存的html标签编码格式就是字符编码,可以是GB2312或者是utf-8 你要把<和>替换掉就可以了 找到解决办法了还是我刚才的代码/// <summary> private static StreamReader sr; private static StreamWriter sw; /// 读取文件 /// </summary> /// <param name="Path">文件路径</param> /// <param name="Coding">文件编码</param> /// <returns></returns> public static string Reader(string Path, string Coding) { string str = ""; if (File.Exists(Path)) { try { sr = new StreamReader(Path, Encoding.GetEncoding(Coding)); str = sr.ReadToEnd(); sr.Close(); } catch (Exception e) { throw new Exception(e.Message, e); } } return str; } /// <summary> /// 写入文件 /// </summary> /// <param name="Path">文件路径</param> /// <param name="Text">写入内容</param> /// <param name="Coding">文件编码</param> public static void Writer(string Path, string Text, string Coding) { if (File.Exists(Path)) { Delete(Path); } try { sw = new StreamWriter(Path, false, Encoding.GetEncoding(Coding)); sw.WriteLine(Text); sw.Flush(); sw.Close(); } catch (Exception e) { throw new Exception(e.Message, e); } } /// <summary> /// 删除文件 /// </summary> /// <param name="Path">文件路径</param> public static void Delete(string Path) { if (File.Exists(Path)) { try { File.Delete(Path); } catch (Exception e) { throw new Exception(e.Message, e); } } }调用Writer方法传入写入内容参数的时候你在外面进行编码比如Writer("d:/aa.doc", Server.HtmlEncode(Reader("d:/webwz/im/HTMLPage.htm","gb2312")),"gb2312"); 辛苦了, 不过你可以试试——把一个html文件直接改名为 xxx.doc, 是否可以正常打开? 拜托 两者不能混为一谈好不 你没认真看我的代码我把html标签用代码读出来之后用Server.HtmlEncode进行编码把编码之后的内容写入文件路面就可以打开了 内容也都正常只不过尖括号变成了代码等你要用的时候在读取进行解密就完全OK了啊 确实不行的, 哥们。 html与word格式区别很大, 直接写入到docx不现实…… //Aspose.Words.Document d = new Aspose.Words.Document("C:\\Users\\Administrator\\Desktop\\土地资源数据说明示例.doc");//Aspose.Words.Document d = new Aspose.Words.Document("c:\\1.doc");//d.Save("c:\\1.html", Aspose.Words.SaveFormat.Html);试试 确实支持, 但并没有将html转存word的功能 一般你问人家, 最好点一下右下角的引用……如果你给我留言, 最好是复制一下贴子的地址……这个没有什么很好太好的办法…… 你可以参考下我的文章:http://blog.csdn.net/yenange/article/details/8858817 RadioButtonList能不能横排 一个button,三个textbox,点击按钮能显示符合要求的数据。 高手帮帮忙!~~! 在更新得时候“this.GridView1.EditIndex = -1; ”不执行是怎么回事? 請問如何調試dll文件? |M| 从来没有用过存储过程,现在学习:一句存储过程 大家看我理解得对不对 求救~~~js里怎么调用.cs里的变量!!! 十万火急!!! 求得到5万个10位的乱序不重复的数字的算法 消息监控 在VS Studio.NET中如何添加一个事件? ueditor使用问题!!一个诡异现象。。。求帮助 动态加载用户控件的布局问题
html格式无法直接用代码添加到word文件里的。如果你真正试过, 真正自己写过相关的代码就知道。
/// 写入文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="Text">写入内容</param>
/// <param name="Coding">文件编码</param>
public static void Writer(string Path, string Text, string Coding)
{
if (File.Exists(Path))
{
Delete(Path);
}
try
{
sw = new StreamWriter(Path, false, Encoding.GetEncoding(Coding));
sw.WriteLine(Text);
sw.Flush();
sw.Close();
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
三个参数中路径是物理路径
比如你要生成word文档
就写d:/aaa.doc
要写入的内容
就是你要保存的html标签
编码格式就是字符编码,可以是GB2312或者是utf-8
private static StreamReader sr;
private static StreamWriter sw;
/// 读取文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="Coding">文件编码</param>
/// <returns></returns>
public static string Reader(string Path, string Coding)
{
string str = "";
if (File.Exists(Path))
{
try
{
sr = new StreamReader(Path, Encoding.GetEncoding(Coding));
str = sr.ReadToEnd();
sr.Close();
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
} return str;
} /// <summary>
/// 写入文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="Text">写入内容</param>
/// <param name="Coding">文件编码</param>
public static void Writer(string Path, string Text, string Coding)
{
if (File.Exists(Path))
{
Delete(Path);
}
try
{
sw = new StreamWriter(Path, false, Encoding.GetEncoding(Coding));
sw.WriteLine(Text);
sw.Flush();
sw.Close();
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
} /// <summary>
/// 删除文件
/// </summary>
/// <param name="Path">文件路径</param>
public static void Delete(string Path)
{
if (File.Exists(Path))
{
try
{
File.Delete(Path);
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
}
调用Writer方法传入写入内容参数的时候你在外面进行编码比如
Writer("d:/aa.doc", Server.HtmlEncode(Reader("d:/webwz/im/HTMLPage.htm","gb2312")),"gb2312");
辛苦了, 不过你可以试试——把一个html文件直接改名为 xxx.doc, 是否可以正常打开?
确实不行的, 哥们。 html与word格式区别很大, 直接写入到docx不现实……
//Aspose.Words.Document d = new Aspose.Words.Document("c:\\1.doc");
//d.Save("c:\\1.html", Aspose.Words.SaveFormat.Html);试试
确实支持, 但并没有将html转存word的功能
如果你给我留言, 最好是复制一下贴子的地址……这个没有什么很好太好的办法…… 你可以参考下我的文章:
http://blog.csdn.net/yenange/article/details/8858817