المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : how to write in excel cells using DataReader if a particular cell is “ReadOnly” using C#



C# Programming
08-03-2009, 06:43 PM
Hi,

I am importing data to Excel sheets from 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 which row it should stop, I have done for many sheets. But in one sheet, I have first cell of the row is "readonly" and the rest are write access permitted.

As, I get entire row from DB using Datareader, I am stuck up to write to other cells, except for the particular 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);


This piece of code reads the data from DB if the reader has rows.It then opens a new preloaded excel template file and assigns the reader data to the Values object. Then for the header length (HeaderData.Length) in XML file it writes to the each excel cell and finally it saves the file and closes. The ******** where the data is written is excelworksheet.Cells[CurTaskNode.DATA_MIN_ROW + minRow, CurTaskNode.DATA_MIN_COL + minCol] = values[iValueIndex]; // Actually a check needs to be performed b4 this whether to write into cell. i'm not able to do it.
Please help me in resolving this.



Thank You,

Ramm