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);
        }
    }
}
相关推荐
流水迢迢lst1 小时前
靶场练习day15--php反序列化
开发语言·php
fengci.1 小时前
ctfshow(web入门)279-286
java·开发语言·学习
TON_G-T1 小时前
javascript中 Iframe 处理多端通信、鉴权
开发语言·前端·javascript
周淳APP1 小时前
【JS之闭包防抖节流,this指向,原型&原型链,数据类型,深浅拷贝】简单梳理啦!
开发语言·前端·javascript·ecmascript
ok_hahaha2 小时前
java从头开始-苍穹外卖day05-Redis及店铺营业状态设置
java·开发语言·redis
2501_933329552 小时前
舆情监测系统的技术演进:从数据采集到AI中台,Infoseek如何实现“监测+处置”一体化
开发语言·人工智能·自然语言处理·系统架构
dgvri2 小时前
Windows上安装Go并配置环境变量(图文步骤)
开发语言·windows·golang
我是唐青枫2 小时前
C#.NET ReadOnlySequence 深入解析:多段内存遍历与零拷贝协议解析
网络·c#·.net
222you2 小时前
Java 并发编程(1)
java·开发语言
C++ 老炮儿的技术栈2 小时前
Linux 文件系统目录架构全解析
linux·服务器·c语言·开发语言·c++