示例

原文件结构:

替换后文档结构:

软件截图:

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading;

using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;

namespace WordInsertImg
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog PictureDlg = new OpenFileDialog();
PictureDlg.Title = "打开文档";
PictureDlg.Filter = "DOC文件(*.doc)|*.doc";
PictureDlg.Multiselect = true;
if (PictureDlg.ShowDialog() == DialogResult.OK)
{
string foldPath = PictureDlg.FileName;
txtPath.Text = foldPath;
if (File.Exists("pathWord.ini"))
{
StreamWriter sw = new StreamWriter("pathWord.ini");
sw.Write(foldPath);
sw.Close();
}
else
{
File.WriteAllText("pathWord.ini", foldPath);
}
}
}

private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog PictureDlg = new OpenFileDialog();
PictureDlg.Title = "打开图片";
PictureDlg.Filter = "JPG图片(*.jpg)|*.jpg|BMP图片(*.bmp)|*.bmp|所有文件(*.*)|*.*";
PictureDlg.Multiselect = true;
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";
if (dialog.ShowDialog() == DialogResult.OK)
{
string foldPath = dialog.SelectedPath;
txtImgPath.Text = foldPath;
if (File.Exists("path.ini"))
{
StreamWriter sw = new StreamWriter("path.ini");
sw.Write(foldPath);
sw.Close();
}
else
{
File.WriteAllText("path.ini", foldPath);
}
}
}
private void btnUpload_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
object MissingValue = Type.Missing;
object file = txtPath.Text;
string PathNew = txtPath.Text.Replace(".doc", "_New_") + DateTime.Now.ToString("yyMMddHHmmss") + ".doc";
if (File.Exists(PathNew))
File.Delete(PathNew);
File.Copy(Convert.ToString(file), PathNew);
file = PathNew;
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
ref file, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue);
//doc.Content.Find.Text = strOldText ;
StringBuilder sbStrOld = new StringBuilder();
StringBuilder sbStrNew = new StringBuilder();
#region 表格替换
if (doc.Tables.Count > 0)
{
//int oldRows = 1;
for (int i = 1; i <= doc.Tables[1].Rows.Count; i++)
{
//取得单元格中的值
string item1 = doc.Tables[1].Cell(i, 1).Range.Text.Replace("\r\a", "");
//将光标指定到当前单元格
doc.Tables[1].Cell(i, 1).Select();
string FileName = "";
if (File.Exists(txtImgPath.Text + "\\" + item1 + ".jpg"))
{
doc.Tables[1].Cell(i, 1).Range.Text = "";
FileName = txtImgPath.Text + "\\" + item1 + ".jpg";//图片所在路径
}
else if (File.Exists(txtImgPath.Text + "\\" + item1 + ".png"))
{
doc.Tables[1].Cell(i, 1).Range.Text = "";
FileName = txtImgPath.Text + "\\" + item1 + ".png";//图片所在路径
}
else if (File.Exists(txtImgPath.Text + "\\" + item1 + ".jpeg"))
{
doc.Tables[1].Cell(i, 1).Range.Text = "";
FileName = txtImgPath.Text + "\\" + item1 + ".jpeg";//图片所在路径
}
if (File.Exists(FileName))
{
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = doc.Application.Selection.Range;
doc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
doc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
doc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
//将图片设置为四周环绕型
//Microsoft.Office.Interop.Word.Shape s = doc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
//s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
//以下为单元格跳转方式
//object what = WdGoToItem.wdGoToTable;
//object which = WdGoToDirection.wdGoToNext;
//doc.Application.Selection.GoTo(ref what, ref which);

//object which = WdGoToDirection.wdGoToNext;
//doc.Application.Selection.GoTo(ref which);
}
FileName = "";
string item2 = doc.Tables[1].Cell(i, 2).Range.Text.Replace("\r\a", "");
doc.Tables[1].Cell(i, 2).Select();
if (File.Exists(txtImgPath.Text + "\\" + item2 + ".jpg"))
{
doc.Tables[1].Cell(i, 2).Range.Text = "";
FileName = txtImgPath.Text + "\\" + item2 + ".jpg";//图片所在路径
}
else if (File.Exists(txtImgPath.Text + "\\" + item2 + ".png"))
{
doc.Tables[1].Cell(i, 2).Range.Text = "";
FileName = txtImgPath.Text + "\\" + item2 + ".png";//图片所在路径

}
else if (File.Exists(txtImgPath.Text + "\\" + item2 + ".jpeg"))
{
doc.Tables[1].Cell(i, 2).Range.Text = "";
FileName = txtImgPath.Text + "\\" + item2 + ".jpeg";//图片所在路径
}

if (File.Exists(FileName))
{
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = doc.Application.Selection.Range;
doc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
doc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
doc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
}
}
}
#endregion
doc.Save();
doc.Close(ref MissingValue, ref MissingValue, ref MissingValue);
//关闭应用
app.Quit(ref MissingValue, ref MissingValue, ref MissingValue);
app = null;
MessageBox.Show("插入完毕");
}

private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists("pathWord.ini"))
{
StreamReader sr = new StreamReader("pathWord.ini");
txtPath.Text = sr.ReadToEnd();
sr.Close();
}
if (File.Exists("path.ini"))
{
StreamReader sr = new StreamReader("path.ini");
txtImgPath.Text = sr.ReadToEnd();
sr.Close();
}
}
}
}

要注意 Microsoft.Office.Interop.Word引用的版本,我这里引用的是14.0版的

将Word表格中单元格中的文字替换成对应的图片的更多相关文章

  1. 使用POI创建word表格合并单元格兼容wps

    poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...

  2. poi 读取word 遍历表格和单元格中的图片

    背景 项目需要解析word表格 需要批量导入系统,并保存每行信息到数据库 并且要保存word中的图片, 并保持每条信息和图片的对应关系 一行数据可能有多条图片 解决办法 没有找到现成的代码,怎么办呐? ...

  3. 使用POI创建word表格-在表格单元格中创建子表格

    要实现的功能如下:表格中的单元格中有子表格 实现代码如下: XWPFParagraph cellPara = row.getCell(j).getParagraphArray(0); //row.ge ...

  4. EXCEL表格单元格中包含数字英文和汉字,如何自动去掉汉字,保留英文和数字

    EXCEL表格单元格中包含数字英文和汉字,如何自动去掉汉字,保留英文和数字 Function 求数字和字母(对象 As String) '在文本与数字混杂中提取数字和字母   Dim myReg    ...

  5. python 将表格多个列数据放到同一个单元格中

      表格模板: 目的将卡片1到卡片5的所有数据组合起来到一个单元格中如下入F列中(工作中为了避免手动复制粘贴),其余不变,因为数据太多 自己一个一个复制工作效率太低,所以写这个脚本是为了方便自己有需要 ...

  6. Swift - 可编辑表格样例(可直接编辑单元格中内容、移动删除单元格)

    (本文代码已升级至Swift3)   本文演示如何制作一个可以编辑单元格内容的表格(UITableView). 1,效果图 (1)默认状态下,表格不可编辑,当点击单元格的时候会弹出提示框显示选中的内容 ...

  7. itextpdf中表格中单元格的文字水平垂直居中的设置

    在使用itextpdf中,版本是5.5.6,使用Doucument方式生成pdf时,设置单元格中字体的对齐方式时,发现一些问题,并逐渐找到了解决方式. 给我的经验就是:看官网的例子才能保证代码的效果, ...

  8. Swift - 异步加载各网站的favicon图标,并在单元格中显示

    下面是一个简单的应用,表格视图的各个单元格自动异步加载各个网站的favicon图标,并显示出来. 主要是复习下如何自定义单元格,单元格中图片的异步加载,以及didSet的用法. 效果图如下: 操作步骤 ...

  9. html table中单元格自动换行

    table中单元格自动换行样式: table-layout: fixed; word-wrap: break-word;   table-layout 可能的值(IE不支持inherit属性) 值 描 ...

随机推荐

  1. vue中什么样的数据可以是在视图中显示

    1. Vue中不可以添加不存在的属性,因为不存在的属性是没有getter和setter的. <div id="app"> {{msg.a}} {{msg.b}} < ...

  2. JavaScript 变量、类型与计算

    变量类型 变量计算 变量 题目: JavaScript 中使用typeof能得到的有哪些类型? ``` 1.1 变量类型 (1).js中的数据类型:字符串.数字.布尔.数组.对象.Null.Undef ...

  3. BFC(块级格式上下文)

    BFC的生成 满足下列css声明之一的元素便会生成BFC 根元素 float的值不为none overflow的值不为visible display的值为inline-block.table-cell ...

  4. 【Java框架型项目从入门到装逼】第十四节 查询用户列表展现到页面

    这一节,我们来实现一下用户列表搜索,最终的效果如下: 这边我们要使用easyUI给我们提供的datagrid组件. HTML结构如下 <!-- 数据列表 --> <table id= ...

  5. Log4net使用详细说明

    1.概述 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是介绍如何在Visual S ...

  6. android在一个应用程序员启动另一个程序

    一般我们知道了另一个应用的包名和MainActivity的名字之后便可以直接通过如下代码来启动: Intent intent = new Intent(Intent.ACTION_MAIN); int ...

  7. js点击图片查看大图,并可以拖动,且滚动滑轮放大缩小

    方法一:此方法在页面没有滚动条时无法缩放 JQuery function hideMax(){ $(".MAX_div").remove(); $("#Cover_Div ...

  8. httpd添加新模块

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  9. 【php】RBAC 管理权限

    用户   角色   权限 用户:张三 角色:管理员 权限:page/index1.php   能访问的页面

  10. 基本c功能使用不当导致崩溃

    一些基本的c语言操作,使用不当也会有出其不意的问题.比如我最近的一个项目中,用到几句代码: uint8_t * out_pcm = NULL; ....... if (NULL == out_pcm) ...