C#和excel删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12255166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# and excel deleting rows
提问by TheNoob
I want to write from my form (in C#) to an excel spread sheet and delete certain rows if blank.
我想从我的表单(在 C# 中)写入一个 excel 电子表格并删除某些空白行。
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.
我可以完美地写入电子表格并保存它,但是假设用户将数据输入到行 a1、a2、a3 和 a4,我现在想删除 a4 和 a29 之间的所有行。
All I need is to find out how to delete a certain range of cells.
我所需要的只是找出如何删除特定范围的单元格。
Thanks
谢谢
采纳答案by DkAngelito
You can do it using a Range Object. I assume here that you are using Excel interop.
您可以使用 Range 对象来实现。我在这里假设您使用的是 Excel 互操作。
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();
回答by Mahmut EFE
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 thisweb site
欲了解更多信息,您可以访问此网站
回答by Akhila
You can specify a cell (eg. A1) and find the entire row containing that cell as a range and then can delete the row.
您可以指定一个单元格(例如 A1)并找到包含该单元格的整行作为范围,然后可以删除该行。
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
上面给出的代码将从 sheet1 中删除第一行
回答by KR Akhil
// 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();
回答by daniele3004
To remove a row you need an only simple row of code as a follow
要删除一行,您只需要一行简单的代码,如下所示
xlWorkSheet.Rows[NUMBER_ROW].Delete();

