当前位置: 移动技术网 > IT编程>开发语言>.net > c# 创建Excel com加载项图片对象批量操作

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

2019年04月17日  | 移动技术网IT编程  | 我要评论

库存服装,蔷花红莲下载,三峡之窗欢迎您

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

 

插件主要界面如下:

 

主要代码如下:

 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         }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网