Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have written a program which writes respective data from xml files to excel sheet, after writing the data, the program will protect the entire excel sheet with a password. So now i should protect entire excel sheet, but except one column. I need help of how to do that.

As per the below code i am reading data like Name, City, Area and Time from .Xml files which are at a path declared in the above code as "Excelpath" and wrtiting the respective data in excel sheet.

I have Protected the excel sheet with password "abcd" but i need to unprotect 5th Column that means the data is written on to first four columns like name, city, time, area. So for 5th column i need to have user editable.

What I have tried:

C#
Excel.Application oXL = null;
Excel.Workbook oWB = null;
Excel.Worksheet excelWorksheet = null;
Excel.Sheets excelSheets = null;
Excel.Worksheet oSheet = null;

//XML Library Declarations
XmlDocument doc = new XmlDocument();
log.Info(excelPATH);
//Declarations
bool isItFirstRow = true;
CurrentRow = 8;
///Process
try
{
	if (!File.Exists(excelPATH))
	{
		log.Error("The Output Excel File was not available");
		return;
	}
	CheckExcellProcesses(); //To Check EXCEl process is running or not

	//Excel Initializations
	log.Info("Intializing the EXCEL Library");
	log.Info(excelPATH);
	oXL = new Excel.ApplicationClass();
	oWB = oXL.Workbooks.Open(excelPATH, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
	excelSheets = oWB.Worksheets;
	excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(1);
	oSheet = excelWorksheet;
	log.Info(String.Format("Program is writing to {0} File", excelPATH));
	//to Get the XML file
	string[] requiredXMLfiles = getRequiredXMLFiles(fromDate, toDate, "_Excelsheet.xml");

	//Now we have the XML files just to read and write to EXCEL
	foreach (string file in requiredXMLfiles)
	{
		if (File.Exists(file))
		{
			doc.Load(file);
			string OriginalTime = doc.GetElementsByTagName("Time")[0].InnerText;
			XmlNodeList bookList = doc.GetElementsByTagName("HYDR.Database");
			isItFirstRow = true;
			foreach (XmlNode node in bookList)
			{
				CurrentColumn = 2;
				XmlElement bookElement = (XmlElement)node;

				//Reading Values from XML

				string Name = bookElement.GetElementsByTagName("HYDR.Name")[0].InnerText;
				string City = bookElement.GetElementsByTagName("HYDR.City")[0].InnerText;
				string Area = bookElement.GetElementsByTagName("HYDR.Area")[0].InnerText;

				//Writing to Excel file
				if (isItFirstRow)//Table has 3 rows, If first has nothing then no need to check
				{                //Remaining Rows. If first row has the data need to check other rows also
					oSheet.Cells[CurrentRow, CurrentColumn++] = OriginalTime;
					oSheet.Cells[CurrentRow, CurrentColumn++] = Name;
					oSheet.Cells[CurrentRow, CurrentColumn++] = City;
					oSheet.Cells[CurrentRow, CurrentColumn++] = Area;
					CurrentRow++; //Going to next row
					isItFirstRow = false;
					break;
				}
				else
				break;
			}
			else
			{
				oSheet.Cells[CurrentRow, CurrentColumn++] = OriginalTime;
				oSheet.Cells[CurrentRow, CurrentColumn++] = Name;
				oSheet.Cells[CurrentRow, CurrentColumn++] = City;
				oSheet.Cells[CurrentRow, CurrentColumn++] = Area;
				CurrentRow++; //Going to next row
				isItFirstRow = false;
			}
		}
	}//End of Xml Reading///////
}
}
oSheet._Protect("abcd", true, true, true, true);
oWB.Save();
Posted
Updated 10-Apr-16 21:10pm
v2
Comments
Peter_in_2780 1-Apr-16 1:02am    
If you were doing it from the keyboard, you'd either protect the whole sheet then select the column and unprotect it. Or in the first place, select just the columns you want to protect. Can't you do the same programmatically?

1 solution

Did you try:
C#
oSheet.Cells[CurrentRow, CurrentColumn].Locked = False;
 
Share this answer
 
Comments
Krishna Chaitanya Bezawada 11-Apr-16 3:01am    
tried but not working..
Patrice T 11-Apr-16 3:08am    
Define "not working"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900