示例

原文件结构:

替换后文档结构:

软件截图:

代码:

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. python-虎扑爬虫

    Python作为一个高级编程语言,不知从何时起就在圈子里流行起来了.个人也是图个鲜,跟上时代步伐学习了一下."鲁迅"说过:不能学以致用,就是耍流氓.我用python对虎扑论坛作了一 ...

  2. Springmvc 并发访问的线程安全性问题

    首先对于spring的IOC来说,对象是由Spring来帮我们管理,也就是在Spring启动的时候,在Spring容器中,由Spring给我们创建的,Spring会帮我们维护,一般都是单例的,也就是一 ...

  3. Python+Selenium安装及环境配置

    一.Python安装 Window系统下,python的安装很简单.访问python.org/download,下载最新版本,安装过程与其他windows软件类似.记得下载后设置path环境变量,然后 ...

  4. JavaScript return 最简单解释

    一.return 返回值 1)函数名字 +括号 :fun() ==> retrun 后面的值 2)所以函数的模范返回值是为未定义 3)return; 后面的任何代码都不会执行了 二.arguem ...

  5. 006-接收键盘的输入(read)

    read  -ptns   变量名 -p 在等待read输入的时候,显示的提示信息 -t 秒数,read等待用户输入的时间 -n read接收用户输入的字符数,只接收指定字符数,就会执行 -s 隐藏输 ...

  6. RocketMQ-广播模式消费

    Rocketmq 消费者默认是集群的方式消费的,消费者还可以用广播的模式进行消费.广播模式消费就是所有订阅同一个主题的消费者都会收到消息.代码实现上其实很简单,就是在消费端添加 consumer.se ...

  7. Azure Powershell对ASM资源的基本操作

    本文主要介绍Windows Azure Powershell对ASM资源的基本操作 1.登陆ASM模式,命令:Add-AzureAccount -Environment AzureChinaCloud ...

  8. 查阅API文档

    Java的API文档:就一句话:应用程序接口 •API (Application Programming Interface,应用程序编程接口)是 Java 提供的基本编程接口. •Java语言提供了 ...

  9. remoteViews简介

    RemoteViews从字面上看是一种远程视图.RemoteViews具有View的结构,既然是远程View,那么它就可以在其他进程中显示.由于它可以跨进程显示,所以为了能够更新他的界面,Remote ...

  10. redis数据类型-散列类型

    Redis数据类型 散列类型 Redis是采用字典结构以键值对的形式存储数据的,而散列类型(hash)的键值也是一种字典结构,其存储了字段(field)和字段值的映射,但字段值只能是字符串,不支持其他 ...