by
0
6
2,245
1
Top 1% !
Popular
Famous
Tagged
Easy-to-find
Specified
Popularity: 8457th place
This snippet is Public
LanguageCSharp
LicenseMIT_X11

How to populate an Excel worksheet with data in CSharp

This c# example shows how to add some data into a worksheet in a Microsoft Excel document (xls). Note the hyperlink pointing to the main sheet.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/611/How-to-populate-an-Excel-worksheet-with-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
/// <summary> /// Populate the Sheet /// </summary> /// <param name="ws">Worksheet</param> /// <param name="tc">SyncSSTestCase</param> private void populateXlsSheet(Worksheet ws, SyncSSTestCase tc) { ws.Name = tc.SheetName; int rowIndex = 1; ws.get_Range("A" + rowIndex, "A" + rowIndex).Cells.Value2 = "Test case Name"; ws.get_Range("A" + rowIndex, "A" + rowIndex).Cells.ColumnWidth = 30; ws.get_Range("B" + rowIndex, "B" + rowIndex).Cells.Value2 = "Purpose"; ws.get_Range("B" + rowIndex, "B" + rowIndex).Cells.ColumnWidth = 30; ws.get_Range("C" + rowIndex, "C" + rowIndex).Cells.Value2 = "Area"; ws.get_Range("C" + rowIndex, "C" + rowIndex).Cells.ColumnWidth = 15; ws.get_Range("D" + rowIndex, "D" + rowIndex).Cells.Value2 = "Entity"; ws.get_Range("E" + rowIndex, "D" + rowIndex).Cells.ColumnWidth = 15; ws.get_Range("E" + rowIndex, "E" + rowIndex).Cells.Value2 = "Type"; ws.get_Range("E" + rowIndex, "E" + rowIndex).Cells.ColumnWidth = 15; ws.get_Range("F" + rowIndex, "F" + rowIndex).Cells.Value2 = "Environment"; ws.get_Range("F" + rowIndex, "F" + rowIndex).Cells.ColumnWidth = 15; ws.get_Range("G" + rowIndex, "G" + rowIndex).Cells.Value2 = "Actions"; ws.get_Range("G" + rowIndex, "G" + rowIndex).Cells.ColumnWidth = 30; ws.get_Range("H" + rowIndex, "H" + rowIndex).Cells.Value2 = "Expected Behavior"; ws.get_Range("H" + rowIndex, "H" + rowIndex).Cells.ColumnWidth = 30; ws.get_Range("I" + rowIndex, "I" + rowIndex).Cells.Value2 = "Comments"; ws.get_Range("I" + rowIndex, "I" + rowIndex).Cells.ColumnWidth = 20; ws.get_Range("A" + rowIndex, "I" + rowIndex).Cells.Font.Color = ColorTranslator.ToWin32(Color.White); ws.get_Range("A" + rowIndex, "I" + rowIndex).Cells.Interior.Color = ColorTranslator.ToWin32(Color.Gray); ws.get_Range("A" + rowIndex, "I" + rowIndex).Cells.Font.Bold = true; rowIndex++; ws.get_Range("A" + rowIndex, "A" + rowIndex).Cells.Value2 = tc.FullName; ws.get_Range("B" + rowIndex, "B" + rowIndex).Cells.Value2 = tc.Purpose; ws.get_Range("C" + rowIndex, "C" + rowIndex).Cells.Value2 = tc.Area; ws.get_Range("D" + rowIndex, "D" + rowIndex).Cells.Value2 = tc.Entity; ws.get_Range("E" + rowIndex, "E" + rowIndex).Cells.Value2 = tc.Type; ws.get_Range("F" + rowIndex, "F" + rowIndex).Cells.Value2 = tc.Environment; string steps = string.Empty; int stepscount = 1; string envNewLine = Environment.NewLine; foreach (XmlNode step in tc.ActionSteps) { if (stepscount == tc.ActionSteps.Count) { envNewLine = ""; } steps += string.Format("Step {0}:{1}{2}", stepscount, step.InnerText, envNewLine); stepscount++; } ws.get_Range("G" + rowIndex, "G" + rowIndex).Cells.Value2 = steps; ws.get_Range("H" + rowIndex, "H" + rowIndex).Cells.Value2 = tc.ExpectedBehavior; ws.get_Range("I" + rowIndex, "I" + rowIndex).Cells.Value2 = tc.Comment; //Create link to Main Sheet Summary Report rowIndex += 2; ws.get_Range("A" + rowIndex, "A" + rowIndex).Cells.Value2 = string.Format("=HYPERLINK(\"#{0}!A1\",\"<< Go to " + c_mainsSheet_Name + "\")", c_mainsSheet_Name); }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus