| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using CoreFS.CA06;
- using Core.Mes.Client.Comm.Tool;
- using System.Data.OleDb;
- using System.IO;
- using Infragistics.Win.UltraWinGrid;
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using System.Threading;
- using System.Collections;
- using Core.Mes.Client.Comm.Format;
- using Core.Mes.Client.Comm.Server;
- namespace Core.StlMes.Client.SaleOrder.ReviewForm
- {
- public partial class FrmExcelToGrid : FrmBase
- {
- public FrmExcelToGrid()
- {
- InitializeComponent();
- }
- public FrmExcelToGrid(OpeBase ob)
- {
- InitializeComponent();
- this.ob = ob;
- }
- HSSFWorkbook hssfworkbook; //Office 2003
- XSSFWorkbook xssfworkbook; //Office 2007 2010
- ISheet sheet = null;
- private string saleOrg;
- /// <summary>
- /// 销售组织
- /// </summary>
- public string SaleOrg
- {
- get { return saleOrg; }
- set { saleOrg = value; }
- }
- /// <summary>
- /// 打开选择的Excel
- /// </summary>
- /// <param name="path"></param>
- private void InitializeWorkbook(string path)
- {
- try
- {
- using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
- {
- hssfworkbook = new HSSFWorkbook(file);
- sheet = hssfworkbook.GetSheetAt(0);
- }
- }
- catch
- {
- using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
- {
- xssfworkbook = new XSSFWorkbook(file);
- sheet = xssfworkbook.GetSheetAt(0);
- }
- }
- }
- delegate void SetGridCallBack(DataTable dt);
- private void SetGrid(DataTable dt)
- {
- if (ultraGrid1.InvokeRequired)
- {
- SetGridCallBack sg = new SetGridCallBack(SetGrid);
- this.Invoke(sg, new object[] { dt });
- }
- else
- {
- ultraGrid1.DataSource = dt;
- ultraGrid1.DisplayLayout.Bands[0].Columns["选择"].Header.CheckBoxVisibility = Infragistics.Win.UltraWinGrid.HeaderCheckBoxVisibility.WhenUsingCheckEditor;
- ultraGrid1.DisplayLayout.Bands[0].Columns[1].Width = 135;
- this.Cursor = Cursors.Default;
- }
- }
- /// <summary>
- /// 将Excel内容转化为DataTable
- /// </summary>
- private void ConvertToDataTable()
- {
- //ISheet sheet = hssfworkbook.GetSheetAt(0);
- System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
- IRow iRow = sheet.GetRow(0);
- short count = iRow.LastCellNum;
- if (saleOrg == "100102")
- {
- if (count != 5)
- {
- MessageUtil.ShowWarning("导入的Excel列数与模板列数不符!");
- return;
- }
- }
- else
- {
- if (count != 6)
- {
- MessageUtil.ShowWarning("导入的Excel列数与模板列数不符!");
- return;
- }
- }
- DataTable dt = new DataTable();
- for (int j = 0; j < count + 1; j++)
- {
- if (j == 0) //加一列选择框
- {
- dt.Columns.Add("选择");
- }
- else
- {
- dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());
- }
- }
- dt.Columns[0].DefaultValue = "False";
- dt.Columns[0].DataType = typeof(Boolean);
- while (rows.MoveNext())
- {
- IRow row = null;
- try
- {
- row = (HSSFRow)rows.Current;
- }
- catch
- {
- row = (XSSFRow)rows.Current;
- }
- DataRow dr = dt.NewRow();
- for (int i = 1; i < row.LastCellNum + 1; i++)
- {
- ICell cell = row.GetCell(i - 1);
- if (cell == null)
- {
- dr[i] = null;
- }
- else
- {
- dr[i] = cell.ToString();
- }
- }
- dt.Rows.Add(dr);
- }
- SetGrid(dt);
- }
- /// <summary>
- /// 使用系统软件打开模板
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- string path = "";
- if (saleOrg == "100102")
- {
- path = System.Windows.Forms.Application.StartupPath + "\\PriceTemplate\\PriceTemplateGM.xls";
- }
- else
- {
- path = System.Windows.Forms.Application.StartupPath + "\\PriceTemplate\\PriceTemplateXS.xls";
- }
- try
- {
- System.Diagnostics.Process.Start(path);
- }
- catch (Exception ex)
- {
- MessageUtil.ShowWarning("没有可用的模板!");
- }
- }
- private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
- {
- switch (e.Tool.Key)
- {
- case "Import":
- ImportExcel();
- break;
- case "Confirm":
- ConfirmData();
- break;
- case "Close":
- this.Close();
- break;
- default:
- break;
- }
- }
- private void ImportExcel()
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "所有文件|*.xls;*.xlsx|xls文件(*.xls)|*.xls|xlsx文件(*.xlsx)|*.xlsx";
- ofd.Multiselect = false;
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- this.Cursor = Cursors.WaitCursor;
- string filePath = ofd.FileName;
- InitializeWorkbook(filePath);
- //ConvertToDataTable();
- Thread t = new Thread(new ThreadStart(ConvertToDataTable));
- t.Start();
- }
- }
- Dictionary<string, string> dic = new Dictionary<string, string>();
- Dictionary<string, ArrayList> price = new Dictionary<string, ArrayList>();
- /// <summary>
- /// 确认数据
- /// </summary>
- private void ConfirmData()
- {
- ultraGrid1.UpdateData();
- int flag = 0;
- foreach (UltraGridRow row in ultraGrid1.Rows)
- {
- if (row.Cells["选择"].Value.ToString().ToUpper() == "TRUE")
- {
- flag += 1;
- string orderNoSeq = row.Cells[1].Value.ToString();
- string balPrice = row.Cells[2].Value.ToString();
- string outPrice = row.Cells[3].Value.ToString();
- string traPrice = row.Cells[4].Value.ToString();
- string pakPrice = row.Cells[5].Value.ToString();
- string priceBase = row.Cells[6].Value.ToString();
- if (orderNoSeq == "")
- {
- MessageUtil.ShowWarning("合同号(合同号/合同行号)不能为空!");
- return;
- }
- string orderNo = "";
- string seq = "";
- try
- {
- orderNo = orderNoSeq.Substring(0, orderNoSeq.LastIndexOf("/"));
- seq = orderNoSeq.Substring(orderNoSeq.LastIndexOf("/") + 1, orderNoSeq.Length - 1 - orderNoSeq.LastIndexOf("/"));
- }
- catch (Exception)
- {
- MessageUtil.ShowWarning("合同号(合同号/合同行号)跟模板数据规则不符!");
- return;
- }
- if (balPrice == "")
- {
- MessageUtil.ShowWarning("结算价不能为空!");
- return;
- }
- else
- {
- if (!StringUtil.IsNumber(balPrice))
- {
- MessageUtil.ShowWarning("结算价不是数字!");
- return;
- }
- }
- if (outPrice == "")
- {
- if (saleOrg != "100102")
- {
- MessageUtil.ShowWarning("出厂价不能为空!");
- return;
- }
- }
- else
- {
- if (!StringUtil.IsNumber(outPrice))
- {
- MessageUtil.ShowWarning("结算价不是数字!");
- return;
- }
- }
- if (traPrice != "")
- {
- if (!StringUtil.IsNumber(traPrice))
- {
- MessageUtil.ShowWarning("运费不是数字!");
- return;
- }
- }
- if (pakPrice != "")
- {
- if (!StringUtil.IsNumber(pakPrice))
- {
- MessageUtil.ShowWarning("吊装费不是数字!");
- return;
- }
- }
- if (priceBase == "")
- {
- MessageUtil.ShowWarning("定价说明不能为空!");
- return;
- }
- if (!dic.ContainsKey(orderNo))
- {
- dic.Add(orderNo, priceBase);
- }
- ArrayList list = new ArrayList();
- list.Add(orderNo);
- list.Add(seq);
- list.Add(balPrice);
- list.Add(outPrice);
- list.Add(traPrice);
- list.Add(pakPrice);
- if (price.ContainsKey(orderNo))
- {
- ArrayList temp = price[orderNo];
- temp.Add(list);
- price[orderNo] = temp;
- }
- else
- {
- ArrayList parm = new ArrayList();
- parm.Add(list);
- price[orderNo] = parm;
- }
- }
- }
- if (flag == 0)
- {
- MessageUtil.ShowWarning("请勾选或者拖选您要确认的数据!");
- return;
- }
- if (MessageUtil.ShowYesNoAndQuestion("是否确认勾选的数据?") == DialogResult.No) return;
- SlmOrderPriceEntity sop = new SlmOrderPriceEntity();
- //提报信息
- sop.ReportName = UserInfo.GetUserName();
- sop.ReportUnitcode = UserInfo.GetDeptid();
- sop.ReportUnitdesc = UserInfo.GetDepartment();
- sop.ReportDeptcode = ClsBaseInfo.GetDepartIdBySectionId(UserInfo.GetDeptid(), this.ob);
- sop.ReportDeptdesc = ClsBaseInfo.GetDepartBySectionId(UserInfo.GetDeptid(), this.ob);
- //评审通过信息
- sop.DealName = UserInfo.GetUserName();
- sop.DealUnitcode = UserInfo.GetDeptid();
- sop.DealUnitdesc = UserInfo.GetDepartment();
- sop.DealDeptcode = ClsBaseInfo.GetDepartIdBySectionId(UserInfo.GetDeptid(), this.ob);
- sop.DealDeptdesc = ClsBaseInfo.GetDepartBySectionId(UserInfo.GetDeptid(), this.ob);
- CoreClientParam ccp = new CoreClientParam();
- ccp.IfShowErrMsg = false;
- ccp.ServerName = "com.steering.pss.sale.order.CoreExcelToGrid";
- ccp.MethodName = "confirmDataToDB";
- ccp.ServerParams = new object[] { dic, JSONFormat.Format(sop), price };
- ccp = this.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
- if (ccp != null)
- {
- if (ccp.ReturnCode == -1)
- {
- MessageUtil.ShowWarning(ccp.ReturnInfo);
- return;
- }
- MessageUtil.ShowTips("数据确认成功!");
- }
- }
- private void ultraGrid1_AfterSelectChange(object sender, Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs e)
- {
- foreach (UltraGridRow uRow in ultraGrid1.Selected.Rows)
- {
- if (uRow.GetType() != typeof(Infragistics.Win.UltraWinGrid.UltraGridGroupByRow))
- {
- uRow.Cells["选择"].Value = true;
- }
- }
- }
- }
- }
|