Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用按钮浏览Excel文件和导入_C#_Linq_Import - Fatal编程技术网

C# 如何使用按钮浏览Excel文件和导入

C# 如何使用按钮浏览Excel文件和导入,c#,linq,import,C#,Linq,Import,我有两个按钮(LoadFile和ImportFile)。我想从directy中单击load file and browser for Excel file,然后单击Import按钮,以便将数据加载到数据库中的表中。Excel表格称为“银行对账单”,有1000多行。我以前没有这样做过,我使用LINQtoSQL来做这件事 这是我的代码,但我不知道如何继续或得到的方式。谁能帮帮我吗。谢谢 装载 public void LoadExcel() { string _Load

我有两个按钮(LoadFile和ImportFile)。我想从directy中单击load file and browser for Excel file,然后单击Import按钮,以便将数据加载到数据库中的表中。Excel表格称为“银行对账单”,有1000多行。我以前没有这样做过,我使用LINQtoSQL来做这件事

这是我的代码,但我不知道如何继续或得到的方式。谁能帮帮我吗。谢谢

装载

    public void LoadExcel()
    {
        string _LoadPath = @"C:\NewFNBFile1.xls";
        TextBox1.Text = _LoadPath.ToString();

        var _File = new ExcelQueryFactory(_LoadPath);

        _File.AddMapping("Number", "Number");
        _File.AddMapping("DateReceived", "DateReceived");
        _File.AddMapping("Description1", "Description1"
        _File.AddMapping("Description2", "Description2");
        _File.AddMapping("Description3", "Description3");
        _File.AddMapping("Amount", "Amount");
        _File.AddMapping("Balance", "Balance");
        _File.AddMapping("AccruedCharges", "AccruedCharges");

    }

    protected void btnBroswer_Click(object sender, EventArgs e)
    {
        LoadExcel();
    }
进口

    protected void btnImport_Click(object sender, EventArgs e)
    {


        var _list = new ExcelQueryFactory(@"C:\NewFNBFile1.xls");
        var _Show = from x in _dc.TESTING_NewBankFile1_s
                    where x ["Description3"] == "A19C28425645285"
                        select new
                        {
                            Number = x["Number"],
                            DateReceived = x["DateReceived"],
                            Description1 = x["Description1"],
                            Description2 = x["Description2"],
                            Description3 = x["Description3"],
                            Amount = x["Amount"],
                            Balance = x["Balance"],
                            AccruedCharges = x["AccruedCharges"],                               
                        }; 

    }
请参考下面的c代码


谢谢,复制完这个方法后,在导入按钮下放哪段代码?只需调用GetTable方法(“Test.xls”、“Sheet1”、“DTResult”)
public System.Data.DataTable GetTable(string filename, string SheetName, string outTableName)
{
    OleDbConnection oleConn = null;
    OleDbDataAdapter oleAdapter = null;
    try
    {
        string Con = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                     @"Data Source=" + filename + ";" +
                     @"Extended Properties=" + Convert.ToChar(34).ToString() +
                     @"Excel 12.0;" + "Imex=1;" + "HDR=Yes;" + Convert.ToChar(34).ToString() ;

        oleConn = new OleDbConnection(Con);
        oleConn.Open();
        OleDbCommand oleCmdSelect = new OleDbCommand();
        oleCmdSelect = new OleDbCommand(
                @"SELECT * FROM ["
                + SheetName
                + "$" + "]", oleConn);
        oleAdapter = new OleDbDataAdapter();
        oleAdapter.SelectCommand = oleCmdSelect;
        System.Data.DataTable dt = new System.Data.DataTable(outTableName);
        oleAdapter.FillSchema(dt, SchemaType.Source);
        oleAdapter.Fill(dt);
        oleCmdSelect.Dispose();
        oleCmdSelect = null;
        oleAdapter.Dispose();
        oleAdapter = null;
        oleConn.Dispose();
        oleConn = null;              
        return dt;
    }
}