Question posted 2010 · +3 upvotes
I generate an excel spread-cheat with c# and I want to freeze the first column. This is the code that I use :
public static void SaveToExcel(object[,] data)
{
Excel = Microsoft.VisualBasic.Interaction.CreateObject("Excel.Application", String.Empty);
Excel.ScreenUpdating = false;
dynamic workbook = Excel.workbooks;
workbook.Add();
dynamic worksheet = Excel.ActiveSheet;
const int left = 1;
const int top = 1;
int height = data.GetLength(0);
int width = data.GetLength(1);
int bottom = top + height - 1;
int right = left + width - 1;
if (height == 0 || width == 0)
return;
dynamic rg = worksheet.Range[worksheet.Cells[top, left], worksheet.Cells[bottom, right]];
rg.Value = data;
// Set borders
for (var i = 1; i <= 4; i++)
rg.Borders[i].LineStyle = 1;
// Set header view
dynamic rgHeader = worksheet.Range[worksheet.Cells[top, left], worksheet.Cells[top, right]];
rgHeader.Font.Bold = true;
rgHeader.Interior.Color = 189 * (int)Math.Pow(16, 4) + 129 * (int)Math.Pow(16, 2) + 78;
rg.EntireColumn.AutoFit();
// Show excel app
Excel.ScreenUpdating = true;
Excel.Visible = true;
}
Could You Please Help me???
Accepted answer +9 upvotes
The right solution is to add the following code :
worksheet.Activate();
worksheet.Application.ActiveWindow.SplitColumn = 1;
worksheet.Application.ActiveWindow.FreezePanes = true;
Excel VBA objects referenced (5)
Application— Using events with the Application objectApplication— Working with Other ApplicationsInterior.Color— Change the Color of the Horizontal Axis of a SparklineRange— Refer to Cells by Using a Range ObjectRange— Delete Duplicate Entries in a Range
Top excel Q&A (6)
- Shortcut to Apply a Formula to an Entire Column in Excel +335 (2011)
- How should I escape commas and speech marks in CSV files so they work in Excel? +136 (2012)
- Convert xlsx to csv in linux command line +96 (2012)
- How to create a link inside a cell using EPPlus +50 (2011)
- IF statement: how to leave cell blank if condition is false ("" does not work) +44 (2013)
- T-SQL: Export to new Excel file +44 (2012)
excel solutions on this site
.