private void but_Table_Click(object sender, EventArgs e)
{

object Nothing = System.Reflection.Missing.Value;
object missing = System.Reflection.Missing.Value;
//创建Word文档
Word.Application wordApp = new Word.ApplicationClass();
Word.Document wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordApp.Visible = true;

//设置文档宽度
wordApp.Selection.PageSetup.LeftMargin = wordApp.CentimetersToPoints(float.Parse("2"));
wordApp.ActiveWindow.ActivePane.HorizontalPercentScrolled = 11;
wordApp.Selection.PageSetup.RightMargin = wordApp.CentimetersToPoints(float.Parse("2"));

Object start = Type.Missing;
Object end = Type.Missing;

PictureBox pp = new PictureBox(); //新建一个PictureBox控件
int p1 = 0;
for (int i = 0; i < MyDS_Grid.Tables[0].Rows.Count; i++)
{
try
{
byte[] pic = (byte[])(MyDS_Grid.Tables[0].Rows[i][23]); //将数据库中的图片转换成二进制流
MemoryStream ms = new MemoryStream(pic); //将字节数组存入到二进制流中
pp.Image = Image.FromStream(ms); //二进制流Image控件中显示
pp.Image.Save(@"C:\22.bmp"); //将图片存入到指定的路径
}
catch
{
p1 = 1;
}
object rng = Type.Missing;
string strInfo = "职工基本信息表" + "(" + MyDS_Grid.Tables[0].Rows[i][1].ToString() + ")";
start = 0;
end = 0;
wordDoc.Range(ref start, ref end).InsertBefore(strInfo); //插入文本
wordDoc.Range(ref start, ref end).Font.Name = "Verdana"; //设置字体
wordDoc.Range(ref start, ref end).Font.Size = 20; //设置字体大小
wordDoc.Range(ref start, ref end).ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //设置字体局中

start = strInfo.Length;
end = strInfo.Length;
wordDoc.Range(ref start, ref end).InsertParagraphAfter();//插入回车

object missingValue = Type.Missing;
object location = strInfo.Length; //如果location超过已有字符的长度将会出错。一定要比"明细表"串多一个字符
Word.Range rng2 = wordDoc.Range(ref location, ref location);

wordDoc.Tables.Add(rng2, 14, 6, ref missingValue, ref missingValue);
wordDoc.Tables.Item(1).Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightAtLeast;
wordDoc.Tables.Item(1).Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));
wordDoc.Tables.Item(1).Range.Font.Size = 10;
wordDoc.Tables.Item(1).Range.Font.Name = "宋体";

//设置表格样式
wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).Color = Word.WdColor.wdColorAutomatic;
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐

//第5行显示
wordDoc.Tables.Item(1).Cell(1, 5).Merge(wordDoc.Tables.Item(1).Cell(5, 6));
//第6行显示
wordDoc.Tables.Item(1).Cell(6, 5).Merge(wordDoc.Tables.Item(1).Cell(6, 6));
//第9行显示
wordDoc.Tables.Item(1).Cell(9, 4).Merge(wordDoc.Tables.Item(1).Cell(9, 6));
//第12行显示
wordDoc.Tables.Item(1).Cell(12, 2).Merge(wordDoc.Tables.Item(1).Cell(12, 6));
//第13行显示
wordDoc.Tables.Item(1).Cell(13, 2).Merge(wordDoc.Tables.Item(1).Cell(13, 6));
//第14行显示
wordDoc.Tables.Item(1).Cell(14, 2).Merge(wordDoc.Tables.Item(1).Cell(14, 6));

//第1行赋值
wordDoc.Tables.Item(1).Cell(1, 1).Range.Text = "职工编号:";
wordDoc.Tables.Item(1).Cell(1, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][0].ToString();
wordDoc.Tables.Item(1).Cell(1, 3).Range.Text = "职工姓名:";
wordDoc.Tables.Item(1).Cell(1, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][1].ToString();

//插入图片

if (p1 == 0)
{
string FileName = @"C:\22.bmp";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = wordDoc.Tables.Item(1).Cell(1, 5).Range; //指定图片插入的区域
//将图片插入到单元格中
wordDoc.Tables.Item(1).Cell(1, 5).Range.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
}
p1 = 0;

//第2行赋值
wordDoc.Tables.Item(1).Cell(2, 1).Range.Text = "民族类别:";
wordDoc.Tables.Item(1).Cell(2, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][2].ToString();
wordDoc.Tables.Item(1).Cell(2, 3).Range.Text = "出生日期:";
try
{
wordDoc.Tables.Item(1).Cell(2, 4).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][3]).ToShortDateString());
}
catch { wordDoc.Tables.Item(1).Cell(2, 4).Range.Text = ""; }
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][3]);
//第3行赋值
wordDoc.Tables.Item(1).Cell(3, 1).Range.Text = "年龄:";
wordDoc.Tables.Item(1).Cell(3, 2).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][4]);
wordDoc.Tables.Item(1).Cell(3, 3).Range.Text = "文化程度:";
wordDoc.Tables.Item(1).Cell(3, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][5].ToString();
//第4行赋值
wordDoc.Tables.Item(1).Cell(4, 1).Range.Text = "婚姻:";
wordDoc.Tables.Item(1).Cell(4, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][6].ToString();
wordDoc.Tables.Item(1).Cell(4, 3).Range.Text = "性别:";
wordDoc.Tables.Item(1).Cell(4, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][7].ToString();
//第5行赋值
wordDoc.Tables.Item(1).Cell(5, 1).Range.Text = "政治面貌:";
wordDoc.Tables.Item(1).Cell(5, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][8].ToString();
wordDoc.Tables.Item(1).Cell(5, 3).Range.Text = "单位工作时间:";
try
{
wordDoc.Tables.Item(1).Cell(5, 4).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[0][10]).ToShortDateString());
}
catch { wordDoc.Tables.Item(1).Cell(5, 4).Range.Text = ""; }
//第6行赋值
wordDoc.Tables.Item(1).Cell(6, 1).Range.Text = "籍贯:";
wordDoc.Tables.Item(1).Cell(6, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][24].ToString();
wordDoc.Tables.Item(1).Cell(6, 3).Range.Text = MyDS_Grid.Tables[0].Rows[i][25].ToString();
wordDoc.Tables.Item(1).Cell(6, 4).Range.Text = "身份证:";
wordDoc.Tables.Item(1).Cell(6, 5).Range.Text = MyDS_Grid.Tables[0].Rows[i][9].ToString();
//第7行赋值
wordDoc.Tables.Item(1).Cell(7, 1).Range.Text = "工龄:";
wordDoc.Tables.Item(1).Cell(7, 2).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][11]);
wordDoc.Tables.Item(1).Cell(7, 3).Range.Text = "职工类别:";
wordDoc.Tables.Item(1).Cell(7, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][12].ToString();
wordDoc.Tables.Item(1).Cell(7, 5).Range.Text = "职务类别:";
wordDoc.Tables.Item(1).Cell(7, 6).Range.Text = MyDS_Grid.Tables[0].Rows[i][13].ToString();
//第8行赋值
wordDoc.Tables.Item(1).Cell(8, 1).Range.Text = "工资类别:";
wordDoc.Tables.Item(1).Cell(8, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][14].ToString();
wordDoc.Tables.Item(1).Cell(8, 3).Range.Text = "部门类别:";
wordDoc.Tables.Item(1).Cell(8, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][15].ToString();
wordDoc.Tables.Item(1).Cell(8, 5).Range.Text = "职称类别:";
wordDoc.Tables.Item(1).Cell(8, 6).Range.Text = MyDS_Grid.Tables[0].Rows[i][16].ToString();
//第9行赋值
wordDoc.Tables.Item(1).Cell(9, 1).Range.Text = "月工资:";
wordDoc.Tables.Item(1).Cell(9, 2).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][26]);
wordDoc.Tables.Item(1).Cell(9, 3).Range.Text = "银行账号:";
wordDoc.Tables.Item(1).Cell(9, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][27].ToString();
//第10行赋值
wordDoc.Tables.Item(1).Cell(10, 1).Range.Text = "合同起始日期:";
try
{
wordDoc.Tables.Item(1).Cell(10, 2).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][28]).ToShortDateString());
}
catch { wordDoc.Tables.Item(1).Cell(10, 2).Range.Text = ""; }
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][28]);
wordDoc.Tables.Item(1).Cell(10, 3).Range.Text = "合同结束日期:";
try
{
wordDoc.Tables.Item(1).Cell(10, 4).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][29]).ToShortDateString());
}
catch { wordDoc.Tables.Item(1).Cell(10, 4).Range.Text = ""; }
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][29]);
wordDoc.Tables.Item(1).Cell(10, 5).Range.Text = "合同年限:";
wordDoc.Tables.Item(1).Cell(10, 6).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][30]);
//第11行赋值
wordDoc.Tables.Item(1).Cell(11, 1).Range.Text = "电话:";
wordDoc.Tables.Item(1).Cell(11, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][17].ToString();
wordDoc.Tables.Item(1).Cell(11, 3).Range.Text = "手机:";
wordDoc.Tables.Item(1).Cell(11, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][18].ToString();
wordDoc.Tables.Item(1).Cell(11, 5).Range.Text = "毕业时间:";
try
{
wordDoc.Tables.Item(1).Cell(11, 6).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][21]).ToShortDateString());
}
catch { wordDoc.Tables.Item(1).Cell(11, 6).Range.Text = ""; }
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][21]);
//第12行赋值
wordDoc.Tables.Item(1).Cell(12, 1).Range.Text = "毕业学校:";
wordDoc.Tables.Item(1).Cell(12, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][19].ToString();
//第13行赋值
wordDoc.Tables.Item(1).Cell(13, 1).Range.Text = "主修专业:";
wordDoc.Tables.Item(1).Cell(13, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][20].ToString();
//第14行赋值
wordDoc.Tables.Item(1).Cell(14, 1).Range.Text = "家庭地址:";
wordDoc.Tables.Item(1).Cell(14, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][22].ToString();

wordDoc.Range(ref start, ref end).InsertParagraphAfter();//插入回车
wordDoc.Range(ref start, ref end).ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //设置字体局中
}
}

Delphi word编辑的更多相关文章

  1. 问题-delphi无法编辑oracle表

    问题现象:在一次开发过程中,遇到了delphi无法编辑oracle表. 问题原因:原来是自己误操作控件的属性了. 问题处理: OraQuery1.ReadOnly:=False; OraQuery1. ...

  2. 如何用Word编辑参考文献------这是引用一位大师的

    如何用Word编辑参考文献修改文献是一件非常痛苦的事情,虽然现在也有很多软件可以编排参考文献,其实word本身就可以. 采用合适的编辑方法会方便地做到整齐,规范,自动排序和交叉引用.1.以尾注的方式插 ...

  3. 怎么让Word编辑公式又快又好

    现在很多办公学习都是在电脑中进行的.很多文件论文都是在Word中编写定稿以后再打印成册或者去投稿.毫无疑问,在Word中编辑各种各样的文字与符号是一项现在社会中非常必要的技能,而这其中一项就是对公式的 ...

  4. Delphi中编辑word

      其他(28)   //启动Word   try     wordapplication1.connect;   except     messagedlg('word may not be ins ...

  5. Delphi word

    [转载]在Delphi中使用CreateOleObject方法 (2011-08-24 14:20:47) 转载▼ 标签: 转载   原文地址:在Delphi中使用CreateOleObject方法作 ...

  6. 如何使用Excel和Word编辑和打印条形码

    本文介绍如何使用Microsoft Office Excel 2007和Microsoft Office Word 2007进行条形码的编辑后,通过普通的办公打印机将条形码打印出来. 对于少量,简单的 ...

  7. delphi edit编辑框使用

    Delphi编辑框Edit的用法 http://wenku.baidu.com/link?url=oKKm0VFBXexqiWt9ZNB8WxFGzwjJqRBM3ohrAy6GSMmOmwIzSWP ...

  8. 提高在word编辑公式的效率,及快捷键、对齐、编号问题

    1.     Word中编辑公式简介(重点看) https://jacobz.top/2017-08/WordMath/ 2.     快捷键 https://wenku.baidu.com/view ...

  9. Delphi 之 编辑框控件(TEdit)

    TEdit 组件主要用于数据的输入和显示和编辑等操作. AutoSelect 获取组件焦点.该属性只能在单行文本组件使用.值为True为选中.false则不选中. BorderStyle 设置编辑框控 ...

随机推荐

  1. Ubuntu环境下手动配置ant

    配置ant 1. 下载ant(http://ant.apache.org/bindownload.cgi) 例如我下载的是:apache-ant-1.9.4-bin.tar.gz 解压ant,将文件夹 ...

  2. eclipse 或MyEclipse将工程进行移动的时候会对@Override报错的处理方法

    有时候导入javaSE,javaEE,android 工程的时候,明明是刚刚用过的没有问题的工程,但重新导入的时候就报错. 提示The method ... must override a sperc ...

  3. Android 注意在finish Activity之后也要停止正在运行的请求

    如果在一个Activity里面启动了网络请求,而在这个网络请求还没返回结果的时候,如果Activity被结束了,则我们需要写如下代码作为防守: @Override public void onPost ...

  4. Thread的第三天学习

    线程互斥 public void method1() {  synchronized(this) {  ... } } 等同于 public synchronized void method1() { ...

  5. 欧拉工程第67题:Maximum path sum II

    By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...

  6. IOS 开发中判断字符串是否为空字符的方法

    NSUInteger是无符号的整型, NSInteger是有符号的整型,在表视图应用中常见 NSUInteger row= [indexPath row];因为这是显示tableViewCell有多少 ...

  7. 1、Web容器的理解&Tomcat的安装与配置

    Web容器的理解 <Java Web开发实战经典——基础篇>一书中对Web容器这一概念阐述得很好,借用其观点对Web容器加以理解: 想要运行一个Java Web的程序,则必须有相应的Web ...

  8. 实例学习Bloom Filter

    0. 科普1. 为什么需要Bloom Filter2. 基本原理3. 如何设计Bloom Filter4. 实例操作5. 扩展 0. 科普 Bloom Filter是由Bloom在1970年提出的一种 ...

  9. gdb 基本命令

    backtrace(或bt) 查看各级函数调用及参数 finish 连续运行到当前函数返回为止,然后停下来等待命令 frame(或f) 帧编号 选择栈帧 info(或i) locals 查看当前栈帧局 ...

  10. MacOS安装phpMyAdmin几点问题

    1. 登录时出现“#2002 无法登录 MySQL 服务器”. 原因: phpMyAdmin为PHP编写,MacOS默认安装的php配置,设置mysql监听socket默认为/var/mysql/my ...