word 字符串参量过长 解决
2016-06-30 13:35
330 查看
动态生成word文档,传参数过长处理。
public static void ReplaceToWord(object path, Dictionary<string, string> datas, string newfile, string physicNewFile)
{
Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word._Document doc = null;
try
{
app = new Microsoft.Office.Interop.Word.Application();//创建word应用程序
object fileName = path;//模板文件C:\Users\Administrator\Desktop\
//打开模板文件
object oMissing = System.Reflection.Missing.Value;
doc = app.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
foreach (var item in datas)
{
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.ClearFormatting();
if (item.Value.Length > 220)
FindAndReplaceLong(app, item.Key, item.Value);
else
FindAndReplace(app, item.Key, item.Value);
//2016-06-30字符串太长
//app.Selection.Find.Text = item.Key;//需要被替换的文本
//app.Selection.Find.Replacement.Text = item.Value;//替换文本
////执行替换操作
//app.Selection.Find.Execute(
//ref oMissing, ref oMissing,
//ref oMissing, ref oMissing,
//ref oMissing, ref oMissing,
//ref oMissing, ref oMissing, ref oMissing,
//ref oMissing, ref replace,
//ref oMissing, ref oMissing,
//ref oMissing, ref oMissing);
}
//对替换好的word模板另存为一个新的word文档
doc.SaveAs(physicNewFile,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
doc.Close();//关闭word文档
}
if (app != null)
{
app.Quit();//退出word应用程序
}
}
}
public static void FindAndReplaceLong(Word.Application wordApp, object findText, object replaceText)
{
int len = replaceText.ToString().Length; //要替换的文字长度
int cnt = len / 220; //不超过220个字
string newstr;
object newStrs;
if (len < 220) //小于220字直接替换
{
FindAndReplace(wordApp, findText, replaceText);
}
else
{
for (int i = 0; i <= cnt; i++)
{
if (i != cnt)
newstr = replaceText.ToString().Substring(i * 220, 220) + findText; //新的替换字符串
else
newstr = replaceText.ToString().Substring(i * 220, len - i * 220); //最后一段需要替换的文字
newStrs = (object)newstr;
FindAndReplace(wordApp, findText, newStrs); //进行替换
}
}
}
public static void FindAndReplace(Word.Application wordApp, object findText, object replaceText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards,
ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceText,
ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}
Asp.Net操作Word内容
如果要正常操作Word Com组件的话,必须要给用户赋上足够的权限的,
1、运行Dcomcnfg.exe
2、组件服务――计算机――我的电脑――DCOM配置――找到microsoft word 文档
3、点击属性
4、选择“安全性”
5、选定“使用自定义访问权限”和“使用自定义启动权限”
6、分别编辑权限,添加ASPNET,VS Developers,Debugger User //不一定
7、选择“身份标识”,在选定“交互式用户” 即可 (关键步骤)必须
8、在Web.config里加 <identity impersonate="true"/>
public static void ReplaceToWord(object path, Dictionary<string, string> datas, string newfile, string physicNewFile)
{
Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word._Document doc = null;
try
{
app = new Microsoft.Office.Interop.Word.Application();//创建word应用程序
object fileName = path;//模板文件C:\Users\Administrator\Desktop\
//打开模板文件
object oMissing = System.Reflection.Missing.Value;
doc = app.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
foreach (var item in datas)
{
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.ClearFormatting();
if (item.Value.Length > 220)
FindAndReplaceLong(app, item.Key, item.Value);
else
FindAndReplace(app, item.Key, item.Value);
//2016-06-30字符串太长
//app.Selection.Find.Text = item.Key;//需要被替换的文本
//app.Selection.Find.Replacement.Text = item.Value;//替换文本
////执行替换操作
//app.Selection.Find.Execute(
//ref oMissing, ref oMissing,
//ref oMissing, ref oMissing,
//ref oMissing, ref oMissing,
//ref oMissing, ref oMissing, ref oMissing,
//ref oMissing, ref replace,
//ref oMissing, ref oMissing,
//ref oMissing, ref oMissing);
}
//对替换好的word模板另存为一个新的word文档
doc.SaveAs(physicNewFile,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
doc.Close();//关闭word文档
}
if (app != null)
{
app.Quit();//退出word应用程序
}
}
}
public static void FindAndReplaceLong(Word.Application wordApp, object findText, object replaceText)
{
int len = replaceText.ToString().Length; //要替换的文字长度
int cnt = len / 220; //不超过220个字
string newstr;
object newStrs;
if (len < 220) //小于220字直接替换
{
FindAndReplace(wordApp, findText, replaceText);
}
else
{
for (int i = 0; i <= cnt; i++)
{
if (i != cnt)
newstr = replaceText.ToString().Substring(i * 220, 220) + findText; //新的替换字符串
else
newstr = replaceText.ToString().Substring(i * 220, len - i * 220); //最后一段需要替换的文字
newStrs = (object)newstr;
FindAndReplace(wordApp, findText, newStrs); //进行替换
}
}
}
public static void FindAndReplace(Word.Application wordApp, object findText, object replaceText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards,
ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceText,
ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}
Asp.Net操作Word内容
如果要正常操作Word Com组件的话,必须要给用户赋上足够的权限的,
1、运行Dcomcnfg.exe
2、组件服务――计算机――我的电脑――DCOM配置――找到microsoft word 文档
3、点击属性
4、选择“安全性”
5、选定“使用自定义访问权限”和“使用自定义启动权限”
6、分别编辑权限,添加ASPNET,VS Developers,Debugger User //不一定
7、选择“身份标识”,在选定“交互式用户” 即可 (关键步骤)必须
8、在Web.config里加 <identity impersonate="true"/>
相关文章推荐
- 双向链表的建立
- Avoiding Buffer Overruns
- maven向本地仓库导入官方仓库没有的jar包
- 复旦大学2015--2016学年第二学期(15级)高等代数II期末考试第六大题解答
- leadJS初构建
- Android Button自动触发点击事件
- Linux中启动和停止jar包的运行
- Android之对话框AlertDialog及其子类的使用(二)
- Javascript中单击按键来循环改变字体的颜色
- 如何查看一个进程中的某个线程是否存活?
- Android实现不重复启动APP的方法
- crontab
- adb 使用的一点命令
- Jquery基本选择器总结
- Maven 手动添加 JAR 包到本地仓库
- GDB(十)--调试正在运行的进程
- mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理
- 优化ELK(2)
- TS学习笔记1之散点图
- 如何在AngularJS指令中创建controller与ngModel进行交互