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
Related
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();
}
Using ClosedXML (v0.93.1) on .Net Core 2.0, attempting to save a workbook so that when opened it prompts the user to open as "Read Only" or enter the protected password. (See linked question below).
I have seen IXLWorksheet.Protect() and XLWorkbook.Protect() - these correctly protect the workbook but still default the user to open with R/W access. This is a problem because it write locks the file and my process can't overwrite it until the user closes excel.
This question indicates OpenXML (the underlying API for ClosedXML) has the ability to set this when saving a workbook, however ClosedXML XLWorkbook.SaveAs(SaveOptions) doesn't include this. Perhaps I can't find the closedxml documentation on this option, but I believe this is the option I need.
According to this answer and the attached github/MSDN links OpenXML doesn't support password protecting but I don't see any info about readonly open other than the first linked question saying it should be possible.
Code example:
XLWorkbook wb = new XLWorkbook();
var ws = wb.AddWorksheet("My Sheet");
// ... add to worksheet
ws.Protect("my-worksheet-password");
wb.SaveAs("C:\\my-workbook.xlsx", new SaveOptions { /* In OpenXML the option would exist during this save step. */ });
This feature is not supported by ClosedXML yet, but you can use OpenXml directly to set the require property. Refer to https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.filesharing.readonlyrecommended?view=openxml-2.8.1
private void GeneratePartContent(WorkbookPart part)
{
FileSharing fileSharing1 = new FileSharing(){ ReadOnlyRecommended = true };
workbook1.Append(fileSharing1);
part.Workbook = workbook1;
}
This might be the duplicate as many posts refer to this kind of issues but I could not find an exact answer to this case.
So, what I have is an Excel file where cell "E4" contains a formula "=C4+D4". All other cells are empty, which means I cannot search or get them via OpenXml, simply because they do not exist in Xml. So in order to fill the cells "C4" and "D4" I have to create them (like this:)
var cell = new Cell(){
CellReference = new StringValue("C4"),
DataType = new EnumValue<CellValues>(CellValues.Number),
CellValue = new CellValue("123")
}
the same for cell "D4" and then append these cells to the row
row.Append(cell);
After I open the excel file it show an error "Excel found unreadable content in file.xlsx. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes."
I checked and when there is no formula in excel file the cells are appended correctly.
So my question is, using OpenXml how do I append cells in Excel file, which contains formula, so that not to break the file?
So the XML is malformed and corrupted the has file, hence the error:
Excel found unreadable content in file
To troubleshoot and fix this I suggest you compare the manually created xlsx to the programmatically created xlsx.
To do this you rename both files extensions from XLSX to ZIP > extract them to different folders > use a WinDiff tool (or better the "OpenXML SDK Productivity Tool") to compare the files.
Once you find how its malformed change your code to try and fix it up.
I solved my problem using EPPLUS
using (ExcelPackage excelPackage = new ExcelPackage(fileStream))
{
ExcelWorkbook excelWorkBook = excelPackage.Workbook;
ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();
excelWorksheet.Cells["C4"].Value = "123";
excelWorksheet.Cells["D4"].Value = "321";
excelPackage.SaveAs(memoryStream);
}
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);
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);