Bonjour
Je voudrais afficher le contenu d'un fichier excel sur une listeview .
j'utilise c#. je suis bloqué je demande votre aide, merci
C#
public partial class Form1 : Form
{
public static Excel.Application? excelApp;
public static Excel.Workbook? excelBook;
public static Excel.Worksheet? excelSheet;
public static Excel.Range? excelRange;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
excelApp = new Excel.Application();
if (excelApp == null)
{
MessageBox.Show("Désolé, cette application nécessite Excel. Veuillez l'installer et réessayer.");
Application.Exit();
}
else
{
try
{
excelBook = excelApp.Workbooks.Open(AppContext.BaseDirectory + "D:\\Cours_Informatique\\Session Mars-Mai-2023\\E14-Maintenance d'applications\\Liste\\ListePatineurs.xlsx");
excelSheet = excelBook.Worksheets[1];
}
catch (Exception ex)
{
MessageBox.Show("Une erreur s'est produite.");
Application.Exit();
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
excelApp!.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
}
private void button1_Click(object sender, EventArgs e)
{
int lastRow = excelSheet!.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
int lastCol = excelSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Column;
MessageBox.Show("le nombre de ligne: " + lastRow + "nombre colonne: " + lastCol);
for (int i = 1; i < lastRow; i++)
{
for (int j = 0; j < lastCol; j++)
{
}
}
}
}
Ali BenchaabanRank #925 · 2.2K reputationPosted Mar 23, 2023, 5:28 AM EDT
private Excel.Application excelApp = null; public Form1() { // Initialize the Windows Components InitializeComponent(); excelApp = new Excel.Application(); // See if the Excel Application Object was successfully constructed if (excelApp == null) { MessageBox.Show("Désolé, cette application nécessite Excel. Veuillez l'installer et réessayer."); Application.Exit(); } // Make the Application Visible excelApp.Visible = true; } private void button1_Click(object sender, System.EventArgs e) { // prepare open file dialog to only search for excel files (had trouble setting this in design view) this.openFileDialog1.FileName = "*.xls"; if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Excel.Workbook theWorkbook = excelApp.Workbooks.Open(openFileDialog1.FileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true); // get the collection of sheets in the workbook Excel.Sheets sheets = theWorkbook.Worksheets; // get the first and only worksheet from the collection of worksheets Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1); // loop through 10 rows of the spreadsheet and place each row in the list view for (int i = 1; i <= 10; i++) { Excel.Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString()); System.Array myvalues = (System.Array)range.Cells.Value; string[] strArray = ConvertToStringArray(myvalues); listView1.Items.Add(new ListViewItem(strArray)); } } } string[] ConvertToStringArray(System.Array values) { // create a new string array string[] theArray = new string[values.Length]; // loop through the 2-D System.Array and populate the 1-D String Array for (int i = 1; i <= values.Length; i++) { if (values.GetValue(1, i) == null) theArray[i - 1] = ""; else theArray[i - 1] = (string)values.GetValue(1, i).ToString(); } return theArray; }