winforms 如何使用OpenXml设置它需要开始阅读的特定行

rnmwe5a2  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(114)

我尝试从特定行读取工作表并将其转换为JSON。
下面是我的尝试:

int startRowNum = 6; //the start reading row number
            int rowNum = 1; // starting from first row in sheet
                            // Iterate over the rows and add the cell data to a dictionary
            foreach (Row row in rows)
            {
                if (rowNum >= startRowNum)
                {
                    if (!row.Descendants<Cell>().Any(c => !string.IsNullOrWhiteSpace(c.InnerText)))
                    {
                        // If the row has no cells with values, exit the loop
                        break;
                    }

                          //code is here
                }
                rowNum++;
            }

下面是我用来读取数据的完整代码:

int startRowNum = 6; //the start reading row number
            int rowNum = 1; // starting from first row in sheet

            // Iterate over the rows and add the cell data to a dictionary
            foreach (Row row in rows)
            {
                if (rowNum >= startRowNum)
                {
                    if (!row.Descendants<Cell>().Any(c => !string.IsNullOrWhiteSpace(c.InnerText)))
                    {
                        // If the row has no cells with values, exit the loop
                        break;
                    }

                    InterfaceRecord record = new InterfaceRecord();
                    int columnNumber = 1;

                    foreach (Cell cell in row.Elements<Cell>())
                    {
                        string columnName = GetColumnName(columnNumber);
                        string cellValue = GetCellValue(cell, spreadsheetDocument);

                        if (columnName == properties.SampleNumber)
                        {
                            record.SampleNumber = cellValue;
                        }
                        else if (columnName == properties.ValueColumn)
                        {
                            decimal value = 0;
                            decimal.TryParse(cellValue, NumberStyles.Any, CultureInfo.InvariantCulture, out value);
                            record.Value = decimal.Round(value, properties.RoundingNumber);
                        }

                        columnNumber++;
                    }

                    // Set remaining properties
                    record.CreatedAt = DateTime.Now;
                    record.MachineName = properties.MachineName;
                    record.ClientID = properties.ClientID;

                    interfaceRecords.Add(record);
                }

                rowNum++;
            }

下面是一个json的例子:

{
    "SampleNumber": "31119-12",
    "Value": 0.00446,
    "CreatedAt": "2023-03-15T17:06:44.5118565+02:00",
    "ClientID": 123,
    "MachineName": "Analyzer A"
  },
  {
    "SampleNumber": "31119-13",
    "Value": 0.01343,
    "CreatedAt": "2023-03-15T17:06:44.5122332+02:00",
    "ClientID": 123,
    "MachineName": "Analyzer A"
  },
  {
    "SampleNumber": "31119-14",
    "Value": 0.02018,
    "CreatedAt": "2023-03-15T17:06:44.512247+02:00",
    "ClientID": 123,
    "MachineName": "Analyzer A"
  },

我最好的猜测是它只阅读有数据的行。因为在代码中我想读取第6行。第一,第三和第四行是空的。我需要它来一些如何包括空行。

mf98qq94

mf98qq941#

我找到了答案:

int startRowNum = 6-1; //the start reading row number
            int currentRowNum = 1; // starting from first row in sheet
            // Iterate over the rows and add the cell data to a dictionary
            foreach (Row row in rows)
            {
                if (currentRowNum >= startRowNum)
                {
                    if (!row.Descendants<Cell>().Any(c => !string.IsNullOrWhiteSpace(c.InnerText)))
                    {
                        // If the row has no cells with values, exit the loop
                        break;
                    }

                          //code is here
                }
                if (row.RowIndex != null)
                {
                    // Increment the current row number if the row index is not null
                    currentRowNum = (int)row.RowIndex.Value;
                }
            }

现在只需添加应用程序设置的开始行,使其更加灵活,应该没问题

相关问题