将Word表格中单元格中的文字替换成对应的图片

示例

原文件结构:

将Word表格中单元格中的文字替换成对应的图片

替换后文档结构:

将Word表格中单元格中的文字替换成对应的图片

软件截图:

将Word表格中单元格中的文字替换成对应的图片

代码:

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版的

上一篇:Excel催化剂开源第15波-VSTO开发之DataTable数据导出至单元格区域


下一篇:Bootstrap 学习笔记 项目实战 首页内容介绍 上