C# :: Aufgabe #263 :: Lösung #3
4 Lösungen

#263
Zelle aus Exceldatei auslesen
Anfänger - C#
von Gustl
- 12.06.2019 um 10:29 Uhr
Es solle eine Excel Datei eingelesen (XLSX) und dann ein bestimmter Inhalt einer Zelle wieder ausgegeben werden.
#3

von hollst (13980 Punkte)
- 28.08.2019 um 14:04 Uhr

using System; using System.Data; using System.Windows.Forms; using System.IO; using ExcelDataReader; namespace read_an_excel_file__xls_or_xlsx { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataSet result; private void btOpen_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog() {Filter = "Excel1|*.xls|Exel2|*.xlsx", ValidateNames = true }) { if (ofd.ShowDialog() == DialogResult.OK) { FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read); FileInfo fi = new FileInfo(ofd.FileName); this.tb_filename.Text = fi.Name; using (var reader = ExcelReaderFactory.CreateReader(fs)) { result = reader.AsDataSet(); comboBox1.Items.Clear(); foreach (DataTable dt in result.Tables) comboBox1.Items.Add(dt.TableName); reader.Close(); } } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { dataGridView1.DataSource = result.Tables[comboBox1.SelectedIndex]; } private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { this.tb_selected_cell.Text = dataGridView1.SelectedCells[0].RowIndex.ToString() + " " + dataGridView1.SelectedCells[0].ColumnIndex.ToString(); string s = dataGridView1[dataGridView1.SelectedCells[0].ColumnIndex, dataGridView1.SelectedCells[0].RowIndex].Value.ToString(); this.tb_content.Text = s; } } } /* Lösung basiert im Wesentlichen auf https://www.youtube.com/watch?v=JlzzdB3K-1M https://stackoverflow.com/questions/27634477/using-exceldatareader-to-read-excel-data-starting-from-a-particular-cell (If you are using ExcelDataReader 3+ you will find that there isn't any method for AsDataSet() for your reader object, You need to also install another package for ExcelDataReader.DataSet, then you can use the AsDataSet() method.) und https://www.youtube.com/watch?v=_h_4-HxrMMc wichtig ist, dass ExcelDataReader und ExcelDataReader.DataSet über NuGet eingebunden werden. */
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1