Winform(C#)操作Excel(数据分组应用)

上传人:平*** 文档编号:9862415 上传时间:2017-10-04 格式:DOCX 页数:9 大小:22.71KB
返回 下载 相关 举报
Winform(C#)操作Excel(数据分组应用)_第1页
第1页 / 共9页
Winform(C#)操作Excel(数据分组应用)_第2页
第2页 / 共9页
Winform(C#)操作Excel(数据分组应用)_第3页
第3页 / 共9页
Winform(C#)操作Excel(数据分组应用)_第4页
第4页 / 共9页
Winform(C#)操作Excel(数据分组应用)_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《Winform(C#)操作Excel(数据分组应用)》由会员分享,可在线阅读,更多相关《Winform(C#)操作Excel(数据分组应用)(9页珍藏版)》请在金锄头文库上搜索。

1、 Winform(C#)操作 Excel(数据分组应用)using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.OleDb;using System.Threading;using Microsoft.Office.Interop;namespace HeXieRandomGrouppublic par

2、tial class RandomGroup : Formprivate DataSet myDataSet;private DataTable GroupDt1, GroupDt2, GroupDt3, GroupDt4, GroupDt5;private Thread thread;private object m_objOpt = System.Reflection.Missing.Value;public RandomGroup()RandomGroup.CheckForIllegalCrossThreadCalls = false;InitializeComponent();priv

3、ate void ConectToxsl_Click(object sender, EventArgs e)GetConnect();this.Start.Enabled = true;private void GetConnect() trystring strCon = Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + Application.StartupPath.ToString() + /hexie.xls; Extended Properties=Excel 8.0;HDR=No;IMEX=1;OleDbConnection myCon

4、n = new OleDbConnection(strCon);string strCom = SELECT * FROM Sheet1$;myConn.Open();OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);myDataSet = new DataSet();myCommand.Fill(myDataSet, Sheet1$);myConn.Close();MessageBox.Show(连接成功!);this.ConectToxsl.Enabled = false;dataGridView1.Data

5、Source = myDataSet.Tables0.DefaultView;catch (OleDbException e)MessageBox.Show(请确认 hexie.xls 表存在! + e.Message);catch (Exception e)MessageBox.Show(链接失败 + e.Message);private void Start_Click(object sender, EventArgs e)if (myDataSet.Tables0.Rows.Count 0)mythread();this.Start.Enabled = false; elseMessag

6、eBox.Show(对不起请确认数据表中有数据!);private void deldataset(string str)for (int i = 0; i myDataSet.Tables0.Rows.Count; i+)if (label1.Text = myDataSet.Tables0.Rowsi0.ToString()myDataSet.Tables0.Rows.RemoveAt(i);private void mythread()thread = new Thread(new ThreadStart(GetPick);thread.Start();private void GetP

7、ick()Random autoRand = new Random();int n, m, group;group = 1;m = myDataSet.Tables0.Rows.Count;GroupDt1 = myDataSet.Tables.Add(Group1);GroupDt1.Columns.Add(Name, typeof(String);GroupDt1.Columns.Add(Address, typeof(String); GroupDt2 = myDataSet.Tables.Add(Group2);GroupDt2.Columns.Add(Name, typeof(Str

8、ing);GroupDt2.Columns.Add(Address, typeof(String);GroupDt3 = myDataSet.Tables.Add(Group3);GroupDt3.Columns.Add(Name, typeof(String);GroupDt3.Columns.Add(Address, typeof(String);GroupDt4 = myDataSet.Tables.Add(Group4);GroupDt4.Columns.Add(Name, typeof(String);GroupDt4.Columns.Add(Address, typeof(Stri

9、ng);GroupDt5 = myDataSet.Tables.Add(Group5);GroupDt5.Columns.Add(Name, typeof(String);GroupDt5.Columns.Add(Address, typeof(String);for (int i = 0; i m; i+)n = (int)(m - i) * autoRand.NextDouble();label1.Text = myDataSet.Tables0.Rowsn0.ToString();DataRow row;switch (group)case 1:listBox1.Items.Add(my

10、DataSet.Tables0.Rowsn0.ToString();row = GroupDt1.NewRow();rowName = myDataSet.Tables0.Rowsn0.ToString();rowAddress = myDataSet.Tables0.Rowsn1.ToString(); GroupDt1.Rows.Add(row);group+;break;case 2:listBox2.Items.Add(myDataSet.Tables0.Rowsn0.ToString();row = GroupDt2.NewRow();rowName = myDataSet.Tabl

11、es0.Rowsn0.ToString();rowAddress = myDataSet.Tables0.Rowsn1.ToString();GroupDt2.Rows.Add(row);group+;break;case 3:listBox3.Items.Add(myDataSet.Tables0.Rowsn0.ToString();row = GroupDt3.NewRow();rowName = myDataSet.Tables0.Rowsn0.ToString();rowAddress = myDataSet.Tables0.Rowsn1.ToString();GroupDt3.Row

12、s.Add(row);group+;break;case 4:listBox4.Items.Add(myDataSet.Tables0.Rowsn0.ToString();row = GroupDt4.NewRow();rowName = myDataSet.Tables0.Rowsn0.ToString();rowAddress = myDataSet.Tables0.Rowsn1.ToString();GroupDt4.Rows.Add(row);group+;break;default:listBox5.Items.Add(myDataSet.Tables0.Rowsn0.ToStrin

13、g();row = GroupDt5.NewRow();rowName = myDataSet.Tables0.Rowsn0.ToString(); rowAddress = myDataSet.Tables0.Rowsn1.ToString();GroupDt5.Rows.Add(row);group = 1;break;deldataset(label1.Text);Thread.Sleep(10);BtnExt.Enabled = true;label1.Text = 成功分组 ;MessageBox.Show(成功分组);private void BtnExt_Click(object

14、 sender, EventArgs e)ExportToExcel(myDataSet, hexieGroup);public void ExportToExcel(DataSet ds, string strExcelFileName)if (ds.Tables.Count = 0 | strExcelFileName = ) return;doExport(ds, strExcelFileName);private void doExport(DataSet ds, string strExcelFileName)Microsoft.Office.Interop.Excel.Applic

15、ation excel = new Microsoft.Office.Interop.Excel.Application();excel.Application.Workbooks.Add(true); excel.Visible = false;for (int i = 1; i ds.Tables.Count; i+)int rowIndex = 1;int colIndex = 0;Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveWorkbook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);/Microsoft.Office.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号