如果特定单元格是“只读”,如何使用 DataReader 在 Excel 单元格中写入 使用 C#

发布于 2024-07-26 01:41:34 字数 1666 浏览 7 评论 0原文

我正在使用 DataReader 将数据写入 Excelsheet 单元格。 在单元具有写入权限之前我没有问题。 但在一种情况下,只有一个单元格是只读的,其余单元格是可写的。

例如:10 * 10 个单元格,只有第一个单元格是只读的。 所以,我应该离开这个单元格并将其写入其余的单元格。 但是使用数据读取器它会一次性写入整行..那么我如何使用 C# 来实现这一点?

Team Leader (required) , , , , , , , , , 
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,

因此,第一个单元格不应由数据读取器写入。 请帮我做这件事

if (reader.HasRows)
{
    minRow = 0;
    minCol = 0;
    // 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++;
        }
    }
}

谢谢, 拉姆

I am using DataReader to write data into excelsheet cells. I have no problem until the cell has write previleges. But in one case, only one cell is readonly and the rest cells are writable.

Eg : 10 * 10 cells, only first cell is readonly. so, i shuld leave this cell and write it to rest of the cells. But with the data reader it writes entire row at one go.. so how can i acheive this using C# ?

Team Leader (required) , , , , , , , , , 
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,

so, the first cell shouldn't be written by datareader.
Please help me in doing this

if (reader.HasRows)
{
    minRow = 0;
    minCol = 0;
    // 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++;
        }
    }
}

Thank you,
Ramm

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

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

发布评论

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

评论(2

挥剑断情 2024-08-02 01:41:34

在谷歌上找到了这个,所以也许它有效/也许它无效:
这里的代码看起来与您的相同

您可以使用范围的AllowEdit属性,这样您就可以这样做:

// Assigning the Value from reader to the particular cell in excel sheet      
Excel.Range c = (Excel.Range)excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol];
if (c.AllowEdit) c.Value2 = values[iValueIndex];

而不是

// 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];

Found this on google so maybe it works/maybe it doesn't:
here, code looks the same as yours

You can use the AllowEdit property of a range so you could do:

// Assigning the Value from reader to the particular cell in excel sheet      
Excel.Range c = (Excel.Range)excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol];
if (c.AllowEdit) c.Value2 = values[iValueIndex];

instead of

// 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];
怪异←思 2024-08-02 01:41:34

我尝试了很多方法来弄清楚如何使用 C# 跳过写入到特定的 Excel 单元格,但无法获得正确的逻辑。

我的例如:
团队负责人(必填)abcxyz User1 user2
客户界面 Focal dfgidf user23 user3

上面的每个名称都必须位于特定的单元格中...但是在 Excel 模板中,第一个单元格(团队领导(必需))是只读的,因此我无法写入该单元格,所以我的最终 Excel 表应该显示

                            abcxyz  User1   user2
Customer Interface Focal    dfgidf  user23  user3.....
.....

我为此尝试了各种逻辑...请参阅下面的代码,

Microsoft.Office.Interop.Excel.Workbook SelWorkBook = excelappln1.Workbooks.Open(
    curfile,
    0, false, 5, "", "", false,
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
    "", true,
    false, 0, false, false, false);

Microsoft.Office.Interop.Excel.Sheets excelSheets = SelWorkBook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet excelworksheet =(Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(CurSheetName);

Microsoft.Office.Interop.Excel.Range excelRange = excelworksheet.UsedRange;

if ((!excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]).Locked)
{
    // 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++;
}

但错误显示在 if 语句中
错误 1 ​​运算符 '!' 不能应用于“object”类型的操作数

,因此,请说明如何处理这种情况。

谢谢
拉姆

I tried a lot to figure out how to skip writin onto a particular excel cell using C#, but couldnt get the proper logic.

my eg:
Team Leader (required) abcxyz User1 user2
Customer Interface Focal dfgidf user23 user3

each of the names above has to be in a particular cell... but in the excel template, first cell (Team Leader (required)) is readonly, so i cant write into that cell, so my final excel sheet should show

                            abcxyz  User1   user2
Customer Interface Focal    dfgidf  user23  user3.....
.....

i tried various logics for this... please see the code below

Microsoft.Office.Interop.Excel.Workbook SelWorkBook = excelappln1.Workbooks.Open(
    curfile,
    0, false, 5, "", "", false,
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
    "", true,
    false, 0, false, false, false);

Microsoft.Office.Interop.Excel.Sheets excelSheets = SelWorkBook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet excelworksheet =(Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(CurSheetName);

Microsoft.Office.Interop.Excel.Range excelRange = excelworksheet.UsedRange;

if ((!excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol]).Locked)
{
    // 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++;
}

but the error shows, in the if statement
Error 1 Operator '!' cannot be applied to operand of type 'object'

so, please say how to handle this case.

Thanks
Ramm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文