如何使用 C# 检查 EXCEL 中的单元格是否为只读

发布于 2024-07-30 04:23:07 字数 3009 浏览 4 评论 0原文

我正在将数据从数据库导入到 Excel 工作表中。 为此,我使用数据读取器。 Excel 工作表模板有一些宏和一些计算公式,它不是普通的 Excel 工作表。 因此,只有在允许特定单元格写入的情况下,我才必须将数据写入 Excel 工作表。 如果不是,则不应导入数据。

为此,我有一个 XML 文件,其中说明我应该从哪一列开始写入以及应该在哪一行停止写入,我已经在很多工作表中这样做了。 但在一张纸中,该行的第一个单元格是“只读”(锁定),其余单元格允许写入访问。

由于我使用 Datareader 从数据库获取整行,因此我需要写入其他单元格,而不写入锁定的单元格。

我附上代码片段以供参考。

请帮我做这件事。

示例 ::

 if (reader.HasRows)
  {
   minRow = 0;
    minCol = 0;
   Excel.Workbook SelWorkBook = excelAppln.Workbooks.Open(curfile, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, false, false, false);
    Excel.Sheets excelSheets = SelWorkBook.Worksheets;

 Excel.Worksheet excelworksheet = (Excel.Worksheet)excelSheets.get_Item(CurSheetName);

                                        // Process each result in the result set
                                        while (reader.Read())
                                        {
                                            // Create an array big enough to hold the column values
                                            object[] values = new object[reader.FieldCount];

                                            // Add the array to the ArrayList
                                            rowList.Add(values);

                                            // Get the column values into the array
                                            reader.GetValues(values);

                                            int iValueIndex = 0;

                                            // If the Reading Format is by ColumnByColumn 
                                            if (CurTaskNode.ReadFormat == "ColumnbyColumn")
                                            {
                                                minCol = 0;
                                                //   minRow = 0;
                                                for (int iCol = 0; iCol < CurTaskNode.HeaderData.Length; iCol++)
                                                {

                                                    // Checking whether the Header data exists or not
                                                    if (CurTaskNode.HeaderData[minCol] != "")
                                                    {
                                                        // Assigning the Value from reader to the particular cell in excel sheet
                                                        excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex];
                                                        iValueIndex++;

                                                    }
                                                    minCol++;
                                                }
                                                minRow++;
                                            }SelWorkBook.Close(true, curfile, null);

请帮我解决这个问题。

谢谢你,

拉姆

I am importing data to Excel sheets from a database. For this, I am using datareader. The excel sheet template has some macros and few formulae calculated and its not the normal excel worksheet. so I have to write the data into the excel sheet only if the particular cell is allowed to write. If not, the data shouldn't be imported.

So for this, I have a XML file which says from which column I should start writing and in which row it should stop, I have done this for many sheets. But in one sheet, the first cell of the row is "readonly" (locked) and the rest are write access permitted.

Since I get the entire row from DB using Datareader, I am stuck with needing to write to the other cells, without writing to the locked cell.

I am attaching the code snippet for reference.

Please help me in doing this.

Sample ::

 if (reader.HasRows)
  {
   minRow = 0;
    minCol = 0;
   Excel.Workbook SelWorkBook = excelAppln.Workbooks.Open(curfile, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, false, false, false);
    Excel.Sheets excelSheets = SelWorkBook.Worksheets;

 Excel.Worksheet excelworksheet = (Excel.Worksheet)excelSheets.get_Item(CurSheetName);

                                        // Process each result in the result set
                                        while (reader.Read())
                                        {
                                            // Create an array big enough to hold the column values
                                            object[] values = new object[reader.FieldCount];

                                            // Add the array to the ArrayList
                                            rowList.Add(values);

                                            // Get the column values into the array
                                            reader.GetValues(values);

                                            int iValueIndex = 0;

                                            // If the Reading Format is by ColumnByColumn 
                                            if (CurTaskNode.ReadFormat == "ColumnbyColumn")
                                            {
                                                minCol = 0;
                                                //   minRow = 0;
                                                for (int iCol = 0; iCol < CurTaskNode.HeaderData.Length; iCol++)
                                                {

                                                    // Checking whether the Header data exists or not
                                                    if (CurTaskNode.HeaderData[minCol] != "")
                                                    {
                                                        // Assigning the Value from reader to the particular cell in excel sheet
                                                        excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex];
                                                        iValueIndex++;

                                                    }
                                                    minCol++;
                                                }
                                                minRow++;
                                            }SelWorkBook.Close(true, curfile, null);

Please help me in resolving this.

Thank You,

Ramm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱人如己 2024-08-06 04:23:07

好的,首先您需要检查第一个单元格的锁定属性,然后如果它被锁定,则对数组进行切片(以便您拥有整行减去第一列),然后写入工作表。 下面是一些代码,不一定准确,SLICE 函数只是伪代码,在 C# 中切片数组有多种不同的方法,请使用您选择的方法:

if (!excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + 1].Locked )
{
  excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex];
  iValueIndex++;
}
else
{
  excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values.SLICE(iValueIndex);
  iValueIndex++;
}

Ok, first you need to check the locked property of the first cell, then if it's locked slice the array (so that you have the whole row minus the first column), then write to the sheet. Here's some code, not necessarily exact, the SLICE function is just pseudo-code, there are a number of different ways of slicing arrays in C#, use the method of your choice:

if (!excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + 1].Locked )
{
  excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex];
  iValueIndex++;
}
else
{
  excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values.SLICE(iValueIndex);
  iValueIndex++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文