当前位置: 首页 > news >正文

WPF窗口读取、显示、修改、另存excel文件——CAD c#二次开发

效果如下:

 

using System.Data;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using ExcelDataReader;
using System.Text;
using ClosedXML.Excel;

namespace IfoxDemo
{
    public partial class SimpleWindow : Window
    {
        public SimpleWindow()
        {
            InitializeComponent();
        }

        private void OnSelectFileButtonClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                string filePath = openFileDialog.FileName;
                System.Data.DataTable dataTable = ReadExcelFile(filePath);
                //dataGrid.HeadersVisibility = DataGridHeadersVisibility.None; // Hide the header row
                dataGrid.ItemsSource = dataTable.DefaultView;
            }
        }

        private void OnSaveFileButtonClick(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog
            {
                Filter = "Excel Files|*.xlsx",
                DefaultExt = "xlsx"
            };

            if (saveFileDialog.ShowDialog() == true)
            {
                string filePath = saveFileDialog.FileName;
                SaveDataGridToExcel(filePath);
            }
        }

        private System.Data.DataTable ReadExcelFile(string filePath)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    var result = reader.AsDataSet();
                    return result.Tables[0];
                }
            }
        }

        private void SaveDataGridToExcel(string filePath)
        {
            var dataTable = ((DataView)dataGrid.ItemsSource).ToTable();
            // Remove the first row
      

            using (var workbook = new XLWorkbook())
            {
                var worksheet = workbook.Worksheets.Add(dataTable, "Sheet1");
                workbook.SaveAs(filePath);
            }
        }
    }
}
<Window x:Class="IfoxDemo.SimpleWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SimpleWindow" Height="500" Width="600">
    <Grid>
        <StackPanel>
            <Button Content="选择Excel文件" Margin="10" Click="OnSelectFileButtonClick"/>
            <Button Content="保存修改后的数据" Margin="10" Click="OnSaveFileButtonClick"/>
            <DataGrid Name="dataGrid" AutoGenerateColumns="True" Margin="10" 
                      HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" 
                      Height="300" CanUserResizeColumns="True" />
        </StackPanel>
    </Grid>
</Window>

 

 

using System.Collections;
using System.Collections.Generic;
using static IFoxCAD.Cad.OpFilter;

namespace IfoxDemo
{
    public class 生成迷宫
    {
        [CommandMethod("xx")]
        public void Demo()
        {
            using var tr = new DBTrans();
            tr.Database.EraseAll();
            Env.Editor.WriteMessage("开始生成迷宫1");
            if (System.Windows.Application.Current == null)
            {
                new System.Windows.Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
            }

            // 显示WPF窗口
            System.Windows.Application.Current.Dispatcher.Invoke(() =>
            {
                var window = new SimpleWindow();
                window.ShowDialog();
            });
        }
    }
  
}

http://www.dtcms.com/a/68280.html

相关文章:

  • TEXT()的作用
  • 杨辉三角形(信息学奥赛一本通-2043)
  • C、C++打印地址用%u
  • DeepSeek面试——分词算法
  • 搭建基于flask的web应用框架
  • 源代码防泄漏之反向沙箱篇
  • 射频相关概念
  • 利用余弦相似度在大量文章中找出抄袭的文章
  • Peach配置文件中<Agent>模块的作用及参数解析
  • 分页查询的实现
  • ELK(Elasticsearch、Logstash、Kbana)安装及Spring应用
  • 【大模型】知识蒸馏(knowledge distillation)算法详解
  • Oracle中的INHERIT PRIVILEGES权限
  • 【搭建环境】windows环境redis\集群;rabbitmq
  • 通过mybatis的拦截器对SQL进行打标
  • 【GIT】什么是GitHub Actions ?
  • 【数据分享】1999—2023年地级市地方一般公共预算收支状况数据(科学技术支出/教育支出等)
  • python--面试题--基础题
  • Python的字符串优雅优化策略:特定编码 -> Unicode码点 -> UTF-8(可自定义)
  • MySQL的事务机制
  • Caused by: java.lang.ClassNotFoundException: jakarta.servlet.ServletContext
  • 每日一题——点击消除
  • 从零基础到能独立设计单片机产品,一般需要经历哪些学习阶段?
  • 网页制作代码html制作一个网页模板
  • [S32K]SPI
  • 结构型模式之适配器模式:让不兼容的接口兼容
  • 某乎x-zse-96加密算法分析与还原
  • 落雪音乐Pro 8.8.6 | 内置8条音源,无需手动导入,纯净无广告
  • Go string 字符串底层逻辑
  • MessageBox()