using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel;
using System.Reflection;
namespace ExcelOpt
{
public partial class ExcelSheetsSelect : Form
{
public ExcelSheetsSelect()
{
InitializeComponent();
}
#region 属性
//任务列表
List<TaskInfo> _TaskList = new List<TaskInfo>();
internal List<TaskInfo> TaskList
{
get { return _TaskList; }
set { _TaskList = value; }
}
//任务-人员(班组)对应列表
List<GroupCompenstation> _GroupList = new List<GroupCompenstation>();
internal List<GroupCompenstation> GroupList
{
get { return _GroupList; }
set { _GroupList = value; }
}
//文件路径
string _GetFilePath = "";
public string GetFilePath
{
get { return _GetFilePath; }
set { _GetFilePath = value; }
}
//获取任务人员列表
List<CompenstationAdjustModel> _AdjustList = new List<CompenstationAdjustModel>();
internal List<CompenstationAdjustModel> AdjustList
{
get { return _AdjustList; }
set { _AdjustList = value; }
}
#endregion
#region 操作函数Kit
public void LoadDataDource()
{
gridControlTask.DataSource = null;
gridControlTask.DataSource = TaskList;
}
#endregion
#region 系统事件
/// <summary>
/// 导入EXCEL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLoadExcel_Click(object sender, EventArgs e)
{
btnProgress.Visible = true;
TaskList.Clear();
if (OpenExcel.ShowDialog() != DialogResult.OK) return;
GetFilePath = OpenExcel.FileName;
if (GetFilePath == "")
{
MessageBox.Show("请导入【职工定额工资分配结算表】!","-友情提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
Excel.Application app = new Excel.Application();
app.Visible = false;
app.UserControl = true;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(@GetFilePath);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
Range range = worksheet.get_Range("A2", Missing.Value);
//任务列表
TaskInfo GetTask = null;
//任务-人员(班组)对应列表
GroupCompenstation GetGroup = null;
string[] info = new string[8];
btnProgress.Maximum = worksheet.UsedRange.Rows.Count;
for (int i = 0; i < worksheet.UsedRange.Rows.Count; i++)
{
GetTask = new TaskInfo();
GetGroup = new GroupCompenstation();
for (int j = 0; j < 8; j++)
{
try
{
info[j] = ((Excel.Range)range.Cells[i, j]).Text.ToString();
}
catch
{
continue;
}
if (info[j] == null) continue;
if (info[j].Length < 5) continue;
if (info[j].Trim().Substring(0, 2) == "班组")
{
//记录任务信息
GetTask.Id = i;
GetTask.IsSelect = false;
GetTask.TaskName = info[j].Trim().Replace("班组", "班组 【").Replace("职工", "】 职工");
TaskList.Add(GetTask);
//记录对应信息
GetGroup.Celly = i;
GetGroup.Cellx = j;
GetGroup.Id = i;
GroupList.Add(GetGroup);
break;
}
}
btnProgress.Value = i;
}
app.Workbooks.Close();
app.Quit();
LoadDataDource();
btnProgress.Visible = false;
btnProgress.Value = 0;
}
/// <summary>
/// 确定导入的任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOK_Click(object sender, EventArgs e)
{
if (TaskList.Count == 0) return;
if (TaskList.FindAll(delegate(TaskInfo model) { return model.IsSelect == true; }).Count == 0)
{
MessageBox.Show("请选择任务!","-友情提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
btnProgress.Visible = true;
if (gridView1.GetFocusedRowCellValue("Id") == null) return;
int GetID = Convert.ToInt32(gridView1.GetFocusedRowCellValue("Id"));
int x, y;
x = GroupList.Find(delegate(GroupCompenstation model) { return model.Id == GetID; }).Cellx;
y = GroupList.Find(delegate(GroupCompenstation model) { return model.Id == GetID; }).Celly;
AdjustList.Clear();
//
Excel.Application app = new Excel.Application();
app.Visible = false;
app.UserControl = true;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(@GetFilePath);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
Range range = worksheet.get_Range("A"+(y+4).ToString(), Missing.Value);
//任务人员列表
CompenstationAdjustModel GetAdjust = null;
string[] info = new string[8];
btnProgress.Maximum = worksheet.UsedRange.Rows.Count;
bool IsOut = false;
int IsErr = 0;
for (int i = 0; i < worksheet.UsedRange.Rows.Count; i++)
{
GetAdjust = new CompenstationAdjustModel();
for (int j = 0; j < 8; j++)
{
try
{
info[j] = ((Excel.Range)range.Cells[i, j]).Text.ToString();
if (info[j].Trim().Substring(0, 2) == "小计")
{
IsErr++;
IsOut = true;
break;
}
}
catch
{
continue;
}
}
if (IsOut) break;
GetAdjust.Id = i;
GetAdjust.UserNumber =
info[1];
GetAdjust.UName = info[2];
GetAdjust.WorkDays = info[3];
GetAdjust.Quality = info[4];
GetAdjust.PayoffSum = info[5];
AdjustList.Add(GetAdjust);
btnProgress.Value = i;
}
app.Workbooks.Close();
app.Quit();
LoadDataDource();
btnProgress.Visible = false;
btnProgress.Value = 0;
if (AdjustList.Count == 1)
{
MessageBox.Show("未检索到数据!","-友情提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if (IsErr == 0)
{
MessageBox.Show("Excel格式不正确!", "-友情提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
Form3 frmnew = new Form3();
frmnew.AdjustList.AddRange(AdjustList);
frmnew.ShowDialog();
}
#endregion
/// <summary>
/// 选择任务时以 独占的方式选择
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_Click(object sender, EventArgs e)
{
if (TaskList.Count == 0) return;
if (gridView1.GetFocusedRowCellValue("Id") == null) return;
int GetID = Convert.ToInt32(gridView1.GetFocusedRowCellValue("Id"));
int ID = Convert.ToInt32(gridView1.GetFocusedRowCellValue("CheckIsSelect"));
foreach (TaskInfo model in TaskList)
{
if (model.Id != GetID)
{
model.IsSelect = false;
continue;
}
model.IsSelect = ID==0?true:false;
}
gridControlTask.RefreshDataSource();
}
}
#region
class CompenstationAdjustModel
{
string _UName;
public string UName
{
get { return _UName; }
set { _UName = value; }
}
int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
string _UserNumber;
public string UserNumber
{
get { return _UserNumber; }
set { _UserNumber = value; }
}
string _Quality;
public string Quality
{
get { return _Quality; }
set { _Quality = value; }
}
string _WorkDays;
public string WorkDays
{
get { return _WorkDays; }
set { _WorkDays = value; }
}
string _PayoffSum;
public string PayoffSum
{
get { return _PayoffSum; }
set { _PayoffSum = value; }
}
string _OutPayoff;
public string OutPayoff
{
get { return _OutPayoff; }
set { _OutPayoff = value; }
}
string _States;
public string States
{
get { return _States; }
set { _States = value; }
}
}
#endregion
#region TaskInfo
class TaskInfo
{
int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
string _TaskName;
public string TaskName
{
get { return _TaskName; }
set { _TaskName = value; }
}
bool _IsSelect;
public bool IsSelect
{
get { return _IsSelect; }
set { _IsSelect = value; }
}
}
#endregion
#region 任务对应班组人员工作信息
class GroupCompenstation
{
/// <summary>
/// 对应任务ID
/// </summary>
int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
/// <summary>
/// 单元格水平坐标
/// </summary>
int _Cellx;
public int Cellx
{
get { return _Cellx; }
set { _Cellx = value; }
}
/// <summary>
/// 单元格垂直坐标
/// </summary>
int _Celly;
public int Celly
{
get { return _Celly; }
set { _Celly = value; }
}
}
#endregion
}