I'm using NPOI 1.2.5 with C# to generate some worksheet.
I need to set my sheet "Scaling mode" to "Reduce/enlarge printout" (see the last option of this LibreOffice screenshot)
No matter how hard I try, my generated sheet always have "Fit print rage..."
I'm stuck on this problem since the last summer, so any help is really appreciated.
Thanks!
myWorksheet.FitToPage = false; did the trick!
Related
I am doing the following in ClosedXML (standard accounting format for numbers):
ws.Column(col).Style.NumberFormat.Format = "_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(#_)";
When the output renders, it doesn't look like what I specified. If I inspect the format, it has been changed to this:
[$$-en-US]#,##0.00_);([$$-en-US]#,##0.00)
Close, and passable, but not what I input.
Anyone know what gives? Is this ClosedXml or is it Excel? I can do it manually in Excel, and it works perfectly.
Looks like Excel is doing some magic on opening a file, propably for localization compatibility. Also on editing the cell format some sort of 'assistance' happens.
Following code should work for you or at least point you in the right direction:
ws.Column(col).Style.NumberFormat.Format = "_([$$-en-US]_(#,##0.00_);_([$$-en-US]_((#,##0.00)_);_(-_);_(#_)";
Please note that on editing the format afterwards in Excel it will likely 'assist' you again and propably mess up. You will find the original format string under 'Userdefined', tho.
Help on format strings is also provided by Microsoft.
As per Current requirement I need to read value from the cell using EPPlus. This cell contains the formula and showing value correctly in XL Sheet. but when i am reading that value using EPPlus some cells are giving correct value but some cells are giving error "#VALUE!". I have used ".Calculate()" before read the value but still facing the same problem. Please find the code below in c#.
totalRecycleWorksheet.Cells[row, colval + 5].Style.Numberformat.Format = "#";
totalRecycleWorksheet.Cells[row, colval + 5].Calculate();
var value = totalRecycleWorksheet.Cells[row, colval + 5].Value;
if (!totalRecycleWorksheet.Cells[row, colval + 5].Value.ToString().Equals("#VALUE!")) {}
and here is the formula in every cells:
=IF(('Failure Item'!E348+ROUNDUP(('Output'!E348)*$B$1,0)-'Purchased Items'!F348)>0,('Failure Item'!E348+ROUNDUP(('Output'!E348)*$B$1,0)-'Purchased Items'!F348),0)
and values are as per the screenshot:
Also you can check the Output I have stored in datatable to check the value:
The only examples I see call Calculate at the workbook level such as
excelPackage.Workbook.Calculate();
I had a similar problem. In my case the Excel workbook was a macro-enabled (.xlsm) file. It was macro enabled because I had made use of VBA functions.
When reading Excel.Range.Value2 property from cells the numerical result was consistently -2146826273. I searched this error code as the Hex (800A 07DF) with no luck, but eventually used a bit of debugging to find it resulted from Excel outputting #VALUE! in the cell I was trying to read.
This was because the macros weren't enabled when I'd loaded it via C#, so calls to the VBA functions were failing.
I followed the advice in: Programmatically enable Excel macro in C#
to enable macros on the workbook and all my #VALUE! problems disappeared.
I am doing some automated excel manipulation using C#. I have been having a hard time figuring out how to autofilter based on a specific color.
There is very little documentation about this type of manipulation, however I have found some VB.net and VBA code for it. I cannot seem to convert the code to C# as "RGB" is not usable as it is in VB.net and VBA (See VB.net code below).
Since there has been no answers to this questions, I want to specify the code that needs to be looked at. In Autofilter(Field,Criteria,Operator), I need to know the C# Microsoft.Office.Interop.Excel criteria that would let me choose a color to filter.
Here is what my code looks like:
Excel.Worksheet xs1:
Excel.Range xRange1;
Excel.Range xRange2;
Excel.Range lastrow;
Excel.Range lastcol;
lastrow = xs1.Rows.End[Excel.XlDirection.xlDown];
lastcol = xs1.Columns.End[Excel.XlDirection.xlToRight];
xRange1 = xs1.Cells[2, 14];
xRange2 = xs1.Cells[lastrow.Row, 14];
Below selects the entire sheet and adds an autofilter(), setting it to filter for color. This works fine, but how do I pick the color I want it to filter for?
xs1.Range["A1", xs1.Cells[lastrow.Row, lastcol.Column]].
AutoFilter(14,Excel.XlColorIndex.xlColorIndexAutomatic,
Excel.XlAutoFilterOperator.xlFilterCellColor);
Here is an example of what the autofilter code would look like in VB.net. It looks very similar to this in an excel macro as well.
xs1.Range("A1", xs1.Cells(lastrow.Row, lastcol.Column)).
AutoFilter(Field:=14,Criteria1:=RGB(0,202,255),
Operator:=Excel.XlAutoFilterOperator.xlFilterCellColors)
So this is how you pick the color index for any poor souls that need to figure it out themselves. I could not find this anywhere on the internet.
xs1.Range["A1", xs1.Cells[lastrow.Row, lastcol.Column]].
AutoFilter(14, xlBook.Colors[33], Excel.XlAutoFilterOperator.xlFilterCellColor);
The important part is the "xlBook.Colors[33]". xlBook being the Workbook. 33 being the color index.
I don't have any reputation points, so I must submit an answer instead of a comment. Anyway, thank you, thank you, thank you. I have spent weeks looking for this answer and happened to search the right key words to find this post. I signed up just to upvote the answer!
My scenario is not exactly like yours, but similar enough that you led me to the solution. So, I will share what worked for me. I am trying to filter by color index using PowerShell. However, PowerShell does not allow the RGB values, as in the VB.net example above. Now, for my contribution to the answer. If you are doing this in PowerShell then it will need to look like this:
$xlFilterCellColor = 8
$xl = New-Object -comobject excel.application
$xl.Visible = $true
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Open("\\path\to\file.xlsx")
$ws = $wb.worksheets.items(1)
$xlCellTypeLastCell = 11
$used = $ws.usedRange
$lastCell = $used.SpecialCells($xlCellTypeLastCell)
$range = $ws.range(A1:$lastCell)
$range.select | out-null
$range.autofilter(1,$wb.colors(6),$xlFilterCellColor)
The operator decimal values are listed here. In the code above, I am filtering yellow colored cells. Cell index colors and values can be found here.
How to make columns to be auto width when texts in columns are long?
I use this code
Worksheet.Column(colIndex).AutoFitColumn() 'on all columns'
Worksheet.cells.AutoFitColumns()
Worksheet.Column(colIndex).BestFit = True 'on all columns'
None of these methods are working
Are there any ways to make it work?
Note: Some of my texts use Unicode.
Use AutoFitColumns, but you have to specify the cells, i assume the entire worksheet:
VB.NET
Worksheet.Cells(Worksheet.Dimension.Address).AutoFitColumns()
C#
Worksheet.Cells[Worksheet.Dimension.Address].AutoFitColumns();
Please note you need to call this method after filling the worksheet.
I have used this code with the version 3.1.3.0 of EPPlus and it is working:
worksheet.Column(1).AutoFit();
where a worksheet is the variable referencing the worksheet I have created in my code (not a class with a static method!).
Obviously you have to call this method after you have filled the columns.
Just wanted to point out you can fit cells with out specifying the range, just make sure to call this after you've formatted all columns etc:
worksheet.Cells.AutoFitColumns()
I know this is an old question, but I use the code below and it seems to directly address what you have tried to do.
using (var xls = new ExcelPackage())
{
var ws = xls.Workbook.Worksheets.Add("Some Name");
//**Add Column Names to worksheet!**
//**Add data to worksheet!**
const double minWidth = 0.00;
const double maxWidth = 50.00;
ws.Cells.AutoFitColumns(minWidth, maxWidth);
return pkg.GetAsByteArray();
}
I know is a little bit late but I've had the same problem today. If you have a worksheet.DefaultColWidthdefined, it won't work. I've removed that line and added Worksheet.cells.AutoFitColumns(); and it works now.
It's working just fine for me.
Try:
ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");
wsSheet1.Cells[wsSheet1.Dimension.Address].AutoFitColumns();
ExcelPkg.SaveAs();
The .NET Core as a successor of .NET doesn't support anymore the function autofit cells with EPPplus library.
worksheet.Cells.AutoFitColumns();
or
worksheet.Column(1).AutoFit();
causes exception:
"System.Drawing is not supported on this platform."
The System.Drawing assembly is dependent on GDI and Windows specific libraries which have to be replaced by another solution. The solution for this issue is to me unknown.
Had to use worksheet.Column(1).AutoFit(0); AutoFit() wasn't doing the trick.
You will need to calculate the width. There is no autosizing function in the library that will work as you intend.
Autofitcolumn will not work with wrapped text and cells with formulas.
Look at http://epplus.codeplex.com/discussions/218294?ProjectName=epplus for examples of how you can solve the problem.
I use this and is working well.
Dim objExcel As New ExcelPackage
Dim Sheet As ExcelWorksheet = objExcel.Workbook.Worksheets.Add("SheetName")
Sheet.Cells("B1:BN").AutoFitColumns()
I'm using late binding to launch Excel from C# and copy data into it (Type.GetTypeFromProgID("Excel.Application"), Activator.CreateInstance, InvokeMember, and all that). I have this working fine, but I can't figure out how to set the cell format, specifically to make a cell bold. anyone have any idea how to do this?
I'm not sure how to do it late binding, but if you created an Excel helper class, this is probably the easiest way do what you want (I also added in color and number formating):
using System;
using Excel = Microsoft.Office.Interop.Excel;
public class MyClass
{
public void FormatRange(Excel.Worksheet sheet)
{
Excel.Range range = sheet.Cells["1","A"];
range.Interior.ColorIndex = 15;//This sets it to gray
range.Font.Bold = true;//Sets the Bold
range.NumberFormat = "#";//Sets it to numeric
}
}
Cheers!
SpreadsheetGear for .NET will let you read, modify and write Excel workbooks without relying on Excel being installed. If you are calling very many APIs, it will also run much faster than using Excel via COM Interop. Here is an example which loads a workbook, sets a cell's font to bold and saves the workbook:
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(#"C:\tmp\MyWorkbook.xlsx");
workbook.Worksheets["Sheet1"].Cells["A1"].Font.Bold = true;
workbook.Save();
You can see a number of live samples here and download the free trial here.
Disclaimer: I own SpreadsheetGear LLC