EXCEL与SQL数据库间导入导出之傻瓜闲扯淡

本博客展示了如何在Windows应用中通过Aspose.Cells库实现Excel数据的导出和导入功能,包括从数据库获取数据并导出为Excel,以及从Excel文件中读取数据并导入到数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.xaml.cs


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Aspose.Cells;
using Microsoft.Win32;

namespace EXCELexportAndLeadIn
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private const string Connstr = "server=127.0.0.1;uid=sa;pwd=123456;database=Music";
        public MainWindow()
        {
            InitializeComponent();
        }

        public DataTable GetDataTable(string str)
        {
            SqlConnection conn = new SqlConnection(Connstr);
            SqlDataAdapter sda = new SqlDataAdapter(@"SELECT * FROM " + str + "", conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dt.Dispose();
            return dt;
        }
        private void Export(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";

            if (openFileDialog.ShowDialog() == true)
            {
                Workbook wbWorkbook = new Workbook(openFileDialog.FileName);
                Worksheet wsWorksheet = wbWorkbook.Worksheets[0];

                int maxColumn = wsWorksheet.Cells.MaxColumn + 1;
                int maxRow = wsWorksheet.Cells.MaxRow + 1;

                DataTable exportDataTable = wsWorksheet.Cells.ExportDataTable(0, 0, maxRow, maxColumn,true);

                MyDataGrid.ItemsSource = null;
                MyDataGrid.Columns.Clear();
                MyDataGrid.AutoGenerateColumns = true;
                MyDataGrid.ItemsSource = exportDataTable.DefaultView;

                //将数据导入数据库

                SqlConnection Con=new SqlConnection(Connstr);
                Con.Open();
                SqlBulkCopy sqlBulkCopy=new SqlBulkCopy(Con);
                sqlBulkCopy.DestinationTableName = "EXCELtest";
                sqlBulkCopy.WriteToServer(exportDataTable);
                Con.Close();
            }
        }

        private void LeadIn(object sender, RoutedEventArgs e)
        {
            DataTable dtDataTable = GetDataTable("EXCELtest");

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";

            if (openFileDialog.ShowDialog() == true)
            {

                var workbook = new Workbook();
                if (workbook.Worksheets.Count == 0)
                {
                    workbook.Worksheets.Add("Sheet1");
                }
                Worksheet worksheet = workbook.Worksheets[0];

                MyDataGrid.ItemsSource = null;
                MyDataGrid.Columns.Clear();
                MyDataGrid.AutoGenerateColumns = true;
                MyDataGrid.ItemsSource = dtDataTable.DefaultView;

                worksheet.AutoFitColumns();
                int importDataTable = worksheet.Cells.ImportDataTable(dtDataTable, false, 0, 0);
                workbook.Save(openFileDialog.FileName);
                Process.Start(openFileDialog.FileName);
            }
        }
    }
}

.xaml


<Window x:Class="EXCELexportAndLeadIn.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="80*"/>
            <RowDefinition Height="500*"/>
        </Grid.RowDefinitions>
       
            <DataGrid></DataGrid>
        <Button Content="导入" Click="Export" Grid.Row="0" HorizontalAlignment="Left" Margin="100,26,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="导出" Click="LeadIn" Grid.Row="0" HorizontalAlignment="Left" Margin="275,26,0,0" VerticalAlignment="Top" Width="75"/>
        <DataGrid Name="MyDataGrid" Grid.Row="1"></DataGrid>
    </Grid>
</Window>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值