English 中文(简体)
C# 列表框, 如何删除信息优异( expert)?
原标题:C# listbox & excel how to delete info in excel?
  • 时间:2012-05-22 13:41:59
  •  标签:
  • c#
  • excel

所以我有列表框和按钮1的双元格式(更新),所以当我按按钮1时,它会打开优异文档,找到我需要的信息,并发布列表框。

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:TESTdata.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;
        }

    }
}

我现在要做什么,删除按钮,让用户选择一个名称,并删除按钮,删除行中以选定名称删除的所有信息,例如,单元格 A2 包含在列表框中显示的名称,当用户点击删除按钮时,它删除了第2行中的所有信息。

问题回答

您可以使用工作表函数。 匹配函数来获取用户所在的行, 从该行中, 您应该可以删除该行 。

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

参数参数

Arg1 Type: System.Object Lookup_value - the value you use to find the value you want in a table.

Arg2 Type: System.Object Lookup_array - a contiguous range of cells containing possible lookup values. Lookup_array must be an array or an array reference.

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.

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workshitefyes.match.aspx" rel=“不跟随 noreferrer'>http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workshitefy.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,

行 1 : 名称

第二行:史蒂夫

第3行:约翰

第4行:亚当

第5行:兰迪

如果您使用 Match ("Adam", A2: A5, 0) , 返回值将是 3, 因为在您 A2: A5 的范围内, 这是第三行, 所以您实际要删除的行是 4 。 您可以将 A1 包含在您的范围中, 在大多数情况下, 这可以有效 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签