编程语言
首页 > 编程语言> > C# 操作excel基础方法

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.Threading.Tasks;
using System.Windows.Forms;
using ClosedXML.Excel;

namespace cwexcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        XLWorkbook g_wb = new XLWorkbook();

        private void button1_Click(object sender, EventArgs e)
        {
            g_wb.AddWorksheet("mysheet01");
            g_wb.SaveAs(@"D:\GOPATH\src\wei.com\cwexcel\123.xlsx");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XLWorkbook g_wb = new XLWorkbook(@"D:\GOPATH\src\wei.com\cwexcel\123.xlsx");
            IXLWorksheet sheet = g_wb.Worksheet(1);
            sheet.Cell(Convert.ToInt32(textBox1.Text),Convert.ToInt32(textBox2.Text)).Value=textBox3.Text;
            g_wb.Save();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            XLWorkbook g_wb = new XLWorkbook(@"D:\GOPATH\src\wei.com\cwexcel\123.xlsx");
            IXLWorksheet sheet = g_wb.Worksheet(1);
            g_wb.AddWorksheet();
            sheet.Delete();
            g_wb.Save();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            XLWorkbook g_wb = new XLWorkbook(@"D:\GOPATH\src\wei.com\cwexcel\123.xlsx");
            IXLWorksheet sheet = g_wb.Worksheet(1);
            sheet.Cell(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)).Value = textBox3.Text;
            sheet.Cell(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)).Style.Fill.BackgroundColor = XLColor.Blue;
            sheet.Cell(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)).Style.Font.FontColor = XLColor.Red;
            g_wb.Save();
        }

    }
}

标签:sheet,wb,C#,Text,excel,System,using,操作,ToInt32
来源: https://blog.csdn.net/m0_46426259/article/details/115363279