したがって、リストボックスとボタン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;
}
}
}
私が今やりたいことは、ボタンを削除して、ユーザーが名前の1つを選択し、削除ボタンの場合、選択した名前の行のすべての情報を削除することです。たとえば、セルA2には、リストボックスに表示される名前が含まれています。ユーザーが削除ボタンを押すと、すべて削除されます行 2 の情報。