所以我有列表框和按钮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行中的所有信息。