How does one delete a column (or multiple columns) in Excel?
eg. How to delete column C and shift the rest left?
Here is the solution to make it clearer (thanks to Leniel for the link)
Excel.Range range = (Excel.Range)sheet.get_Range("C1", Missing.Value);
range.EntireColumn.Delete(Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
This was the first result I hit and deleting a column in Excel doesn't need as much code as the current answers suggest. In fact (assuming you have a Worksheet object already, listed below as mySheet) all that is needed for the original question is:
mySheet.Columns["C"].Delete();
If you want to delete multiple columns then:
mySheet.Columns["C:D"].Delete();
You can specify a variable in the Delete method (see https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.xldeleteshiftdirection?view=excel-pia) i.e. mySheet.Columns["C"].Delete(xlShiftToLeft)but there's no need as the Delete method is smart enough to realise that the Range you are selecting is a single column, so will do this automatically.
You can also uses a numeric value to designate the column i.e. mySheet.Columns[2].Delete()
Here you find how to do it:
http://bytes.com/topic/c-sharp/answers/258110-how-do-you-delete-excel-column
http://quicktestprofessional.wordpress.com/2008/02/14/delete-columns-from-xl-sheet/
Related
I use EPPLus to create a excel file with a table in it. Creating the excel file and table works like a charm using:
worksheet.Cells["A1"].LoadFromCollection(rows, true, TableStyles.Light12);
...but when I try to insert a formula into the first empty row after the table, the file becomes corrupt and cannot be opened normally. Excel can recover it though, but the formula I added will never show.
I use this line of code to create the formula:
worksheet.Cells["E"+(rows.Count+2).ToString()].Formula = "=SUBTOTAL(109;[TurnoverCurrent])";
I've already confirm that I'm targeting the correct cell, and if I insert just a string instead of a formula, it works. Hence my feeling is that I don't use the formula method correctly. How can I achieve a SUBTOTAL formula after my table?
Three things jump out at me, and I'm not sure if either/or will fix it, but I thought to offer.
1) your subtotal formula is right next to the table -- when Excel renders the XML, is it trying to add this to the table and freaking out because it's self-referencing? In other words, the subtotal formula is now part of the table and adding itself?
2) when I use subtotal, I use a comma, not a semicolon
3) you didn't reference the table name in your formula. Since this is a brand new sheet, it's safe to try Table1. If you really want to be fancy, you can look up the table and find its name. I wouldn't.
So maybe add one row, prefix the table name and change your semicolon to a comma?
worksheet.Cells[rows.Count + 3, 5].Formula = "=SUBTOTAL(109,Table1[TurnoverCurrent])";
Change
worksheet.Cells["E" + (rows.Count+2).ToString()].Formula = "=SUBTOTAL(109;[TurnoverCurrent])";
to
worksheet.Cells["E" + (rows.Count+2).ToString()].Formula = "SUBTOTAL(109;[TurnoverCurrent])";
Do nut use the "="
I've 2 excel sheets(Sheet1 & Sheet2) in my Excel workbook. I want to copy row data from Sheet2 to Sheet1.
Condition is:
if Sheet2 copied row doesn't exist in Sheet1 then paste it otherwise don't paste the row.
Copied Rows except 1st row in Sheet2:
Range dataWithoutFirstRow = xlAccrualSheet.Range[xlAccrualSheet.UsedRange.Cells[2, 1],
xlAccrualSheet.UsedRange.SpecialCells(XlCellType.xlCellTypeLastCell)];
dataWithoutFirstRow.Copy();
Paste in below used range in Sheet1:
Range DataRange = xlAccrualWorkSheet.Cells[emptycell, 1];
DataRange.PasteSpecial(XlPasteType.xlPasteAllUsingSourceTheme);
Please Tell me How to check already exist rows in Sheet1.
Awaiting for Your Response
Please Tell me How to check already exist rows in Sheet1.
Use Range to read out all data from the workSheet. You are already doing a similar thing accrualSheet. Then, you can use range.Cells(i,j) or range.Rows(r).Value or even range.Rows(r).Cells(i,j) to get the data inside each specific row.
When pasting occurs, loop over all pasted rows, and for each pasted row compare it with rows from workSheet. You may do it directly by reading that on the fly (as mentioned above), or you may read all rows from workSheet and store them in a List and compare incoming rows against that list - it will work much faster that way.
Now, one of the most interesting things is probably what does it mean to "compare rows". Either you will need to compare all cells within a row with another one, or you will need to compare just a specific set of "columns" like "date, time, cause, origin, caseId" etc. But that.. noone knows, you said nothing about that. If there's no info on that, then you probably should compare whole rows and assume that any difference in any cell means that the rows are different.
I need to insert a row into an existing Excel worksheet using C# but keep the existing formatting.
The default behaviour in Excel is to use the formatting of the row above, which in some cases is what I don't want as this could be different.
I am sure I have seen that there is a way to specify using the format of the row below rather than from the row above but I can't find it now.
Does anyone know how to specify this?
This is the code I am using:
sheet.Range[string.Format("{0}:{0}", row)]
.EntireRow
.Insert(
Excel.XlInsertShiftDirection.xlShiftDown);
You need to use the CopyOrigin parameter as well.
sheet.Range[string.Format("{0}:{0}", row)]
.EntireRow
.Insert(
Excel.XlInsertShiftDirection.xlShiftDown,
Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow
)
See here for details on Insert and that enum.
It is possible to get this parameter by recording a macro. There is a small box that pops up when you insert the row to choose where the formatting comes from.
I am using ExcelDNA to set and get cell values. I can get and set cell values
var ref = ExcelReference(2, 2);
var val = ref.GetValue();
ref.SetValue(42);
is there a way to insert an entire row or column by moving entries to the right or down? I want the same behavior as when the user right clicks the column and all the entries are shifted to the right. Solution can use NetOffice if necessary.
I'd recommend using the COM object model for this, and the code will be similar to VBA for the same task.
You get hold of the root Application object with a call to ExcelDnaUtil.Application. The resulting object will be of type Microsoft.Office.Interop.Excel.Application and can be used to select the row of column, then call app.Selection.Insert() to insert the new row or column.
It should also be possible using the C API, but that is unlikely to be easier or faster.
I would like to add that NetOffice does not support the EntireRow and EntireColumn methods of the Range object, which would be useful for inserting or deleting full rows. As a workaround, one can replace this for rows by addressing full rows by Range(rowNoStart + ":" + rowNoEnd).
For columns, one can write Range(GetExcelColumnName(colStart) + ":" + GetExcelColumnName(colEnd)), where GetExcelColumnName is a function from this former SO post.
This code is supposed to get all rows in the range that I specify, and delete ONLY the rows with no cell DATA in them. It's actually deleting every row in the range though. Why?
Range range = _sheet.get_Range("A25:A542", Type.Missing);
range = range.EntireRow;
range.Delete(Type.Missing);
Type.Missing doesn't mean what you think it means. Type.Missing is a COM artefact - it just tells the Excel object that you're not providing that particular parameter. It's the kind of thing that's normally taken care of for you in VB.NET and VBA. C# 4.0 has support for optional parameters, which makes things much easier.
You do not check if any DATA exists, so the program deletes all rows from line 25 til line 542.