从数据库读出一些数据:字段1:文字 字段2:图片路径,要输出:字段1+字段2 作为word里面的一个 段落Paragraph,同时要图片垂直居中对齐。
------解决思路----------------------
参考下:
http://bbs.csdn.net/topics/60326501
http://bbs.csdn.net/topics/380101169
------解决思路----------------------
C#向Word文档中写入图片和文字示例
//向Word文档写入文字
private Word.Application G_wa;//定义Word应用程序字段
private object G_missing = //定义missing字段并赋值
System.Reflection.Missing.Value;
private object G_str_path;//定义文件保存路径字段
private FolderBrowserDialog G_FolderBrowserDialog;//定义文件夹浏览对话框字段
private void btn_New_Click(object sender, EventArgs e)
{
btn_New.Enabled = false;//停用新建按钮
ThreadPool.QueueUserWorkItem(//使用线程池
(P_temp) =>//使用lambda表达式
{
G_wa = new Word.Application();//创建Word应用程序对象
Word.Document P_wd = G_wa.Documents.Add(//建立新文档
ref G_missing, ref G_missing, ref G_missing, ref G_missing);
Word.Range P_Range = P_wd.Paragraphs[1].Range;
P_Range.Text = txt_add.Text;
G_str_path = string.Format(//计算文件保存路径
@"{0}\{1}", G_FolderBrowserDialog.SelectedPath,
DateTime.Now.ToString("yyyy年M月d日h时s分m秒fff毫秒") + ".doc");
P_wd.SaveAs(//保存Word文件
ref G_str_path,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing);
((Word._Application)G_wa.Application).Quit(//退出应用程序
ref G_missing, ref G_missing, ref G_missing);
this.Invoke(//开始执行窗体线程
(MethodInvoker)(() =>//使用lambda表达式
{
btn_Display.Enabled = true;//启用显示按钮
MessageBox.Show("成功创建Word文档!", "提示!");
}));//codego.net/tags/1/1/
});
}//(省略其他)
//向Word文档插入图片
private Word.Application G_wa;//定义Word应用程序字段
private object G_missing = //定义missing字段并赋值
System.Reflection.Missing.Value;
private object G_str_path;//定义文件保存路径字段
private OpenFileDialog G_OpenFileDialog;//定义打开文件对话框字段
private FolderBrowserDialog G_FolderBrowserDialog;//定义文件夹浏览对话框字段
private void btn_New_Click(object sender, EventArgs e)
{
btn_New.Enabled = false;//停用新建按钮
ThreadPool.QueueUserWorkItem(//使用线程池
(P_temp) =>//使用lambda表达式
{
G_wa = new Word.Application();//创建Word应用程序对象
Word.Document P_wd = G_wa.Documents.Add(//建立新文档
ref G_missing, ref G_missing, ref G_missing, ref G_missing);
Word.Range P_Range = P_wd.Paragraphs[1].Range;//得到段落范围
object P_Ranges = P_Range;//创建ojbect对象
P_wd.InlineShapes.AddPicture(//向文档中插入图片
G_OpenFileDialog.FileName, ref G_missing, ref G_missing, ref P_Ranges);
G_str_path = string.Format(//计算文件保存路径
@"{0}\{1}", G_FolderBrowserDialog.SelectedPath,
DateTime.Now.ToString("yyyy年M月d日h时s分m秒fff毫秒") + ".doc");
P_wd.SaveAs(//保存Word文件
ref G_str_path,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing);
((Word._Application)G_wa.Application).Quit(//退出应用程序
ref G_missing, ref G_missing, ref G_missing);
this.Invoke(//开始执行窗体线程
(MethodInvoker)(() =>//使用lambda表达式
{
btn_Display.Enabled = true;//启用显示按钮
MessageBox.Show("成功创建Word文档!", "提示!");
}));
});
}