0

Excel シートの何行がデータで満たされているか、つまり空でない行を細かくする必要があります。私はC#を使用してそれを行う必要があります。

現在、私は次のコードを使用しています:

Excel.Application excelApp = new Excel.Application();
            string workbookPath = "C:\\ScriptTest\\bin\\Debug\\SDownloadScripts\\ExcelResult.xlsx";
            Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
        0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
        true, false, 0, true, false, false);
            Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            string currentSheet = "Sheet1";
            Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
            //long i = excelWorksheet.UsedRange.Rows.Count;
            int lastUsedRow = excelWorksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,

Type.Missing).Row;
            return lastUsedRow;
        }

私のシートには 4 行しか入力されていませんが、65536 を取得しています。結果として 4 行が必要です。つまり、データが入力された行がありません。提案してください。

4

3 に答える 3

3

これを試して..

string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\xxxxxx\Desktop\1-8-13-ct.xlsx';Extended Properties=Excel 12.0;Persist Security Info=False";
    //Create Connection to Excel work book
    OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
    //Create OleDbCommand to fetch data from Excel
     OleDbCommand cmd = new OleDbCommand("select count(*) from [Sheet1$]", excelConnection);
    con.Open();
    int rows = (int)cmd.ExecuteScalar();
  con.Close();
于 2013-09-04T06:48:29.613 に答える
0

同様の質問をしているスタックオーバーフローのスレッドをいくつか見つけました。たぶん、これらはすでにあなたの質問を解決しています。この答えはあなたが探しているもののようです。

あなたの結果、XlCelltypeのドキュメントとアウトコメントされた行に応じて、シートの usedRange が実際に使用されている領域よりも大きいと想定しています。他の解決策が機能せず、データ行の間に空の行がない場合は、空のセルを持つ列の最初の行を検索してみてください (この行 1 は、必要な行数である必要があります)。

于 2013-09-04T06:50:57.513 に答える
0

プライベート DataSet CreateDataSource(string strFilePath, string strSheetName) { DataSet myDataSet; myDataSet = null; 試す {

        if (strSheetName.Length > 0)
        {
            StringBuilder strConnectionString = new StringBuilder();
            strConnectionString.AppendFormat("Provider={0};", "Microsoft.ACE.OLEDB.12.0");
            strConnectionString.AppendFormat("Data Source={0};", strFilePath);
            strConnectionString.Append("Extended Properties=");
            strConnectionString.Append(((char)34).ToString()); //start of trickypart
            strConnectionString.Append("Excel 12.0 Xml;");
            // always treat contents of cells as text, which gives us full control/responsibility for casting to numeric when ncessary
            strConnectionString.Append(((char)34).ToString()); // end of tricky part
            string str = strConnectionString.ToString();

            OleDbConnection conn = new OleDbConnection(str);

            OleDbDataAdapter myCommand = new OleDbDataAdapter(" SELECT * FROM [" + strSheetName + "$] where QuestionDescription <>''", str);
            myDataSet = new DataSet();
            myCommand.Fill(myDataSet);
        }
        else
        {
            trError.Visible = true;
            lblError.Text = "File is Invalid format";
        }
    }
    catch
    {
        trError.Visible = true;
        lblError.Text = "Invalid format!!";
    }
    return myDataSet;
}

上記のコードを使用して、空白でない行を取得するためにクエリを実行できます。結果はデー​​タセットに保存されます。データセットから空でないセル数を取得できます。このコードを機能させるには、"Microsoft.ACE.OLEDB.12.0" プロバイダーを使用する必要があります。

于 2013-09-04T06:49:59.573 に答える