zoukankan      html  css  js  c++  java
  • C#删除Excel整行,更新单元格内容

    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();        
            }
        }
    }
    

      

  • 相关阅读:
    《剑指offer》面试题7:旋转数组的最小数字
    eclipse ------ TODO、FIXME、XXX 等任务标记
    RT-Thread ------ waitqueue
    RT-Thread ------ 设备注册
    ubuntu 安装 glibc
    openwrt上面移植MQTT代码
    MH5000-31模组无法识别SIM卡
    "Hello osmdroid World"手机GPS轨迹数据
    地质数据下载
    绘图软件Surfer绘制等高线
  • 原文地址:https://www.cnblogs.com/soundcode/p/13804983.html
Copyright © 2011-2022 走看看