将Word表格中单元格中的文字替换成对应的图片
2018-02-28 17:34
633 查看
示例
原文件结构:
替换后文档结构:
软件截图:
代码:
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版的
原文件结构:
替换后文档结构:
软件截图:
代码:
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版的
相关文章推荐
- Java处理word文档 用jacob 表格图片文字替换
- c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
- c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
- C# 操作Word书签(二)——插入图片、表格到书签;读取、替换书签
- C# :Word 标签替代文字或图片 (图片浮于文字之上 插入后回车或换行符替换的解决方法)
- C# VBA 填充Word表格 ,并随意转移单元格焦点或插入文本,及相关文字的颜色、粗体等相关属性。
- poi操作word文档,以07版本为参考,将word文档中图片替换为文本(替换文本也一样),读图片可选文字内容
- delphi操作word基类,插入表格,分页符,日期,页码,替换,图片
- C#通过模板导出Word(文字,表格,图片)
- 用C#操作office组件库实现word文档的创建及文字 表格 图片的插入
- jacob插件导出文字、表格、图片到Word
- word - 无边框表格帮助布局文字和图片
- 怎样在WORD中,删除表格但保留表格中得文字在对应得位置
- C++操作word:插入文字、图片、表格,设置样式字体
- word 批量 文字替换图片
- 提取word文档的图片及表格并用html代码替换
- delphi操作word基类,插入表格,分页符,日期,页码,替换,图片
- 使用poi替换word中的文字和图片实现打印
- C# VBA 填充Word表格 ,并随意转移单元格焦点或插入文本,及相关文字的颜色、粗体等相关属性。
- 巧用正则表达式将Word文字替换为图片