Open 846736002 opened 6 years ago
private void button1_Click_1(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以选择多个文件 dialog.Title = "请选择文件夹"; dialog.Filter = "所有文件(.)|."; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string file = dialog.FileName; tb_FileDir.Text = file; tb_outdir.Text = "\out"; } }
private void btn_onlyshow_Click(object sender, EventArgs e)
{
//先获取文件路径,没有就不处理
String path = tb_FileDir.Text;
if (path == null || "".Equals(path))
{
return;
}
//文件流处理
StreamReader sr = null;
try
{
sr = new StreamReader(path, Encoding.Default);
}
catch (Exception e1)
{
MessageBox.Show(e1.StackTrace);
return;
}
///每一行读
String content;
//第一行列数
int colNum = 0;
//excel行数
int rowcount = 1;
tb_showDate.Text = "";
try
{
while ((content = sr.ReadLine()) != null)
{
String[] strrow = content.Split(',');
if (rowcount == 1)
{
colNum = strrow.Length;
}
tb_showDate.Text = tb_showDate.Text + ":"+strrow.Length+":" + content.ToString()+"\r\n";
rowcount++;
}
}
catch (Exception e1)
{
MessageBox.Show(e1.StackTrace);
}
MessageBox.Show($"第一行列数{colNum}+excel行数{rowcount}");
if (sr != null)
{
sr.Close();
}
}
private void button3_Click(object sender, EventArgs e) { //先获取文件路径,没有就不处理 String path = tb_FileDir.Text; if (path == null || "".Equals(path)) { return; }
dgv_excel1.Visible = true;
//dt.Rows.Count
//dt.Columns.Count
int[] matchNum = new int[2];
string str1 = tb_rownum.Text;
String str2 = tb_colnum.Text;
int strint1 = 0;
int strint2 = 0;
try
{
strint1 = int.Parse(str1);
strint2 = int.Parse(str2);
matchNum[0] = strint1 - 1;
matchNum[1] = strint2 - 1;
}
catch (Exception e23)
{
MessageBox.Show(e23.StackTrace);
}
DataSet ds = ExcelToDS(path,"");
DataTable tbl = ds.Tables["table1"];
String[] strrow = new String[tbl.Columns.Count];
String[][] aaa = new String[2][];
for (int i = 0; i < tbl.Columns.Count; i++)
{
DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
dgv_excel1.Columns.Add(c);
}
foreach (DataRow row in tbl.Rows)
{
int index = dgv_excel1.Rows.Add();
//tb_showDate.Text = tb_showDate.Text + row[0] + "\r\n";
//count++;
for (int i =0;i<tbl.Columns.Count; i++)
{
dgv_excel1.Rows[index].Cells[i].Value = row[i];
tb_showDate.Text = tb_showDate.Text + row[i] + "\t";
}
tb_showDate.Text = tb_showDate.Text + "\r\n";
}
}
public DataSet ExcelToDS(string Path,string sheetname) { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";