所以我有带有列表框和按钮1(更新)的winform,所以当我按下按钮1时,它会打开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;
}
}
}
我现在要做的是删除按钮,以便用户选择其中一个名称,然后删除按钮删除具有所选名称的行中的所有信息,例如单元格 A2 包含列表框中显示的名称,当用户点击删除按钮时,它会删除所有第 2 行的信息。