0

i am using the code below to select all of the data from a excel file and was wondering if it would be possible for me to start from row three and read in the rest of the data within the file..

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;
4

3 回答 3

0

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

于 2012-11-16T06:02:44.887 回答
0

You could use something like this to ammend your file before reading in with 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;

    }
于 2012-11-15T11:50:55.857 回答
0
var w = sqlData.AsEnumerable().Where(data => data.Field<String>("slideNo") == "5")
                .Select(data=> data.Field<String>("QuestionStartText"));

这是编码的答案...

于 2012-11-15T15:15:31.600 回答