문제

다음 코드를 얻었습니다 여기 c# .net을 사용하여 Excel 파일을 읽으려면 :

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
DbCommand command = connection.CreateCommand()
command.CommandText = "SELECT City,State FROM [Cities$]";

4 번째 행에서 시작하여 10 행만 선택하고 싶습니다. 어떻게 할 수 있습니까?

도움이 되었습니까?

해결책

나는 Excel Querying Expert가 아니지만 확실히 작동합니다. 상단을 사용하여 쿼리를 13 행으로 제한하십시오. 첫 번째 행은 열 이름이있는 헤더이므로 계산되지 않을 수 있습니다. 내가 오해하면 분명히 변합니다. 그런 다음 행 ID를 추적하고 4 켜기 또는 후 4 행을 수행하십시오.

도움이 되었기를 바랍니다!

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString)) {
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
    cmd.Connection = conn;
    cmd.CommandText = "SELECT top 13 City,State FROM [Cities$]";

    conn.Open();
    System.Data.IDataReader dr = cmd.ExecuteReader();

    int row = 2;
    while (dr.Read()) {
        if (row++ >= 4) {
            // do stuff
            Console.WriteLine("{0}, {1}", dr[0], dr[1]);
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top