tarzanbappa
tarzanbappa

Reputation: 4958

C# Excel only allow edit for specific row

I'm generating an excel file in C# and excel file is format is as follows.

enter image description here

And Columns From C1 will be dynamic since there can be several dates.

And what I need to do is only allow editing for rows with Ctype

YYY

Is there any way to identify rows with Ctype = YYY

The code I use to generate excel is as follows

public string ExcelGenerator()
        {
            DataTable dt = GetDataTable();

            var fileName = "ExcelFile";
            var excelApp = new Application();
            var workbooks = excelApp.Workbooks;
            var excelWorkBook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            var sheets = excelWorkBook.Sheets;
            var excelWorkSheet = sheets[1];
            excelWorkSheet.Name = "ExcelFile";

            int iCol = 1;
            // Add column headings...

            foreach (DataColumn c in dt.Columns)
            {
                excelWorkSheet.Cells[1, iCol] = c.ColumnName; ;
                ((Range)excelWorkSheet.Cells[1, iCol]).Interior.Color = ColorTranslator.ToOle(Color.DarkGray);
                iCol++;
            }

            // for each row of data...

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    excelWorkSheet.Cells[j + 2, k + 1] = dt.Rows[j].ItemArray[k].ToString();

                }
            }

            string fullpath = Path.Combine(excelPath, fileName);
            if (File.Exists(fullpath))
            {
                try
                {
                    File.Delete(fullpath);
                }
                catch (IOException e)
                {
                    Console.Write(e.Message);
                }
            }
            excelApp.DisplayAlerts = false;

            excelWorkBook.SaveAs(fullpath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
            excelWorkBook.Close(false, Type.Missing, Type.Missing);
            excelApp.Quit();
            Marshal.ReleaseComObject(workbooks);
            Marshal.ReleaseComObject(excelWorkSheet);
            Marshal.ReleaseComObject(sheets);
            Marshal.ReleaseComObject(excelWorkBook);
            Marshal.ReleaseComObject(excelApp);
            excelWorkSheet = null;
            excelWorkBook = null;
            excelApp = null;
            workbooks = null;
            GC.WaitForPendingFinalizers();
            GC.Collect();
            return fileName;

        }

Upvotes: 0

Views: 585

Answers (1)

 FrozenChicken
FrozenChicken

Reputation: 11

Excel.Office.Interop don't allow range unprotection by programmatically.

For more information, you may visit this website. https://msdn.microsoft.com/en-us/library/dkcs53f3.aspx

But if you use EPPlus library, you can do like Excel users.

if you allow selective editable column, excel requires sheet protection = true and some unprotected range.

(using EPPlus)

excelWorkSheet.Protection.IsProtected = true;
excelWorkSheet.ProtectedRanges.Add("editable", new ExcelAddress("B"));

Upvotes: 1

Related Questions