c# 创建Excel com加载项图片对象批量操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# 创建Excel com加载项图片对象批量操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3986字,纯文字阅读大概需要6分钟。
内容图文
![c# 创建Excel com加载项图片对象批量操作](/upload/InfoBanner/zyjiaocheng/833/0864f22ccf644aba820ff32a21705801.jpg)
技术含量较低,主要是通过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 }
内容总结
以上是互联网集市为您收集整理的c# 创建Excel com加载项图片对象批量操作全部内容,希望文章能够帮你解决c# 创建Excel com加载项图片对象批量操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。