I want to locate the last cell with a value from the selection made by a user in Excel.
When running the code below the lastCell variable returns the last cell in the sheet instead of the last cell in the selection.
I have no idea why this is happening as I'm telling the lastCell to use my selection.
Any information would be appreciated.
My code is as follows.
Excel.Range selectedRange = excelApp.Selection;
Excel.Worksheet ws = excelApp.ActiveWorkbook.ActiveSheet;
Excel.Range lastCell = selectedRange.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
var newSelectedRange = ws.Cells.Range[selectedRange.Item[1, 1], lastCell];
Related
How do I copy an Array to an Excel Range in C#?
Why does the below code not work?
If I assign it like this, then in each cell is the same value
Workbook workbook;
Worksheet worksheet;
List<double> flow = new List<double>();
workbook = excel.Workbooks.Open(filename);
worksheet = workbook.Worksheets.Add();
worksheet.Range[$"$A$1:$A{flow.count}"].Value = flow.ToArray();
Of course the list is filled with values. I have omitted this part here.
replace your last line with
Excel.Range range = sheetSource.UsedRange.Columns[columnIndx];
range.Value = application.WorksheetFunction.Transpose(flow.ToArray());
I have a prob lem with excel and c#. When I set in the cell A1 a number like 123456789.
In all column A when the format is Number, next code is working without problems:
Workbook workbook = new Workbook(stream);
Worksheet worksheet = workbook.Worksheets[0];
var count = worksheet.Cells.Count; // count is 1
If I have column A design in format Text, converts that cell as '123456789 and then the code is wrong and returns me count=126 and when I query the cells all cells from 2 to 126 are empty or null and A1 as '1234567890
Why is happening that behavior?
Try out something like
Excel.Range range;
range = worksheet.UsedRange;
var count = range.Count;
It's a problem othersalready got.
Source : Interop Excel UsedRange Rows Count incorrect
I have a Microsoft.Office.Interop.Excel.Range object and want to estimate the end-coordinates of that range ie (last column, last row).
There are no properties like LastCol, LastRow. The only properties are Column and Row, which specify the first cell. But how can I get the last cell of the range?
there is two way to do this :
Using Worksheet.UsedRange to determine the range. This will give you a range like A1:F10, or use Worksheet.UsedRange.SpecialCells(IExcel.XlCellType.xlCellTypeLastCell) to obtain F10.
With Range.End[IExcel.XlDirection] property, it returns the last continuous no-empty cell in the specified direction. Note that if the Range is empty, it will return the first no-empty cell or last cell(when excel reach its boundaries) in the given direction. Ex: the whole column A is empty, Range["A1"].End[IExcel.XlDirection.xlDown] will be A65535(last row in excel 97-2003, A1048576 for excel 2007)
//Just In case if you are wondering what IExcel is
using IExcel = Microsoft.Office.Interop.Excel;
This should get the first first and last cell of the range:
Initiate Excel.
Open workbook.
Select your range, in this case I got the used range of the active sheet.
Call the get_address method of the range.
Split the result of get_address on the colon.
The first value of the array resulting from the split will have the beginning cell.
The second value of the array resulting from the split will have the ending cell.
Replace the dollar signs with nothing and you will have the beginning and ending cells for the range.
Microsoft.Office.Interop.Excel.Application excel = new Application();
Microsoft.Office.Interop.Excel.Workbook workBook =
excel.Workbooks.Open(fileLocation);
Microsoft.Office.Interop.Excel.Worksheet sheet = workBook.ActiveSheet;
Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
string address = range.get_Address();
string[] cells = address.Split(new char[] {':'});
string beginCell = cells[0].Replace("$", "");
string endCell = cells[1].Replace("$", "");
workBook.Close(true);
excel.Quit();
If you want the last column and last row relative to the beginning of the range you can do the following:
int lastColumn = range.Columns.Count;
int lastRow = range.Rows.Count;
Using Excel 2013, we've had lots of cases where UsedRange and xlCellTypeLastCell simply gave horribly wrong values.
For example, we'd have a worksheet containing just 7 columns, and sometimes these two functions would tell our C# code that there were 16,000+ columns of data.
The only method I found which truly worked out the bottom-right cell containing data was to use the tips in this suggestion:
sheet.Cells.Find()
I want to write from my form (in C#) to an excel spread sheet and delete certain rows if blank.
I can write perfectly fine to a speadsheet and save it, but lets say the user entered data into row a1, a2, a3, and a4, I now want to delete all the rows in between a4 and a29.
All I need is to find out how to delete a certain range of cells.
Thanks
// Here is the answers to
// 1. Delete entire row - Below rows will shift up
// 2. Delete few cells - Below cells will shift up
// 3. Clear few cells - No shifting
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(ResultFilePath);
ExcelApp.Visible = true;
Excel.Worksheet ExcelWorksheet = ExcelWorkbook.Sheets[1];
Excel.Range TempRange = ExcelWorksheet.get_Range("H11", "J15");
// 1. To Delete Entire Row - below rows will shift up
TempRange.EntireRow.Delete(Type.Missing);
// 2. To Delete Cells - Below cells will shift up
TempRange.Cells.Delete(Type.Missing);
// 3. To clear Cells - No shifting
TempRange.Cells.Clear();
You can use this. İt's working ...
_Application docExcel = new Microsoft.Office.Interop.Excel.Application { Visible = false };
dynamic workbooksExcel = docExcel.Workbooks.Open(#"C:\Users\mahmut.efe\Desktop\Book4.xlsx");
var worksheetExcel = (_Worksheet)workbooksExcel.ActiveSheet;
((Range)worksheetExcel.Rows[2, Missing.Value]).Delete(XlDeleteShiftDirection.xlShiftUp);
workbooksExcel.Save();
workbooksExcel.Close(false);
docExcel.Application.Quit();
For more information you can visit this web site
You can do it using a Range Object. I assume here that you are using Excel interop.
Let say you have your book open, then set the range then delete it
It should look something like this
ApplicationClass excel = new ApplicationClass();
//Abrir libro y seleccionar la hoja adecuada aqui
//...
Microsoft.Office.Interop.Excel.Range cel = (Range)excel.Cells[rowIndex, columnIndex];
cel.Delete();
You can specify a cell (eg. A1) and find the entire row containing that cell as a range and then can delete the row.
excel.Worksheet sheet = (excel.Worksheet)excelWorkBook.Sheets["sheet1"];
excel.Range cells = (excel.Range)sheet.Range["A1", Type.Missing];
excel.Range del = cells.EntireRow;
del.Delete();
The above given code will delete first row from sheet1
To remove a row you need an only simple row of code as a follow
xlWorkSheet.Rows[NUMBER_ROW].Delete();
using Microsoft.office.interop.excel delete rows using workseet range
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(srcFile);
Excel.Worksheet workSheet = xlWorkbook.Sheets[1];
Excel.Range range = workSheet.Rows[2];
range.Delete();
xlWorkbook.SaveAs(dstFile);
xlWorkbook.Close();
xlApp.Quit();
I am using c# to color particular cells of excel file.
I am using:
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(destPath);
Worksheet ws = wb.Worksheets[1];
ws.get_Range(ws.Cells[row, clmn]).Cells.Interior.Color = 36;
...to color cells, but this is not working.
Can anyone help me out?
Try something like that
ws.Cells[row, clmn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
Cells[row, clmn] is a range so you don't need to call get_Range() and there is a enum that you can use for colors.
ws.Cells[row, clmn].Interior.Color = XlRgbColor.rgbBlack;
If you want to set color by color index, you need to use this method:
Cells[row, col].Interior.ColorIndex = 36;
You can color a cell or a entire column or entire row.
The below code will help you out.
xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 2], xlWorkSheet.Cells[2, 4]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
else
xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 3], xlWorkSheet.Cells[2, 3]).Interior.Color = Excel.XlRgbColor.rgbRed;
Here xlWorksheet is the object excel Worksheet object.
get_Range takes 2 variable one start cell and other is end cell.
so if you specify both the values same then only one cell is colored.
xlWorkSheet.cells[row, column] is used to specify a cell.
System.Drawing.ColorTranslator.ToOle(SystemDrawing.Color.Green) is used to define the color in OLE format.
Excel.XlRgbColor.rgbRed is a excel way of coloring the cells
This method gives access to large number of colors which can be found here list of colors
The below code is the way i defined the excel worksheet.
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range xlwidthadjust; //used this to adjust width of columns
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
with this code i am sure that you wont get this exception Exception from HRESULT: 0x800A03EC
Make sure you are using:
using Excel = Microsoft.Office.Interop.Excel;
If you have a variable for the range you want to change, then use:
chartRange = xlWorkSheet.get_Range("a5", "a8");
chartRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
If you want to just change the color of a specific cell, then use:
xlWorkSheet.Cells[row, col].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
...where 'row' is the row number, and 'col' is the column number assigned to the given lettered columns (starting at 1).
Exception from HRESULT: 0x800A03EC
Solution: Change the misValue to sheet1, sheet2 or sheet3.
xlWorkBook = xlApp.Workbooks.Add("sheet1");
This works for me. system.reflaction.missing.value what was that, it is not related to Excel.workbooks.add came from a Excel file default value.
When you create a Excel file, the default worksheets are sheet1, sheet2 and sheet3.