C# - 如何从INI文件路径读取excel文件。 [英] C# - how read excel file from INI file path.

查看:93
本文介绍了C# - 如何从INI文件路径读取excel文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi,

I am doing some calculation based on my excel input.

This Excel user can select from the particular path and then select the file.

But, now client wants, they don't want to select excel file from the path.

Instead of that, they will mention the file path in the INI file.

While loading the form, first it should read the INI File and then read and convert into datatable.

I am new to use INI File.

Can anyone suggest how to write code to read the file from INI and convert those file into datatable

See my current code below..





我尝试过:





What I have tried:

private void btnbrowse_Click(object sender, EventArgs e)
  {
            this.openFileDialog1.Filter = "Excel file (*.xls;)|*.xlsx";
            this.openFileDialog1.Multiselect = true;
            this.openFileDialog1.Title = "Select Excel";
            DialogResult dr = this.openFileDialog1.ShowDialog();
            DataTable dataTable1 = new DataTable();
            
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                foreach (String file in openFileDialog1.FileNames)
                {
                    Excelname = file;
                    txtXLfile.Text = Excelname;
                }
            }
            dtproject = getXLDatatable(Excelname, dataTable1);

            DataRow dr1;

            dr1 = dtproject.NewRow();
            dr1.ItemArray = new object[] { 0, "Select Project" };
            dtproject.Rows.InsertAt(dr1, 0);
            cmbProject.ValueMember  = "EmployeeName";
            cmbProject.DisplayMember = "EmployeeName";
            cmbProject.DataSource = dtproject;  
    }

    private static DataTable getXLDatatable(string XLbroname, DataTable dataTable1)
        {
           
            FileStream fstream1 = new FileStream(XLbroname, FileMode.Open);
          
            Workbook workbook1 = new Workbook(fstream1);
            Worksheet worksheet1 = workbook1.Worksheets[0];
            dataTable1.AcceptChanges();
            DataSet ds1 = new DataSet();
            DataTable dt2 = new DataTable();
            ds1.Tables.Add(dataTable1);
            dataTable1 = worksheet1.Cells.ExportDataTable(1, 0, worksheet1.Cells.MaxRow + 1, worksheet1.Cells.MaxColumn + 1);
            dataTable1.TableName = "tableXL";
            dataTable1.Columns[0].ColumnName = "Id";
            dataTable1.Columns[1].ColumnName = "EmployeeName";
            dataTable1.Columns[2].ColumnName = "Dept";
            dataTable1.Columns[3].ColumnName = "Section";

            dt2 = dataTable1;
            return dt2;
        }

推荐答案

C#read ini file- Google Search [ ^ ]


这篇关于C# - 如何从INI文件路径读取excel文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆