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");
}
Is it possible to call the list names individually?
Sorry I'm new to c# and wpf.
XAML
<DockPanel Margin="10">
<WrapPanel HorizontalAlignment="Center" DockPanel.Dock="Top" Margin="0,0,0,10">
<Button x:Name="btnSelectFile" Width="75" Height="30" Margin="5" Click="btnSelectFile_Click">Select Files</Button>
<Button x:Name="btnMergeFile" Width="75" Height="30" Margin="5" Click="btnMergeFile_Click">Merge Files</Button>
<Button x:Name="btnClearFile" Width="75" Height="30" Margin="5" Click="btnClearFile_Click">Clear Files</Button>
<TextBox x:Name="newFileName" TextAlignment="Left" HorizontalAlignment="Center" Width="150" Text="New File Name"/>
</WrapPanel>
<ListBox x:Name="listBox1" />
</DockPanel>
</Grid>
.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();
}
}
}