如何解决在C#中如何选择和显示现有Excel文件中的随机行
我是C#和excel编码的新手。我想选择一个excel文件,然后在用户按下随机按钮后显示一个或多个随机行(需要用户输入用户想要的行数)后显示它,并且可以像另一种形式一样弹出屏幕或更改数据网格认为没关系。用户可以从计算机中选择excel文件,我可以在数据网格视图中显示该文件,但卡在随机行部分,无法在Internet中找到任何有用的东西。这可能是代码(我尝试了太多的事情,因此代码中可能包含一些无效的代码行):
using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.sqlClient;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
namespace Excel_D
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTableCollection tableCollection;
public void button1_Click(object sender,EventArgs e)
{
using(OpenFileDialog openFileDialog=new OpenFileDialog() { Filter="Excel Workbook|*xlsx|Excel 97-2003 Workbook|*xls"})
{
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog.FileName;
using(var stream = File.Open(openFileDialog.FileName,FileMode.Open,FileAccess.Read))
{
using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
{
DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
});
tableCollection = result.Tables;
cBoxSheet.Items.Clear();
foreach (System.Data.DataTable table in tableCollection)
cBoxSheet.Items.Add(table.TableName);
}
}
}
}
}
Random rnd = new Random();
public void label1_Click(object sender,EventArgs e)
{
}
public void comboBox1_SelectedindexChanged(object sender,EventArgs e)
{
System.Data.DataTable dt = tableCollection[cBoxSheet.SelectedItem.ToString()];
GridView.DataSource = dt;
}
public void label2_Click(object sender,EventArgs e)
{
}
public void textBox1_TextChanged(object sender,EventArgs e)
{
}
public void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)
{
}
public void dataGridView1_SelectionChanged(object sender,EventArgs e)
{
}
public void GridView_CellContentClick(object sender,DataGridViewCellEventArgs e)
{
}
public void button1_Click_1(object sender,EventArgs e)
{
}
}
}
我不知道这是否重要,但这是代码的设计者部分:
namespace Excel_D
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void dispose(bool disposing)
{
if (disposing && (components != null))
{
components.dispose();
}
base.dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.Btnbrowse = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.cBoxSheet = new System.Windows.Forms.ComboBox();
this.txtFileName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.GridView = new System.Windows.Forms.DataGridView();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportinitialize)(this.GridView)).BeginInit();
this.SuspendLayout();
//
// Btnbrowse
//
this.Btnbrowse.Location = new System.Drawing.Point(682,279);
this.Btnbrowse.Name = "Btnbrowse";
this.Btnbrowse.Size = new System.Drawing.Size(75,20);
this.Btnbrowse.TabIndex = 0;
this.Btnbrowse.Text = "=>";
this.Btnbrowse.UseVisualStyleBackColor = true;
this.Btnbrowse.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12,279);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(58,13);
this.label1.TabIndex = 2;
this.label1.Text = "Dosya Adı:";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// cBoxSheet
//
this.cBoxSheet.FormattingEnabled = true;
this.cBoxSheet.Location = new System.Drawing.Point(86,306);
this.cBoxSheet.Name = "cBoxSheet";
this.cBoxSheet.Size = new System.Drawing.Size(121,21);
this.cBoxSheet.TabIndex = 3;
this.cBoxSheet.SelectedindexChanged += new System.EventHandler(this.comboBox1_SelectedindexChanged);
//
// txtFileName
//
this.txtFileName.Location = new System.Drawing.Point(86,279);
this.txtFileName.Name = "txtFileName";
this.txtFileName.ReadOnly = true;
this.txtFileName.Size = new System.Drawing.Size(590,20);
this.txtFileName.TabIndex = 4;
this.txtFileName.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12,306);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(37,13);
this.label2.TabIndex = 5;
this.label2.Text = "Sayfa:";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(12,333);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(723,27);
this.button1.TabIndex = 6;
this.button1.Text = "Rastgele örneklem seçmek için tıklayın.";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// GridView
//
this.GridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.GridView.Location = new System.Drawing.Point(12,12);
this.GridView.Name = "GridView";
this.GridView.Size = new System.Drawing.Size(745,254);
this.GridView.TabIndex = 1;
this.GridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.GridView_CellContentClick);
//
// button2
//
this.button2.Location = new System.Drawing.Point(12,366);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(723,23);
this.button2.TabIndex = 7;
this.button2.Text = "Rastgele örneklem görmek için tıklayın.";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.Sizef(6F,13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.ClientSize = new System.Drawing.Size(766,451);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtFileName);
this.Controls.Add(this.cBoxSheet);
this.Controls.Add(this.label1);
this.Controls.Add(this.GridView);
this.Controls.Add(this.Btnbrowse);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ExcelÖrneklem";
((System.ComponentModel.ISupportinitialize)(this.GridView)).EndInit();
this.ResumeLayout(false);
this.Performlayout();
}
#endregion
private System.Windows.Forms.Button Btnbrowse;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox cBoxSheet;
private System.Windows.Forms.TextBox txtFileName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridView GridView;
private System.Windows.Forms.Button button2;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。