示例

原文件结构:

替换后文档结构:

软件截图:

代码:

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. Failed to load the JNI library "E:\JDK6.0\bin\client\jvm.dll"

    在打开Eclipse是错误提示:Failed to load the JNI library "E:\JDK6.0\bin\client\jvm.dll" 如图1所示 图1 遇到这 ...

  2. [知了堂学习笔记]_集合接口list与集合接口set的区别

    在Java中 除了 Map以外的集合的根接口都是Collection接口,而在Collection接口的子接口中,最重要的莫过于List和Set集合接口. 今天我们就来谈谈List集合接口与Set集合 ...

  3. git 文件状态与工作区域

    在上一篇简单讲述了文件状态与工作区域,在这里结合相关git命令详细了解文件的状态变更. 目录 1. 介绍 2. 常用命令 3. 实际操作 1. 介绍 git的文件状态是其git核心内容,了解后对后续的 ...

  4. linux_mount相关故障

    fstab修改错误导致系统无法启动故障修复方案 1. 维护模式或救援模式 2. mount -o rw,remount 挂载点 # 这个方式也可以解决有些分区只能读的故障 3. 然后修改 /etc/f ...

  5. yum错误,Cannot find a valid baseurl for repo: base 和 No more mirrors to try

    可能出错原因: 1. yum 配置错误 2. 虚拟机无法连接外网 3. 域名解析没有 如何解决这个错误? 1. 网上找 /ect/yum.conf 和 /etc/yum.repos.d/CentOS- ...

  6. input标签(待填坑)

    input标签几种属性值 button:用作定义按钮 checkbox:定义复选框 file:供文件上传 hidden:定义隐藏的输入字段 image:图像形式的提交按钮 password:密码字段 ...

  7. 自动化安装DHCP配置脚本

    DHCP配置脚本: #!/bin/sh NET=192.168.6.0 MASK=255.255.255.0 RANGE="192.168.6.50 192.168.6.100" ...

  8. SpringMVC源码情操陶冶-DispatcherServlet父类简析

    阅读源码有助于陶冶情操,本文对springmvc作个简单的向导 springmvc-web.xml配置 <servlet> <servlet-name>dispatch< ...

  9. es故障节点恢复后加入集群导致删除索引重新出现

    es的每个shard下的文件都可以看做一个完整的lucene文件,shard数据目录下的segment文件包含了索引的分片数量,副本数量.es shard可以恢复,就是因为每个shard都包含了一份数 ...

  10. B 洛谷 P3604 美好的每一天 [莫队算法]

    题目背景 时间限制3s,空间限制162MB 素晴らしき日々 我们的情人,不过是随便借个名字,用幻想吹出来的肥皂泡,把信拿去吧,你可以使假戏成真.我本来是无病呻吟,漫无目的的吐露爱情---现在这些漂泊不 ...