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);
        }
    }
}
相关推荐
Edingbrugh.南空35 分钟前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
CodeCraft Studio1 小时前
借助Aspose.HTML控件,在 Python 中将 HTML 转换为 Markdown
开发语言·python·html·markdown·aspose·html转markdown·asposel.html
QQ_4376643141 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
aramae1 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
封奚泽优2 小时前
使用Python实现单词记忆软件
开发语言·python·random·qpushbutton·qtwidgets·qtcore·qtgui
Goona_2 小时前
拒绝SQL恐惧:用Python+pyqt打造任意Excel数据库查询系统
数据库·python·sql·excel·pyqt
忘忧记2 小时前
excel删除重复项场景
excel
liulilittle3 小时前
C++/CLI与标准C++的语法差异(一)
开发语言·c++·.net·cli·clr·托管·原生
★YUI★3 小时前
学习游戏制作记录(剑投掷技能)7.26
学习·游戏·unity·c#
daixin88483 小时前
什么是缓存雪崩?缓存击穿?缓存穿透?分别如何解决?什么是缓存预热?
java·开发语言·redis·缓存