How to change index of columns in MS excel using interop?
Say I want to move column C to column A position
I wonder how to do this programatically using excel interop
Try This..
// cut column c and insert into A, shifting columns right
Excel.Range copyRange = xlWs.Range["C:C"];
Excel.Range insertRange = xlWs.Range["A:A"];
insertRange.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, copyRange.Cut());
Related
I'm creating two worksheets with openxml, I want to add a hyperlink from a cell in sheet 2 to a cell in sheet 1. I know that with Microsoft interop excel, you could use a match function to find the row number where a value is found. I was wondering how could the same task be done with openXML instead.
With Microsoft interop excel, I did something like this
var excelApp = new Application();
WorksheetFunction function = excelApp.WorksheetFunction;
var rowNumber = function.Match(currentItem.originID, sheet1.Range["B1","B" + rowCount], 0);
Where sheet1 is the sheet I'm searching through, and rowCount is how many rows there are in that sheet. This would find currentItem.originID in the range of column B and return the row number that the match was found
How could I do something similar to this but with openXML?
How to merge a rang Excel in C# Interop?
I want to merge all from A1 to C3.
I do
Rang rang = Cells[1,3] as Rang;
But cant work.
A B C
1|----|----|----|
2|----|----|----|
3|----|----|----|
|----|----|----|
yourWorksheet.Range[yourWorksheet.Cells[rowBegin,colBegin], yourWorksheet.Cells[yourWorksheet.rowEnd, colEnd]].Merge();
Row and Col start at 1.
I am trying to insert a formula into an excel column using EPPlus and evaluate it in Excel. To be clear: no, I don't need the result at the runtime of the program.
This is my code:
using (ExcelRange range = worksheet.Cells[1, 1, rowCounter - 1, worksheet.Dimension.End.Column])
{
ExcelTable table = worksheet.Tables.Add(range, $"someName");
table.ShowHeader = true;
// insert calculated column
ExcelTableColumn column = table.Columns[0];
column.Name = "Remaining Runtime";
column.CalculatedColumnFormula = "=DAYS([DateOfRepayment],TODAY())";
}
The range includes a column with the header DateOfRepayment, the formula works if I select a field and then deselect it or when I just select the formula in the cell in Excel and hit enter. Before these steps, I have an Invalid Name Error referencing the [DateOfRepayment]. However, using just =[DateOfRepayment] as the formula works fine.
How can I insert this formula in EPPlus so that it will work in Excel?
(I am using EPPlus version 4.5.1)
At the moment of this answer, the formula you are referring "=DAYS()" is not supported by EPPlus. I know you are not looking for the result at the runtime of the program. But you can either replace that formula by the one that is supported by EPPlus or generate result at runtime.
https://github.com/JanKallman/EPPlus/wiki/Supported-Functions
I have found two ways those were being used by people if we want to read or write to a cell in an excel.
Declaration:
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook srcWorkBook = ExcelApp.Workbooks.Open(#"C:\test.xls");
Excel.Worksheet srcWorkSheet = srcWorkBook.Worksheets[1];
Excel.Range srcRange = srcWorkSheet.UsedRange;
Usage 1:
srcWorkSheet.Cells[1, 1].value2 = "foo bar";
Usage 2:
srcRange.Cells[2, 2].Value2 = "foo bar";
Which one is the best way to use ? or it's all fine in .NET ?
The two ways are very different.
srcWorkSheet.Cells[1, 1].value2 = "foo bar";
The 1. usage refers to the A1 cell of the first worksheet.
MSDN Worksheets.Cells property Excel
srcRange.Cells[2, 2].Value2 = "foo bar";
The 2. usage takes the cell on the 2. row, 2. column of the UsedRange. If the UsedRange in Excel is B2:D5, it would put value at C3:
MSDN Worksheets.Range Property Excel
Having a variable named Range as the Range class is really not a great idea. The same goes for WorkSheet and WorkBook.
Either option will work, but they get messy as soon as you have to modify the layout of the worksheet.
If it's an option, I like to add named ranges. This works particularly well if you're using a template where you can modify the "starting" state of the workbook.
Let's say you have a series of columns, and one of them contains the "foo" value. You can add a name like "fooColumn" to the entire column or the top cell in the column.
Then you can get the column number using
worksheet.Range("fooColumn").Column
That protects you from having to manually edit column or row numbers all over the place if you move elements around on your worksheet.
And to second what others have said, use EPPlus instead of Interop. You can still use named ranges. But EPPlus is good and Interop can bring all sorts of pain when COM objects aren't released. Interop automates an application that manipulates the file. EPPlus just works with the file.
In C# using asp/MVC the app generates an Excel .xlsx file based on data thats filled in to the specific columns, works great.
But the goal is to provide additional worksheets that use the same columns but sort on specific colums, such as Column "J"
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Proj Info");
var ws2 = wb.Worksheets.Add("Sort By Dates");
The worksheet ws has values filled in by variables or formulas, the data is correct, but cannot make it sort on a column
ws.AutoFilter.Column("J"); //no, nothing changes
ws.Column("J").Sort(); -> this shifts all the columns up but does not sort
ws.Column("J").Sort(XLSortOrder.Ascending); ->same, doesnt sort only shifts
Update: ws.Sort(9); worked in sorting, but the problem is that Column 10 has a Formula, and I need to sort on that Column.
ws.Cell("J" + c).FormulaR1C1 = "=C$2-F" + c;
With this? it WILL NOT SORT. The ws.Sort(10); works when the cell contains a final value, but when its got the Formula? Is there any workaround to force the Excel page to sort after its implemented the formula's in each cell?
You are not sorting the table, but the values in column J.
Try this:
ws.Sort("Column10");