846736002 / c-

0 stars 0 forks source link

1 #1

Open 846736002 opened 6 years ago

846736002 commented 6 years ago

public DataSet ExcelToDS(string Path,string sheetname) { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";

       // strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
       // "Data Source=d:/test.xlsx;" + "Extended Properties=Excel 8.0;";

        //此连接可以操作.xls与.xlsx文件
        strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" + Path + ";Extended Properties='Excel 12.0; HDR=NO;IMEX=1'";

        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        string strExcel = "";
        OleDbDataAdapter myCommand = null;
        DataSet ds = null;

        //得到所有sheet的名字
        DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
        //得到第一个sheet的名字
        string firstSheetName = sheetsName.Rows[0][2].ToString();
        //查询字符串
        strExcel = string.Format("SELECT * FROM [{0}]", firstSheetName);
        //strExcel = "select * from [firstSheetName$]";

        myCommand = new OleDbDataAdapter(strExcel, strConn);
        ds = new DataSet();
        myCommand.Fill(ds, "table1");
        return ds;
    }
846736002 commented 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"; } }

846736002 commented 6 years ago

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();
        }
    }
846736002 commented 6 years ago

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";
        }
    }