C#删除Excel整行,更新单元格内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; using Excel = Microsoft.Office.Interop.Excel; namespace DelExcel { public partial class Form1 : Form { private Excel._Application excelApp = null ; private Excel.Workbook book = null ; private Excel.Worksheet sheet = null ; private Excel.Range range = null ; string filePath = @"E:\2010.xls" ; public Form1() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e) { delExcel(filePath); } private void delExcel( string filePath) { excelApp = new Microsoft.Office.Interop.Excel.Application(); excelApp.Visible = false ; book = excelApp.Workbooks.Open(filePath, Missing.Value, false , Missing.Value, Missing.Value, Missing.Value, true , Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); int wsCount = book.Worksheets.Count; for ( int i = 1; i <= wsCount; i++) { sheet = (Excel.Worksheet)book.Worksheets[i]; //获取编辑范围 range = (Excel.Range)sheet.Rows[1, Missing.Value]; //删除整行 range.Delete(Excel.XlDirection.xlDown); //更新单元格内容 sheet.Cells[1, 3] = "pcID" ; //保存编辑 book.Save(); } //关闭book book.Close(Missing.Value, Missing.Value, Missing.Value); //退出excel application,可以将前面的excelApp.Visible = false改为excelApp.Visible = true看看; excelApp.Workbooks.Close(); excelApp.Quit(); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步