I am working on MS Excel 2013 generating report where all the worksheets in workbook should have freeze pane at column 6 and row 1. I have searched on Google but could not find any solution as for freezing the pane, workbook has to be active. I have tried a lot of things but no success. I will really appreciate if someone can help me.
Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Open("filelocation");
foreach (Excel.Worksheet ws in workbook.Worksheets)
{
ws.Application.ActiveWindow.SplitColumn = 6;
ws.Application.ActiveWindow.SplitRow = 1;
ws.Application.ActiveWindow.FreezePanes = true;
}
excel.Visible = true;
I Hope it help others. I have used ClosedXML Library for Excel and after creating each worksheet I used
worksheet.SheetView.Freeze(1,6);
This freezes the Row 1, Col 6. You can freeze any row/column.
Here is link to ClosedXML. It's widely supported and very good documentation.
To freeze the panes on each worksheet you need to modify your for loop to add a line to activate the current sheet prior to setting the other properties. Here is my solution:
Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Open("filelocation");
foreach (Excel.Worksheet ws in workbook.Worksheets)
{
ws.Activate();
ws.Application.ActiveWindow.SplitColumn = 6;
ws.Application.ActiveWindow.SplitRow = 1;
ws.Application.ActiveWindow.FreezePanes = true;
}
excel.Visible = true;
Related
Here I'm opening excel and writing to excel sheet. I'm changing my windows application to asp website and seen this error. I have added all the references and libraries. Don't know what I am missing here.
Getting error as mentioned below. Please help me.
Excel.Application excel = new Excel.Application();
excel.Visible = false; // to hide the processing
Excel.Workbook wb = excel.Workbooks.Add();
Excel.Worksheet sh = wb.Sheets.Add(); // Error at wb
sh.Name = "Links";
for (int i = 1; i < list.Count; i++)
{
sh.Cells[i.ToString(), "A"].Value2 = list[i]; //Error at .Value2
}
you have to create a new Worksheet with Sheets array by providing WorkSheet Name.
and also please Cast The Newly Created WorkSheet.
Replace this :
Excel.Worksheet sh = wb.Sheets.Add();
with following
Excel.Worksheet sh = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets["Sheet1"];
To resolve your second error,
//Error at .Value2
Go to project properties. (Click Project in Menu, Click properties)
Set the Target Framework as .NET Framework 4
This should resolve your .Value2 error.
I started creating an Excel-Add-IN with C#.
what I need to do is simple, I need to set a workbook to a variable, the workbook is already running, I tried this but did not work
Excel.Application excel = new Excel.Application();
Excel.Workbook wb = excel.ActiveWorkbook as Excel.Workbook;
wb.SaveAs("C:\\Users\\ro_sg\\Desktop\\Pasta1.xlsx");
Excel.Worksheet ws = wb.Worksheets["Plan1"];
Excel.Range range = ws.Range["A1"];
range.Value = "Success";
wb.Save();
The wb variable cannot find the workbook (gets null), and I can't see why.
Please, if any of you spot the mistake let me know.
Thanks!
I believe your issue is it may not be finding the active Excel application upstream from when you set your workbook variable. It appears that your code is trying to create a new excel application (without a workbook) rather than get the existing one that is open.
Give this a try:
Excel.Application excel = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
Excel.Workbook wb = (Excel.Workbook)excel.ActiveWorkbook;
wb.SaveAs("D:\\WeeeDueceDuece.xlsx");
I don't know if you need to get a specific Sheet but if you try this: Excel.Worksheet ws = wb.Worksheets[1]; It will get the first Sheet of your Workbook
If your actual code doesn't have more in-between steps, it will always fail. I'm surprised this line didn't error:
Excel.Workbook wb = excel.ActiveWorkbook as Excel.Workbook;
It's because a new instance of Excel does not necessarily create a new workbook. You can check this with the following lines:
Excel.Application application = new Excel.Application();
Excel.Workbooks workbooks = application.Workbooks;
Console.WriteLine(workbooks.Count); // "0"
The new workbook, however, should create the default number of worksheets (usually 3, but editable).
I have an excel workbook with many, many sheets. I want to delete all the sheets except for three of them.
Specifically, i would like to know if there is a way to remove the sheets using sheet name instead of ordinals (sheet number).
I am using excel interop and C# to work with Excel.
Microsoft.Office.Interop.Excel.Application xlApp = null;
Excel.Workbook xlWorkbook = null;
Excel.Sheets xlSheets = null;
Excel.Worksheet xlNewSheet = null;
xlApp.DisplayAlerts = false;
for (int i = xlApp.ActiveWorkbook.Worksheets.Count; i > 0 ; i--)
{
Worksheet wkSheet = (Worksheet)xlApp.ActiveWorkbook.Worksheets[i];
if (wkSheet.Name == "NameOfSheetToDelete")
{
wkSheet.Delete();
}
}
xlApp.DisplayAlerts = true;
I know this is old but I just use the fallowing
workBook.Sheets["Sheet1"].Delete();
I know this thread is really old but for future visitors, if you know the names of the sheets you want to delete, a better way is to loop over the names of the sheets like this:
Workbook book = excelApp.Workbooks.Open("filePathHere");
string[] sheetsToDelete = {"s1", "s2"};
excelApp.DisplayAlerts = false;
foreach(string sheetToDelete in sheetsToDelete )
{
book.Worksheets[sheetToDelete].Delete();
}
excelApp.DisplayAlerts = true;
It's always good practice to avoid deleting items in a collection while iterating through it.
I am writing a program in C# for analysing a structure details in an earth quake.I have a lot of data and want to put them in a excel file.
I mean i need a function like this :
public void excel(int sheet_no,int row,int column,string value)
Is there any way to parse the text file and Put this data in Excel sheet ?
use this :
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Workbooks workbooks;
_Workbook workbook;
_Workbook workbook2;
Sheets sheets;
_Worksheet worksheet;
app.Visible = false;
workbooks = app.Workbooks;
workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
sheets = workbook.Worksheets;
worksheet = (_Worksheet)sheets.get_Item(1);
worksheet.Cells[row, column] = value;
workbook.Saved = true;
workbook.SaveAs(output_file);
app.UserControl = false;
app.Quit();
Take a look at ClosedXml
Code sample:
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");
But if you are dealing with a lot of data, consider using a database like sqlserver.
It provides a lot of analysis tools.
You can connect it as a datasource to excel too.
For now, I am providing links for you to read # Excel Tasks and code samples # Excel controls in ASP.net. If you're still confused, I may be able to find code samples from my projects or the Internet, which are not hard to find.
Look at link Workbook Open (create) method to create Workbooks, and link Workbook Open method to open Workbooks.
I am trying to do the following using c# code:
Hide some rows in excel.
Clear all data and formats in excel sheet.
Put other data to excel sheet.
I would like the hidden rows still remain hidden.
Is it possible?
Thank You!
I've had great results using ClosedXML to manipulate excel spreadsheets.
While I haven't tried your case I've done similar things. In my case I put my private data into a new worksheet and hide that, which ClodedXML made simple.
Here's a sample code that can get you going....
//Create an Excel App
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook xlWorkBook = null;
Microsoft.Office.Interop.Excel._Worksheet xlWorksheet;
//Open a Workbook
xlWorkBook = xlApp.Workbooks.Open(#"d:\test.xlsx");
xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[1];
//My Workbook contains 10 rows with some data and formatting
//I Hide rows 3, 4 & 5
Microsoft.Office.Interop.Excel.Range hiddenRange = xlWorksheet.get_Range("A3:C5");
hiddenRange.EntireRow.Hidden = true;
//Get the entire sheet and Clear everything on it including data & formatting
Microsoft.Office.Interop.Excel.Range allRange = xlWorksheet.UsedRange;
allRange.Clear();
//Now Add some new data, say a Title on the first cell, and some more data in a loop later
xlWorksheet.Cells[1, 1] = "Title";
for (int i = 6; i < 10; i++)
{
xlWorksheet.Cells[i, 1] = i.ToString();
}
xlApp.Visible = true;
Thats it....
Store them in a variable and hide them again after you have populated excel with data.