c#操作excel

说明

vs2022开发,调用excel

代码

csharp 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace ExcelReplace
{
    public partial class Form1 : Form
    {
        private Microsoft.Office.Interop.Excel.Application app;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnPath_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.tbPath.Text = this.openFileDialog1.FileName;
            }
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            if (tbPath.Text.Trim().Length == 0)
            {
                MessageBox.Show("请选择Excel文件");
                return; 
            }

            //替换
            app=new Microsoft.Office.Interop.Excel.Application();
            app.Visible = true;

            var wb=app.Workbooks.Open(tbPath.Text);
            foreach (Worksheet ws in wb.Worksheets)
            {
                //使用的行数
                var rowCount =ws.UsedRange.Rows.Count;
                //使用的列数
                var colCount=ws.UsedRange.Columns.Count;

                for (int row = 1; row <= rowCount; row++)
                {
                    for (int col = 1; col <= colCount; col++)
                    {
                        Range c=ws.Cells[row, col];
                        string s = c.Text;

                        if (s.Contains("亿"))
                        {
                            string s1 = "=" + s.Replace("亿", "*10000");

                            c.Formula = s1;
                        } else if (s.Contains("万"))
                        {
                            string s1 = s.Replace("万", "");

                            c.Value = s1;
                        }
                    }
                }
            }


            //保存文件
            FileInfo f=new FileInfo(tbPath.Text);
            string path = f.DirectoryName+"\\" + f.Name.Replace(f.Extension, "") + DateTime.Now.ToString("yyyyMMddHHmmss") + f.Extension;
            wb.SaveAs(path);

            //关闭
            wb.Close();

            //退出excel
            app.Quit();

            MessageBox.Show("处理完成,保存文件为:" + path);
        }
    }
}
相关推荐
147API6 分钟前
Python批量生成蒸馏数据,并发、重试、幂等和断点续跑
开发语言·jvm·python
青 春 记 忆8 分钟前
零基础入门python04:用注册校验理解字典、集合和条件判断
开发语言·vscode·python·python3.11
kels889912 分钟前
实战排坑:黄金实时API开发,XAUUSD Tick报文异常处理实践
开发语言·python·websocket·网络协议·信息可视化
阿pin16 分钟前
Java随笔-JDK7 HashMap头插法为何能导致死循环?
java·开发语言·hashmap
FBI HackerHarry浩18 分钟前
Pandas 库中用于分类变量独热编码(One-Hot Encoding)的函数 pd.get_dummies() 函数
开发语言·人工智能·python·pandas
名字还没想好☜33 分钟前
Go 的 sync.Cond 实战:用条件变量做等待/通知,比忙轮询省 CPU
开发语言·数据库·后端·golang·go
唐青枫1 小时前
别再手写 args[0]:C#.NET CommandLineParser 命令行工具实战详解
c#·.net
运维行者_1 小时前
企业带宽监控工具实战:网络流量分析与异常排查的5个关键能力
运维·服务器·开发语言·网络·分布式·后端·php
Patrick在香港1 小时前
Python爬虫框架实战:优雅抓取香港政府公开API数据的通用方案
java·开发语言·爬虫·python·ai编程·数据可视化·可用性测试
hey_sml1 小时前
MySQL常用操作速记:从字符集到游标全解析
java·开发语言·mysql