I understand that the question stupid and from FAQ, but i cant set auto width in excel columns (using ClosedXML.Excel library)
my code:
var wb = new XLWorkbook();
var wsDep = wb.Worksheets.Add("MyWorksheet");
wsDep.Columns("A").AdjustToContents();
wsDep.Columns("B1").AdjustToContents();
wsDep.Columns().AdjustToContents();
but nothing changes.
how can i set auto width columns with ClosedXML.Excel library??
Your cells don't have any contents.Therefore AdjustToContents() won't have any effect.
wsDep.Columns().AdjustToContents();
You should write this code at the end of your code section because it's working only when you write it at the end of your code.
You need to give the range of the columns.
Example:
wsDep.Columns("A","H").AdjustToContents();
Adding to an old post in case someone comes across this one like I did. My fix was to use the worksheet.Columns().AdjustToContents(); but I had to add it after I wrote out all of the data. Initially I was setting it when I was setting up all of my column headers and colors during up creation of the worksheet and it wasn't working. I then came across this thread and it made me think that it was adjusting the contents at the point that I was calling the AdjustToContents which in my case have anything more than the column headers at that point. I moved it to the end after writing out all of my data and it worked as expected and actually makes sense as to what it was doing.
Related
i have a small winforms application im working on and using ClosedXML to handle our excel files. Im trying to build the read logic in a way that no matter what row the headers are on, i can find that row and work with the data below that. Because our reports come from our enterprise reporting system, the files are not always the same in where they start with the data because the exports from our system appends the report filters and selections to the top x rows then below that it starts the data dump. So right now that only way i can get it to work is if i manually remove all those rows at the top and make the header row the first row.
Im looking for some assistance in how i can find the "header" row based on column names or any other method. I have already looked thru their wiki https://github.com/ClosedXML/ClosedXML/wiki but that only has mention of working with printing headers and footers..
Here is where i believe i need to focus my work, but unclear where to start:
// Look for the first row used
var firstRowUsed = ws.FirstRowUsed(); //{'Precision Calculator D'!A1:XFD1}
//var firstRowUsed = "'Precision Calculator D'!A9:XFD9";
// Narrow down the row so that it only includes the used part
var udasRow = firstRowUsed.RowUsed(); //{'Precision Calculator D'!A10:A10}
//var udasRow = "'Precision Calculator D'!A10:A10}";
// Move to the next row (it now has the titles)
udasRow = udasRow.RowBelow();
There are reports ive tried that have the header starting on row 5 and others that start on row 7 and so on, so there is no actual row that they will alays be on, so need to find a way to determine it automatically. is there anyway to determine the row that the column names are in? The columns will always be in the same order, so those i have determined.
So ran across this in a mention of closedXML and it def may help get me where i need to be, but unclear how to implement
var foundMonth = ws.Search("Month", System.Globalization.CompareOptions.OrdinalIgnoreCase);
Since it returns a IEnumerable there is a chance that there may be more than one cell with the value "Month" and in my file that im testing with, there is 2 rows that contain the word and not sure how i can determine in this case that i want the last cell it found if there are multiple.
Addressed the concern about the multiple cells returned, and can now determine which row the headers are on with the following:
var foundMonth = ws.Search("Month", System.Globalization.CompareOptions.OrdinalIgnoreCase);
var monthRow = foundMonth.Last().Address.ToString();
Still unclear how to implement this into the original code post above, so that the firstRowUsed is reflected correctly in this case would be A11:XFD11
After exhausting search of ClosedXML and reading thru a number of other questions, i was able to find a solution. Below is the code that will help set the used range based on my current data structure within the file..
var foundMonth = ws.Search("Month", System.Globalization.CompareOptions.OrdinalIgnoreCase);
var monthRow = foundMonth.Last().Address; // A11
var lastcell = ws.LastCellUsed().Address; // BC3950
var rangeUsed = ws.Range(monthRow, lastcell);
Since i have no idea where my header row will be from file to file, im searching for my column header name in column A, since all the usable data is mostly numbers i can safely assume that in column A, the last found instance of the word "Month" is my header row.
With that and the last cell used i am able to determine my data range as seen above. Although i still need to figure out how to replace my firstRowUsed logic to work the same way, this is a step closer to a final solution. Ill post back my findings on that one before i mark this question answered.
var firstRowUsed = ws.Range(monthRow, lastcell).FirstRowUsed();
This line provides you the same as this line below
var firstRowUsed = ws.FirstRowUsed();
I tried this logic with 3 different files, each one having more and less data and also having the header row on different rows. and works like a charm
I have some data that is displayed in a pivot table, from that pivot table I currently copy and paste them in another table with the corresponding current day, with so many operation numbers it takes about 1hr to complete this process daily, I'm trying to make a macro in which will help me in making this process in less time
This is the pivot table in which I copy the data
and here is where I paste it (in this table I use it to create some graphs)
Both tables consist of many different numbers of operation, as you can imagine this is very tedious.
Any help or ideas that can help me solve this is appreciated, I'm willing to try some other alternatives like fomulas, vba (preferred), even closedxml for c#.
I dont know if im explaining my self?
This is my logic.
Look for operation number
Copy Sum of "Sx"
Look in other sheet for the same operation number.
Paste value in corresponding day.
Look for same operation number with next "Sx"
.
.
End
Right now I'm just focusing on Operation1, since in Operation2 I use another sheet in which I do the same thing. (will probably use the same code as Operation1).
To clarify I'm not asking for someone to just give me the solution (although it is appreciated ), but any guide on this is helpful.
You do not need VBA to do this. Use GETPIVOTDATA.
Consider below replicated data:
In above sample, I copied some of your data in your screen shot, made a pivot out of it, put it in the same sheet and also put the table you need to populate below it. Then I use GETPIVOTDATA. I feel I need to show you how it is done even though it is well explained in the link I posted.
So we use this formula:
=GETPIVOTDATA("Sum of "&$I18,$I$2,"Date",J$17,"Opt",$I$16,"Id",$J$16)
in Cell J14. Take note that instead of using Day 1, Day2.. etc on your destination table, we used the actual date. Why? Because we need that in our GETPIVOTDATA Formula. Then copy the formula to the rest of the cells.
Result:
Now, GETPIVOTDATA errors out if it does not find anything which matches the criteria you supplied, so you might want to incorporate error handling using IFERROR statement.
Final Formula:
=IFERROR(GETPIVOTDATA("Sum of "&$I18,$I$2,"Date",J$17,"Opt",$I$16,"Id",$J$16),0)
Although you prefer VBA, I don't think a better approach than the built-in Excel functionality is more suitable in your case.
Im creating an xlsx-file using EPPlus and want to have autofilters for all headers. I set the autofilters like this:
worksheet.Cells["A3:G" + (3 + data.Count).ToString()].AutoFilter = true;
This works smooth for just one table of content. However, I have two separate tables in the same worksheet, and when trying to set autofilters for the second one, the autofilters for the first one disappears.
Any known workarounds for this, or other suggestions?
This is a very good question!
A traditional limitation of AutoFilter is that it can be applied only once on a worksheet.
Starting with Excel 2007 you can create several Tables on a worksheet...........each with filtering capabilities.
See Pieterse's Article
Hi I want to add validation to every row in a column of an excel file. I'm using EP Plus for handling the creation and reading of excel files. Here is the code I have tried.
var codeValidation = codeListSheet.DataValidations.AddTextLengthValidation("A2:AN");
But this isn't working it says that it overlaps with the range of my next column
var paretnCodeValidation = codeListSheet.DataValidations.AddTextLengthValidation("B2:BN");
I know there should be an easy way of doing this but I can't find the answer. Hopefully there is someone who has come across this before.
OK my bad the answer was in the FAQ on the EP Plus
To Select an entire column you can use A:A or B:B etc
My worksheet has a number of tables. I need to manipulate one in particular. Of course I could simply look at its cell numbers and manipulate it cell by cell, but is there any "get" function to "get" the table programatically?
And if so, how would I then manipulate the cells? Is there a command to "get" the entire header, or an entire column by its header (rather than its column number)/
If anyone knows of a guide that explains this, that would be sufficient. I tried googling it but all the results are about database tables, i.e. populating a spreadsheet from a DB.
Swift, I'm working on something similar and I ran across this article which helped me.
In short, references to tables can be found in a worksheets list objects. Iterating through the list object and requesting the range or datarange should get you what you need.
I also found this article which has very similar info.
All these examples are in VBA or VB but I found it not to difficult to port over.
-M
When you say a table, I assume you mean a range. The following code will get a range from within an VS2010 Excel Add-In -
Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
Excel.Range myRange = activeWorksheet.get_Range("A1", "D20");