0

我正在阅读一个 excel 文件的列。列不能为空。我如何测试每一列,即 col1value 到 col15value 而不对所有列使用 && 语句

                           for (int rowNumber = startRow + 1; rowNumber <= currentWorkSheet.Dimension.End.Row; rowNumber++)
                            // read each row from the start of the data (start row + 1 header row) to the end of the spreadsheet.
                            {
                                try
                                {
                                    object col1Value = currentWorkSheet.Cells[rowNumber, 1].Value;
                                    object col2Value = currentWorkSheet.Cells[rowNumber, 2].Value;
                                    object col3Value = currentWorkSheet.Cells[rowNumber, 3].Value;
                                    object col4Value = currentWorkSheet.Cells[rowNumber, 4].Value;
                                    object col5Value = currentWorkSheet.Cells[rowNumber, 5].Value;
                                    object col6Value = currentWorkSheet.Cells[rowNumber, 6].Value;
                                    object col7Value = currentWorkSheet.Cells[rowNumber, 7].Value;
                                    object col8Value = currentWorkSheet.Cells[rowNumber, 8].Value;
                                    object col9Value = currentWorkSheet.Cells[rowNumber, 9].Value;
                                    object col10Value = currentWorkSheet.Cells[rowNumber, 10].Value;
                                    object col11Value = currentWorkSheet.Cells[rowNumber, 11].Value;

                                    object col12Value = currentWorkSheet.Cells[rowNumber, 12].Value;
                                    object col13Value = currentWorkSheet.Cells[rowNumber, 13].Value;
                                    object col14Value = currentWorkSheet.Cells[rowNumber, 14].Value;
                                    object col15Value = currentWorkSheet.Cells[rowNumber, 15].Value;


                                    if ((col1Value != null && col2Value != null && col3Value != null))
                                    {
                                        excelFileDataList.Add(new priceAddDelete
                                        {
                                            businessunitcode = col1Value.ToString(),
                                            customerNumber = Convert.ToInt32(col2Value),
                                            productcode = col3Value.ToString(),
                                            sizecode = col4Value.ToString(),
                                            finishingmethodcode = col5Value.ToString(),
                                            startdate = Convert.ToDateTime(col6Value),
                                            typeofprice = col7Value.ToString(),
                                            pricetype = Convert.ToInt32(col8Value),
                                            typeofrent = Convert.ToInt32(col9Value),
                                            changesperweek = Convert.ToInt32(col10Value),
                                            adddelete = col11Value.ToString(),
                                            price =  Convert.ToInt32(col12Value),
                                            processed = Convert.ToInt32(col13Value),
                                            processed_date = Convert.ToDateTime(col14Value),
                                            systemuser_id = Convert.ToInt32(col15Value)                                           
                                        });
                                    }

                                }
                                catch (Exception ex)
                                {

                                }

                            }
4

2 回答 2

0

您可以使用 for 循环。尝试这个

bool _notempty = false;
for (int x = 1;x <= 15; x++)
{
    if (currentWorkSheet.Cells[rowNumber, x].Value == null) 
    {
        //MessageBox.Show("Error Message");
        _notempty = false;
        break;
    }
    else
    {
        _notempty = true;
    }
}

然后把它放在循环之外

if (_notempty)
{
     excelFileDataList.Add(new priceAddDelete
     {
         businessunitcode = col1Value.ToString(),
         customerNumber = Convert.ToInt32(col2Value),
         productcode = col3Value.ToString(),
         sizecode = col4Value.ToString(),
         finishingmethodcode = col5Value.ToString(),
         startdate = Convert.ToDateTime(col6Value),
         typeofprice = col7Value.ToString(),
         pricetype = Convert.ToInt32(col8Value),
         typeofrent = Convert.ToInt32(col9Value),
         changesperweek = Convert.ToInt32(col10Value),
         adddelete = col11Value.ToString(),
         price =  Convert.ToInt32(col12Value),
         processed = Convert.ToInt32(col13Value),
         processed_date = Convert.ToDateTime(col14Value),
         systemuser_id = Convert.ToInt32(col15Value)                                           
    });
}
于 2013-10-05T23:47:34.220 回答
-1

您可以将列值放入对象数组或列表中并测试它是否为空。

        // read each row from the start of the data (start row + 1 header row) to the end of the spreadsheet.
        for (int rowNumber = startRow + 1; rowNumber <= currentWorkSheet.Dimension.End.Row; rowNumber++)
        {
            List<object> columns = List<object>();
            columns.Add(currentWorkSheet.Cells[rowNumber, 1].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 2].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 3].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 4].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 5].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 6].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 7].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 8].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 9].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 10].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 11].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 12].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 13].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 14].Value);
            columns.Add(currentWorkSheet.Cells[rowNumber, 15].Value);

            if (!columns.Contains(null))
            {
                excelFileDataList.Add(new priceAddDelete
                {
                    businessunitcode = columns[0].ToString(),
                    customerNumber = Convert.ToInt32(columns[1]),
                    productcode = columns[2].ToString(),
                    sizecode = columns[3].ToString(),
                    finishingmethodcode = columns[4].ToString(),
                    startdate = Convert.ToDateTime(columns[5]),
                    typeofprice = columns[6].ToString(),
                    pricetype = Convert.ToInt32(columns[7]),
                    typeofrent = Convert.ToInt32(columns[8]),
                    changesperweek = Convert.ToInt32(columns[9]),
                    adddelete = columns[10].ToString(),
                    price = Convert.ToInt32(columns[11]),
                    processed = Convert.ToInt32(columns[12]),
                    processed_date = Convert.ToDateTime(columns[13]),
                    systemuser_id = Convert.ToInt32(columns[14])
                });
            }
        }
于 2013-10-06T00:02:11.647 回答