将文件数据excel到datatable中

时间:2012-11-15 11:41:42

标签: c# excel ole

我正在使用下面的代码从excel文件中选择所有数据,并想知道我是否可以从第三行开始并读取文件中的其余数据..

excelConnectionString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFileName + ";Extended Properties=Excel 12.0";
            // Create Connection to Excel Workbook
            using (OleDbConnection excelConnection =
                new OleDbConnection(excelConnectionString))
                {                    
                    excelConnection.Open();
                    System.Data.DataTable dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    string[] excelSheet = new String[dt.Rows.Count];
                    int sheet = 0;
                    foreach (DataRow row in dt.Rows)
                    {
                        excelSheet[sheet] = row["Table_Name"].ToString();
                        sheet++;

                    }
                    excelDataTable.Clear();
                    for (int i = 0; i < excelSheet.Length; i++)
                    {
                        OleDbCommand command = new OleDbCommand
                             ("Select  * FROM [" + excelSheet[i] + "]", excelConnection);

                        excelAdapter.SelectCommand = command;
                        excelAdapter.Fill(excelDataTable);
                    }
                    excelConnection.Close();
                }

            return excelDataTable;

3 个答案:

答案 0 :(得分:0)

在使用Oledbconnection阅读之前,您可以使用类似的内容修改您的文件: -

private string customiseTemplate(string filename)
    {
        try
        {
            //Create Instance of Excel Appllication
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

            //Create a WorkBook Object.
            Workbooks wbks = app.Workbooks;

            //Populate Workbook from Excel File.
            _Workbook _wbk = wbks.Add(filename);

            //Create Sheet Object.
            Sheets shs = _wbk.Sheets;

            //Determine which sheet to work with. In this instance there should only ever be one. 
            _Worksheet _wsh = (_Worksheet)shs.get_Item(1);



            //Delete Unwanted Rows 1 to 3.

            Microsoft.Office.Interop.Excel.Range
            range1 = (Range)_wsh.get_Range("A1:A3", Missing.Value);

            range1.EntireRow.Delete(Missing.Value);

            // Release Range
            System.Runtime.InteropServices.Marshal.ReleaseComObject(range1);

            //Save WorkBook.

            app.AlertBeforeOverwriting = false;

            string newFile = filename.Insert(3, "Ammended_");

            _wbk.SaveAs(newFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Missing.Value,
            Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            // CLose Excel App.
            app.Quit();

            // Release unnecessary excel processes
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
            app = null;

            return newFile;

        }
        catch(Exception x)
        {
            MessageBox.Show(x.Message);
        }

        return null;

    }

答案 1 :(得分:0)

var w = sqlData.AsEnumerable().Where(data => data.Field<String>("slideNo") == "5")
                .Select(data=> data.Field<String>("QuestionStartText"));

这是编码的答案......

答案 2 :(得分:0)

德里克是对的,我同意他的看法。如果您仍想保留原始Excel文件数据并仅显示数据表中的一部分,则需要先将总Excel数据导出到datatable,然后删除数据表中不需要的行。如果您不需要行,可以先删除原始excel文件中不需要的行,然后将excel导出到datatable。