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);
        }
    }
}
相关推荐
石山代码6 分钟前
C++ 内存分区 堆区
java·开发语言·c++
无风听海26 分钟前
C# 隐式转换深度解析
java·开发语言·c#
LateFrames1 小时前
520 - 如何说晚安 (WPF)
c#·wpf·浪漫·ui体验
一只大袋鼠1 小时前
Git 进阶(二):分支管理、暂存栈、远程仓库与多人协作
java·开发语言·git
LuminousCPP2 小时前
数据结构 - 线性表第四篇:C 语言通讯录优化升级全记录(踩坑 + 思考)
c语言·开发语言·数据结构·经验分享·笔记·学习
魔法阵维护师2 小时前
从零开发游戏需要学习的c#模块,第十四章(保存和加载)
学习·游戏·c#
web3.08889992 小时前
1688 图搜接口(item_search_img / 拍立淘) 接入方法
开发语言·python
один but you3 小时前
从可变参数到 emplace:现代 C++ 性能优化的核心组合
java·开发语言
MY_TEUCK4 小时前
【Java 后端 | Nacos 注册中心】微服务治理原理、选型与注册发现实战
java·开发语言·微服务
测试员周周4 小时前
【Appium 系列】第13节-混合测试执行器 — API + UI 的协同执行
开发语言·人工智能·python·功能测试·ui·appium·pytest