SharePoint 更新word 等文档的内容,包括替换哦。功能强大
2012-08-13 10:03
375 查看
public void UpdateDocument()
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteColl = new SPSite("http://localhost:8080/"))
{
using (SPWeb web = siteColl.OpenWeb())
{
try
{
SPFile spfile = web.GetFile("http://localhost:8080/Lists/DemoLib/abc.txt");
if (spfile.Exists)
{
byte[] byteArrayFileContentsBefore = spfile.OpenBinary();
if (byteArrayFileContentsBefore.Length > 0)
{
string strFileContentsBefore = enc.GetString(byteArrayFileContentsBefore); //convert byte array to string.
string newStr = strFileContentsBefore.Replace("http://dev:999","http://google.com");
byte[] byteArrayFileContentsAfter = null;
if (!newStr.Equals(""))
{
byteArrayFileContentsAfter = enc.GetBytes(newStr);
spfile.SaveBinary(byteArrayFileContentsAfter); //save to the file.
}
}
}
}
catch (Exception e) { }
}
}
});
}
SPFile has a CopyFile method which can copy the file to a new location. But if there was an existing file on the new location, you can set the overwrite parameter to true to overwrite it. Here is a problem, supposingly there was a workflow already on the file in the new location... when you use CopyFile.. the workflow is lost.. basically it is not an update of the file.. it is infact a delete and re-adding of the file. The following code will overcome this problem
?
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteColl = new SPSite("http://localhost:8080/"))
{
using (SPWeb web = siteColl.OpenWeb())
{
try
{
SPFile spfile = web.GetFile("http://localhost:8080/Lists/DemoLib/abc.txt");
if (spfile.Exists)
{
byte[] byteArrayFileContentsBefore = spfile.OpenBinary();
if (byteArrayFileContentsBefore.Length > 0)
{
string strFileContentsBefore = enc.GetString(byteArrayFileContentsBefore); //convert byte array to string.
string newStr = strFileContentsBefore.Replace("http://dev:999","http://google.com");
byte[] byteArrayFileContentsAfter = null;
if (!newStr.Equals(""))
{
byteArrayFileContentsAfter = enc.GetBytes(newStr);
spfile.SaveBinary(byteArrayFileContentsAfter); //save to the file.
}
}
}
}
catch (Exception e) { }
}
}
});
}
SPFile has a CopyFile method which can copy the file to a new location. But if there was an existing file on the new location, you can set the overwrite parameter to true to overwrite it. Here is a problem, supposingly there was a workflow already on the file in the new location... when you use CopyFile.. the workflow is lost.. basically it is not an update of the file.. it is infact a delete and re-adding of the file. The following code will overcome this problem
?
相关文章推荐
- python python-docx 替换word文档部分内容
- 黄聪:C#实现WORD文档的内容复制和替换
- vb控制word的类模块,查找、替换Word文档内容
- C#实现WORD文档的内容复制和替换
- 具有强大构建强大的Word报表,支持XAML数据绑定,文档格式转换,导出为PDF等功能的控件TX Text Control .NET for WPF介绍
- 强大的:Word文档巧替换(空行替换、空格替换、软回车替换成硬回车)
- poi操作word文档,以07版本为参考,将word文档中图片替换为文本(替换文本也一样),读图片可选文字内容
- SharePoint禁用(Office Web Apps)在word打开,配置启用浏览器功能的文档的默认打开行为
- C#实现WORD文档的内容复制和替换
- Word 2007 POI 替换文档内容
- java poi组件 读取word文档 替换文档 内容 图片
- 强大的word查找替换功能
- 用VBA批量替换多个Word文档中同一内容
- 我做了一个Asp.net网站,其中包含一个word文档的生成功能,
- UltraEdit强大的搜索功能,有提示的正则匹配搜索匹配替换
- word参考文献标注功能--自动更新--以word 2013为例
- C# 给Word文档添加内容控件
- 关于向Word文档中的RichText控件插入rtf内容的问题
- c++ STL中元素替换功能的强大函数
- Java 替换word文档文字并指定位置插入图片