合并Excel文档WPF C# [英] Merging excel documents WPF C#
问题描述
我正在尝试创建一个简单的程序来合并excel文件并且无法单独加载ListBox中的文件。
用户可以选择多个出现的文件进入ListBox,然后当点击合并时,会生成一个新文件,其名称在侧面的TextBox中给出。
我的问题是当我来加载要从ListBox合并的文件。
I am trying to make a simple program to merge excel files and having trouble loading the files in the ListBox individually.
The user can select multiple file which appear into a ListBox, then when merge is clicked a new file is generated with the name given in the TextBox at the side.
My problem is when i come to load the files to merge from the ListBox.
btnMergeFile_Click(object sender, RoutedEventArgs e)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"filename.xlsx");
Workbook workbook2 = new Workbook();
workbook2.LoadFromFile(@"filename.xlsx");
}
是否可以单独调用列表名称?
对不起我是新的c#和wpf。
这里是字面上的所有代码!
XAML
Is it possible to call the list names individually?
Sorry I'm new to c# and wpf.
Here is literally all the code!
XAML
<DockPanel Margin="10">
<WrapPanel HorizontalAlignment="Center" DockPanel.Dock="Top" Margin="0,0,0,10">
Select Files
Merge Files
Clear Files
<TextBox x:Name="newFileName" TextAlignment="Left" HorizontalAlignment="Center" Width="150" Text="New File Name"/>
<ListBox x:Name="listBox1" />
</DockPanel>
.CS
.CS
using System;
using System.Windows;
using Microsoft.Win32;
using System.Data;
using Spire.Xls;
namespace ExcelMerge_1._1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void btnSelectFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Filter = "csv files (*.csv)|*.csv|Excel files (*.XLSX)|*.XLSX";
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog() == true)
{
foreach (string filename in openFileDialog.FileNames)
listBox1.Items.Add(System.IO.Path.GetFullPath(filename));
}
}
public void btnMergeFile_Click(object sender, RoutedEventArgs e)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"filename.xlsx");
Workbook workbook2 = new Workbook();
workbook2.LoadFromFile(@"filename.xlsx");
Worksheet sheet2 = workbook2.Worksheets[0];
DataTable dataTable = sheet2.ExportDataTable();
Worksheet sheet1 = workbook.Worksheets[0];
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
workbook.SaveAsXml(newFileName.Text);
}
private void btnClearFile_Click(object sender, RoutedEventArgs e)
{
listBox1.Items.Clear();
}
}
}
我尝试了什么:
首次尝试interlop并且无法正常工作因此决定尝试Spire,看起来很有希望然而这个简单的障碍让我感到高兴!
What I have tried:
First tried interlop and couldn't get that working so decided to try Spire, looks promising however this simple hurdle is getting me!
推荐答案
是否可以单独调用列表名称?是。这是一种更简单的方法:
Is it possible to call the list names individually? Yes. Here is an easier way of doing it:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public ObservableCollection<XlFile> XlFiles { get; }
= new ObservableCollection<XlFile>();
public void btnSelectFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Filter =
"csv files (*.csv)|*.csv|Excel files (*.XLSX)|*.XLSX";
openFileDialog.InitialDirectory =
Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog() == true)
{
foreach (string filename in openFileDialog.FileNames)
XlFiles.Add(new XlFile
{
Name = System.IO.Path.GetFileName(filename),
FullPath = filename
});
}
}
public void btnMergeFile_Click(object sender, RoutedEventArgs e)
{
foreach (var xlFile in XlFiles)
{
// do work here...
}
}
}
public class XlFile
{
public string Name { get; set; }
public string FullPath { get; set; }
}
和ListBox的Xaml:
And the Xaml for the ListBox:
<ListBox ItemsSource="{Binding XlFiles}" DisplayMemberPath="Name"/>
这篇关于合并Excel文档WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!