简体   繁体   English

C#列表框和Excel如何在Excel中删除信息?

[英]C# listbox & excel how to delete info in excel?

So I have winform with listbox and button1(update) so when i press button1 it opens excel document finds info that I need and populates listbox. 所以我有带列表框和button1(update)的winform,所以当我按button1时,它会打开excel文档,查找我需要的信息并填充列表框。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string FName = @"c:\TEST\data.xlsx";
            var excelApp = new Excel.Application();
            excelApp.Visible = true;

            Excel.Workbook excelbk = excelApp.Workbooks._Open(FName,
               Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
               Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
               Type.Missing, Type.Missing);

            Excel.Worksheet xlSht = (Excel.Worksheet)excelbk.Worksheets["Sheet1"];

            //find Column Number
            //Now find Test in Row 1
            Excel.Range column = (Excel.Range)xlSht.Columns[1, Type.Missing];
            string FindWhat = "name";
            bool MatchCase = true;

            Excel.Range FindResults = column.Find(FindWhat, Type.Missing,
                Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole,
                Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlNext,
                MatchCase, Type.Missing, Type.Missing);


            int colNumber = FindResults.Column;

            //Get Last Row of Data
            Excel.Range xlRange = (Excel.Range)xlSht.get_Range("A" + xlSht.Rows.Count, Type.Missing);
            int LastRow = xlRange.get_End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row;

            //start update
            listBox1.BeginUpdate();

            //read data into form
            string CellData;
            for (int RowCount = 2; RowCount <= LastRow; RowCount++)
            {
                xlRange = (Excel.Range)xlSht.Cells[RowCount, colNumber];
                CellData = (string)xlRange.Text;
                listBox1.Items.Add(CellData);

            }

            //end update
            listBox1.EndUpdate();

            object SaveChanges = (object)false;
            excelbk.Close(SaveChanges, Type.Missing, Type.Missing);
            excelApp.Quit();
            excelApp = null;
        }

    }
}

What I want to do now, delete button, so that user selects one of the names and for delete button delete all info in row with selected name for example cell A2 contains name that is shown in listbox, when user hits delete button it deletes all info in row 2. 我现在要执行的操作是删除按钮,以便用户选择其中一个名称,然后使用删除按钮删除具有选定名称的行中的所有信息,例如,单元格A2包含列表框中显示的名称,当用户单击删除按钮时,它将删除所有第2行中的信息。

You can find use the WorksheetFunction.Match function to get the row that a user is on, and from there you should be able to delete that row. 您可以找到使用WorksheetFunction.Match函数来获取用户所在的行,然后应该可以从该行删除该行。

double Match(
    Object Arg1, 
    Object Arg2, 
    Object Arg3
)

Parameters 参量

Arg1 Type: System.Object Lookup_value - the value you use to find the value you want in a table. Arg1类型:System.Object Lookup_value-用于在表中查找所需值的值。

Arg2 Type: System.Object Lookup_array - a contiguous range of cells containing possible lookup values. Arg2类型:System.Object Lookup_array-包含可能的查找值的单元格的连续范围。 Lookup_array must be an array or an array reference. Lookup_array必须是数组或数组引用。

Arg3 Type: System.Object Match_type - the number -1, 0, or 1. Match_type specifies how Microsoft Excel matches lookup_value with values in lookup_array. Arg3类型:System.Object Match_type-数字-1、0或1。Match_type指定Microsoft Excel如何将lookup_value与lookup_array中的值进行匹配。

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.worksheetfunction.match.aspx http://msdn.microsoft.com/zh-CN/library/microsoft.office.interop.excel.worksheetfunction.match.aspx

Extra stuff: In the spirit of being more helpful, let's say your excel sheet has column headers in the first row, and the names in the first column, starting at row 2. So something like, 多余的东西:出于提供更多帮助的精神,假设您的excel工作表的第一行具有列标题,而第一列的名称则从第2行开始。因此,

Row 1: Name 第1行:名称

Row 2: Steve 第2行:史蒂夫

Row 3: John 第3行:约翰

Row 4: Adam 第4行:亚当

Row 5: Randy 第5行:兰迪

If you used Match("Adam", A2:A5, 0) , the returned value would be 3, because within your range of A2:A5, it was the 3rd row, so the row you'll actually want to delete is 4. Gotta account for these kinds of things. 如果您使用Match("Adam", A2:A5, 0) ,则返回值为3,因为在您的A2:A5范围内,它是第三行,因此您实际上要删除的行为4必须考虑这类事情。 You could include A1 in your range, and that could work in most cases. 您可以将A1包括在您的范围内,并且在大多数情况下都可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM