c# 创建Excel com加载项图片对象批量操作

技术含量较低,主要是通过VBA代码转换成c#代码而来,从而实现图片批量插入、删除、另存为的批量操作,增加文档使用的通用性。

 

插件主要界面如下:

c# 创建Excel com加载项图片对象批量操作

 

主要代码如下:

 1   private void button8_Click(object sender, RibbonControlEventArgs e) 
 2         {
 3             if (checkBox4.Checked == true)
 4             {
 5                 覆盖图片名称插入();
 6             }
 7             else
 8             if (checkBox4.Checked == false)
 9             {
10                 excelapp = Globals.ThisAddIn.Application;
11                 excel.Worksheet wst = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
12                 excel.Range inrow = excelapp.InputBox("选择单元格[行]", Type: 8);
13                 excel.Range incol = excelapp.InputBox("选择单元格[列]", Type: 8);
14                 excel.Range oucol = excelapp.InputBox("选择单元格[列]", Type: 8);
15                 int in_row = inrow.Row;
16                 int in_col = incol.Column;
17                 int ou_col = oucol.Column;
18                 float PicLeft, PicTop, PicWidth, PicHeight;
19                 int n = wst.Cells[incol.Rows.Count, in_col].End(3).Row;
20                 MessageBox.Show("共:" + n.ToString() + "张图片需要插入");
21                 for (int i = 1; i < n + 1; i++)
22                 {
23                     if (incol.Columns.Count > 1 || inrow.Rows.Count > 1 || oucol.Columns.Count > 1)
24                     {
25                         MessageBox.Show("所有的选择:只能为1行或者1列");
26                         break;
27                     }
28                     string str = Convert.ToString(wst.Cells[i, in_col].Value2);
29                     if (str == null || str == string.Empty)
30                     {
31                         continue;
32                     }
33                     wst.Cells[i, ou_col].RowHeight = editBox2.Text;
34                     wst.Cells[i, ou_col].ColumnWidth = editBox1.Text;
35                     string filename = Settings.Default.图片插入文件夹路径 + "\\" + wst.Cells[i, in_col].Value2;
36                     PicLeft = Convert.ToSingle(wst.Cells[i, ou_col].Left);
37                     PicTop = Convert.ToSingle(wst.Cells[i, ou_col].Top);
38                     PicWidth = Convert.ToSingle(wst.Cells[i, ou_col].Width);
39                     PicHeight = Convert.ToSingle(wst.Cells[i, ou_col].Height);
40                     if (checkBox2.Checked == true)
41                     {
42                         wst.Shapes.AddPicture(filename, MsoTriState.msoFalse, MsoTriState.msoTrue, PicLeft, PicTop, -1, -1);
43                         float r;
44                         foreach (excel.Shape shp in wst.Shapes)
45                         {
46                             if (shp.Type == MsoShapeType.msoPicture)
47                             {
48                                 float rh = PicHeight / shp.Height;
49                                 float rw = PicWidth / shp.Width;
50                                 if (rw > rh)
51                                 {
52                                     r = rh;
53                                 }
54                                 else
55                                 {
56                                     r = rw;
57                                 }
58                                 //shp.Left = PicL;
59                                 //shp.Top = PicT;
60                                 shp.ScaleWidth(r, MsoTriState.msoFalse, MsoScaleFrom.msoScaleFromTopLeft);
61                             }
62                         }
63                     }
64                     else
65                     {
66                         wst.Shapes.AddPicture(filename, MsoTriState.msoFalse, MsoTriState.msoTrue, PicLeft, PicTop, PicWidth, PicHeight);
67                     }
68                 }
69                 if (checkBox1.Checked == true)
70                 {
71                     跟随单元格();
72                 }
73                 else
74                 {
75                     不随单元格();
76                 }
77             }
78         }

 

上一篇:LDAP


下一篇:dsquery 命令 查询 AD