Changing sheet displayed with ClosedXML - c#

I am dynamically creating an Excel workbook with multiple sheets using ClosedXML. I am struggling changing the sheet selected back to the first sheet in the workbook after I am done generating the content and cannot find anything in the documentation on how to change the sheet that is displayed. I have tried:
wb.Worksheet(1).Select();
This selects all of the cells in the worksheet, but it doesn't change the view back.
wb.Worksheet(1).Cells(1,1).Value = wb.Worksheet(1).Cells(1,1).Value
seems to set the value, but again doesn't change the view.
I have tried hiding the other sheets in hopes that it would auto select the visible sheets, but that does not seem to work. Any suggestions?

Using ClosedXML to set the Active Sheet to the first worksheet one would use this code:
wb.Worksheet(1).SetTabActive();

I found that Andrew K's solution did not deselect previously selected tabs. I wrote this extension to meet this requirement also:
public static class Extensions
{
public static void SetActiveSheet(this XLWorkbook workbook, IXLWorksheet sheetToSetActive)
{
foreach (var sheet in workbook.Worksheets)
{
var isCurrentSheet = sheet.Equals(sheetToSetActive);
sheet.SetTabActive(isCurrentSheet);
sheet.SetTabSelected(isCurrentSheet);
}
}
}
Use it as follows:
var sheetToSetActive = wb.Worksheet(1);
wb.SetActiveSheet(sheetToSetActive);

Related

Number of pages of excel sheet

Does anyone know how to get the number of total pages within a excel sheet via OpenXml? I do NOT need the number of sheets. I need the number of pages within a sheet.
Background:
We have to export multiple worksheets into a pdf file. Therefore we have to create a table of content.
Thanks!
EDIT-------
This is how it can be achieved via Office Interop:
var app = new MsExcel.Application();
var wb = app.Workbooks.Add(fullPath);
foreach (Worksheet sheet in wb.Sheets)
{
sheet.Activate();
string name = sheet.Name;
int pages = sheet.PageSetup.Pages.Count;
}
It would be gread if someone knew a solution with OpenXml, ClosedXml or any other free library.
Thanks again!!!

Excel formula attached to Column but value is not rendered

i have been trying to download an excel workbook from windows application(C#) with data from DB. i have few sheets that have static data and few sheets formulated with data from DB. There are few column where i have formatted excel formulas.
When i download the sheet that contains only DB data, the formulas columns have accurate value rendered in them. But while downloading with Static Data Sheets, the formulas is bind with columns but value is not rendered. On clicking on any cell for edit in the sheet,the values are rendered.
i am using NPOI dll. For adding the static data sheets, i have been initializing the excel to be downloaded with static field sheets and adding the remaining sheets.
IWorkbook hssfwb;
ISheet sheetObj;
//--- read existing data
using (FileStream file = new FileStream(Existingfile, FileMode.Open, FileAccess.Read))
{
hssfwb = new XSSFWorkbook(file);
file.Close();
}
//-- adding sheets from DB data
sheetObj = (XSSFSheet)hssfwb.CreateSheet(dtSet.Tables[sheetcount].TableName);
I've faced the click and display problem before.
There is a ForceFormulaRecalculation property on ISheet, you can try to set it to true.

How to read and import data from Excel to database using EEPLUS

I want to read data from excel using EEPLUS in C#. Who can teach me or share the link to teach it? Thanks for reading!
It is really easy to use, just read through the GetStarted of their GitHub.
Example:
//Open the workbook (or create it if it doesn't exist)
var fi=new FileInfo(#"c:\workbooks\myworkbook.xlsx")
using (var p = new ExcelPackage(fi))
{
//Get the Worksheet created in the previous codesample.
var ws=p.Workbook.Worksheets["MySheet"];
Set the cell value using row and column.
ws.Cells[2, 1].Value = "This is cell A2. It is set to bolds";
//The style object is used to access most cells formatting and styles.
ws.Cells[2, 1].Style.Font.Bold=true;
//Save and close the package.
p.Save();
}

How can I change the Row-Color of an excel-row with NetOffice?

I'm struggling with a simple problem, but I can`t figure it out.
I have an excel document, which I do some processing with (using the NetOffice API). This works fine, but I want to change the row-color after the processing, so each row within the range should have the same color after being processed.
Im getting a COMException (HRESULT: 0x800A03EC) with the following code:
foreach (Excel.Range row in rg)
{
//do the processing...
...
row.Interior.Color = XlRgbColor.rgbAliceBlue;
}
I also googled for this HRESULT and tried to solve the problem, within the
Open()-Method by setting readOnly to false and editable and corruptLoad to true. That didn`t
work. I have also tried to set the interactive property to true and saved the excel file in different formats (.xls, .xlsx) but nothing worked out.
I found that the excelfile/workbook is protected. So I tried to unprotect the ActiveWorkbook like so
app.ActiveWorkbook.Unprotect();
But this also went wrong and throws a COMException that the unprotect-property of the Workbook object can not be assigned.
I hope that someone can help me with that.
Thanks in advance,
Cordell
On Workbook open pass read only parameter to false and also pass password of excel file.
Excel.Workbook workBook = excelApplication.Workbooks.Open(sMyExcelPath,0,
False,5,123,123,True,XlPlatform.xlWindows,"\t",False,False,0,True,1,0)
You can change color of Row-Color of excel-row using below code.
Excel.Worksheet workSheet = workBook.Worksheets(1);
workSheet.Rows.Interior.Color = XlRgbColor.rgbAliceBlue;
To Set Border Try below codes:
workSheet.Rows.Borders(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlDouble;
workSheet.Rows.Borders(XlBordersIndex.xlInsideHorizontal).Weight = 4;
workSheet.Rows.Borders(XlBordersIndex.xlInsideHorizontal).Color = ToDouble(Color.Black);
Above code change color of all rows of excel sheet. If you want to change only used range colors then try with below code.
workSheet.UsedRange.Interior.Color = XlRgbColor.rgbAliceBlue;
To set Border Try below Codes:
workSheet.UsedRange.Borders(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlDouble;
workSheet.UsedRange.Borders(XlBordersIndex.xlInsideHorizontal).Weight = 4;
workSheet.UsedRange.Borders(XlBordersIndex.xlInsideHorizontal).Color = ToDouble(Color.Black);

Does ClosedXML support setting a worksheet's zoom level?

I am using closedXML to generate an Excel file from C# and I am trying to see if there is anyway to set the zoom level of a worksheet. I can't find this in any of the documentation or from googling?
Does anybody know how to do this?
It's now possible, starting with ClosedXML version 0.87.0, via the IXLSheetView.ZoomScale property.
using Excel = ClosedXML.Excel;
var wb = new Excel.XLWorkbook();
Excel.IXLWorksheet ws = wb.AddWorksheet("zoom");
Excel.IXLSheetView view = ws.SheetView;
/* Value can be set between 10 and 400 */
view.ZoomScale = 85;
You can check the IXLSheetView source code for more information.
Update for version 0.87+: https://stackoverflow.com/a/52755386/2610249
No, ClosedXML does not support setting the zoom. The option that johny links to is only for scaling of the pages when printing.
There is a feature request on the ClosedXML page, but no answer from the developer.
As previously answered, you can't, but I've found a way around it which works well:
Create a template Excel file in advance, with all sheets' zoom levels set to how you want them.
When you create your workbook, instead of:
public XLWorkbook CreateWorkbook()
{
XLWorkbook workbook = new XLWorkbook();
IXLWorksheet worksheet = workbook.AddWorksheet("First sheet");
// ...
return workbook;
}
do this:
public XLWorkbook CreateWorkbookWithZoom()
{
XLWorkbook workbook = new XLWorkbook(#"C:\your template file.xlsx");
IXLWorksheet worksheet = workbook.Worksheet(1);
worksheet.Name = "First sheet";
// ...
return workbook;
}
where C:\your template file.xlsx is the path of your template file.
I think you can also handle having a variable number of sheets, by copying existing (blank) worksheets instead of creating new ones. You can get creative with having different blank template worksheets to choose from, if you need to set the zoom level dynamically.
A pull request for this has been logged at https://github.com/ClosedXML/ClosedXML/pull/180

Categories

Resources