Click here to Skip to main content
14,981,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a fileUpload to upload Excel file, after Uploading i want to insert all data in my SQL Database or dataset in WPF

My code for File Uploading..

private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
ofd.Filter = "AllFiles|*.*";

if ((bool) ofd.ShowDialog()==true)
{
string FileName = ofd.FileName;
txtFileName.Text = FileName;
}
}

So please help me how to insert data in SQL database or DataSet or DataTable from Excel file in WPF Application....
Posted

1 solution

You can sue below code to export excel to datatable in wpf:

C#
namespace dataexport

{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
          Workbook workbook = new Workbook();
          workbook.LoadFromFile(@"D:\dataexport.xls", ExcelVersion.Version97to2003);  
          Worksheet sheet = workbook.Worksheets[0];
          DataTable dataTable = sheet.ExportDataTable();
          DataView view = new DataView(dataTable);
          this.dataGrid1.ItemsSource = view;
          this.dataGrid1.AutoGenerateColumns = true;
        }
    }
}


but you have to download a .net excel[^]component.Hope it works.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900