Files
Groupware/SubProject/FPJ0000/EBoard/fEBoardImport.cs
2024-05-08 14:53:55 +09:00

376 lines
15 KiB
C#

using FCOMMON;
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;
namespace FPJ0000
{
public partial class fEBoardImport : fBase
{
string setFile;
dsPRJ.EETGW_JobReport_EBoardDataTable dt = new dsPRJ.EETGW_JobReport_EBoardDataTable();
DataTable dtExcel = new DataTable();
public fEBoardImport()
{
InitializeComponent();
setFile = System.IO.Path.Combine(FCOMMON.Util.CurrentPath, "PreSet", Name + ".xml");
this.FormClosed += __Closed;
}
private void __Load(object sender, EventArgs e)
{
EnsureVisibleAndUsableSize();
this.dtPdate.Value = DateTime.Now;
}
void __Closed(object sender, FormClosedEventArgs e)
{
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog od = new OpenFileDialog();
od.Filter = "excel|*.xlsx|all files|*.*";
var fi = new System.IO.FileInfo(setFile);
if (fi.Directory.Exists == false) fi.Directory.Create();
var xml = new arUtil.XMLHelper(setFile);
if (xml.Exist() == false) xml.CreateFile();
od.FileName = xml.get_Data("path", "xls"); // FCOMMON.Util.CurrentPath + "model";
//od.FilterIndex = 1;
//od.RestoreDirectory = true;
if (od.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
//선택하면 저장해준다.
xml.set_Data("path", "xls", od.FileName);
textBox1.Text = od.FileName;
}
private void button1_Click(object sender, EventArgs e)
{
int ColumnNameNo = (int)numericUpDown2.Value;
if (textBox1.Text.isEmpty())
{
FCOMMON.Util.MsgE("파일을 선택하세요");
textBox1.Focus();
return;
}
if (!System.IO.File.Exists(textBox1.Text))
{
FCOMMON.Util.MsgE("입력하신 파일이 존재하지 않습니다.");
textBox1.Focus();
textBox1.SelectAll();
return;
}
dtExcel.Columns.Clear();
dtExcel.Rows.Clear();
dtExcel.AcceptChanges();
libxl.Book book;// = new libxl.BinBook();
book = new libxl.XmlBook();
book.setKey(FCOMMON.info.libxlCompany, FCOMMON.info.libxlKey);
try
{
book.load(textBox1.Text);
}
catch (Exception ex)
{
FCOMMON.Util.MsgE(ex.Message);
return;
}
int sheetNum = (int)numericUpDown1.Value;
if (sheetNum >= book.sheetCount())
{
FCOMMON.Util.MsgE("입력한 시트 번호는 존재하지 않습니다.");
book = null;
return;
}
var sheet = book.getSheet(sheetNum);
var MaxRow = sheet.lastRow();
var MinRow = sheet.firstRow();
MaxRow = (int)Math.Min(MaxRow, nudE.Value - 1);
MinRow = (int)Math.Max(MinRow, nudS.Value - 1);
if (MinRow <= (ColumnNameNo - 1))
{
FCOMMON.Util.MsgI("시작줄을 제목줄 보다 커야 합니다. 자동으로 +1 증가합니다.");
MinRow = ColumnNameNo;
}
var MaxCol = sheet.lastCol();
var MinCol = sheet.firstCol();
MaxCol = (int)Math.Min(MaxCol, nudCE.Value - 1);
MinCol = (int)Math.Max(MinCol, nudCS.Value - 1);
//제목줄을 처리한다. 181029
List<string> cols = new List<string>();
//string sDate = sd.Value.ToShortDateString();
//string eDate = ed.Value.ToShortDateString();
for (int c = MinCol; c <= MaxCol; c++)
{
var strVallue = sheet.readStr(ColumnNameNo - 1, c);
if (strVallue.isEmpty())
{
if (c == MinCol)
{
//첫줄 첫행이 빈값이면 처리하지 않는다.
FCOMMON.Util.MsgE("열 제목에 빈값이 있어 처리되지 못합니다.");
return;
}
else
{
//빈값이 나왓으므로 열 최대값을 변경해준다.
if (c < MaxCol)
{
FCOMMON.Util.MsgI("빈값으로 인해 최대 열 번호를 " + c.ToString() + "로 변경합니다");
MaxCol = c;
break;
}
}
}
cols.Add(strVallue);
this.dtExcel.Columns.Add(strVallue);
}
try
{
for (int r = MinRow; r <= MaxRow; r++)
{
DataRow dr = dtExcel.NewRow();
Boolean nullColumn = false;
for (int c = MinCol; c <= MaxCol; c++)
{
var colNo = c - MinCol;
if (sheet.isDate(r, c))
{
var datevalue = sheet.readNum(r, c);
int y, m, d;
y = m = d = 0;
book.dateUnpack(datevalue, ref y, ref m, ref d);
if (y >= 2000)
dr[cols[colNo]] = string.Format("{0:0000}-{1:00}-{2:00}", y, m, d);
else
dr[cols[colNo]] = string.Empty;
//일자가 만약 넘어서면 패스한다.
if (cols[colNo] == "일자")
{
string value = dr[cols[colNo]].ToString();
if (value == "")
{
nullColumn = true; //날짜가 없다면 패스
break;
}
//else if(value.CompareTo(sDate) < 0)
//{
// nullColumn = true; //과거데이터라서 패스
// break;
//} else if(value.CompareTo(eDate) > 0)
//{
// nullColumn = true; //미래데이터 패스
// break;
//}
}
}
else
{
var dateStrin = sheet.readStr(r, c);
var strVallue = sheet.readStr(r, c);
if (strVallue.isEmpty() && c == MinCol)
{
//첫줄 첫행이 빈값이면 처리하지 않는다.
nullColumn = true;
break;
}
if (!cols[colNo].isEmpty())
dr[cols[colNo]] = strVallue;
}
}
if (nullColumn) continue; //줄처리를 못한 경우 넘어감
if (dr != null)
{
dtExcel.Rows.Add(dr);
}
}
dtExcel.AcceptChanges();
}
catch (Exception ex)
{
FCOMMON.Util.MsgE("불러오는 중 오류 발생\n" + ex.Message);
}
//
book = null;
this.bs.DataSource = dtExcel;
this.dataGridView1.DataSource = dtExcel;
this.bn.BindingSource = this.bs;
if (this.bs.Count < 1)
{
FCOMMON.Util.MsgE("입력된 자료가 없습니다.\n\n지정된 엑셀의 1번째 칸에 값이 없다면 입력되지 않습니다.");
}
}
private void button3_Click(object sender, EventArgs e)
{
if (dtExcel == null || dtExcel.Rows.Count < 1)
{
FCOMMON.Util.MsgE("등록 가능한 자료가 없습니다.");
return;
}
if (col_sdate.Value < 1)
{
FCOMMON.Util.MsgE("CR번호/품명/SId는 반드시 입력되어야 합니다.");
return;
}
System.Text.StringBuilder sb = new StringBuilder();
sb.AppendLine("다음 자료를 추가하시겠습니까?");
sb.AppendLine();
sb.AppendLine("'저장 완료' 메세지가 나올때 까지 기다려 주세요.");
sb.AppendLine();
sb.AppendLine("실행 하려면 '예' 를 누르세요");
var dlg = FCOMMON.Util.MsgQ(sb.ToString());
if (dlg != System.Windows.Forms.DialogResult.Yes) return;
dt.Clear();
dt.AcceptChanges();
this.progressBar1.Value = 0;
this.progressBar1.Maximum = dtExcel.Rows.Count;
//12,13
foreach (DataRow dr in dtExcel.Rows)
{
this.progressBar1.Value += 1;
//데이터추가
var newdr = dt.NewEETGW_JobReport_EBoardRow();
if (dr[(int)col_sdate.Value] == DBNull.Value) continue; //nullerror
newdr["wdate"] = dtPdate.Value;//.ToString("yyyy-MM-dd");// "2019-01-01";// dr[0].ToString();
newdr["wuid"] = FCOMMON.info.Login.no;
newdr["gcode"] = FCOMMON.info.Login.gcode;
newdr["import"] = true;
newdr["memo"] = string.Empty;
if (col_site.Value >= 0) newdr["Site"] = dr[(int)col_site.Value].ToString().Trim();
if (col_request.Value >= 0) newdr["요청자"] = dr[(int)col_request.Value].ToString().Trim();
if (col_sdate.Value >= 0) newdr["pdate"] = dr[(int)col_sdate.Value].ToString().Trim();
if (col_edate.Value >= 0) newdr["수리완료일"] = dr[(int)col_edate.Value].ToString().Trim();
if (col_status.Value >= 0) newdr["Status"] = dr[(int)col_status.Value].ToString().Trim();
if (col_div.Value >= 0) newdr["분류"] = dr[(int)col_div.Value].ToString().Trim();
if (col_line.Value >= 0) newdr["Line"] = dr[(int)col_line.Value].ToString().Trim();
if (col_division.Value >= 0) newdr["Division"] = dr[(int)col_division.Value].ToString().Trim();
if (col_team.Value >= 0) newdr["Team"] = dr[(int)col_team.Value].ToString().Trim();
if (col_process.Value >= 0) newdr["Process"] = dr[(int)col_process.Value].ToString().Trim();
if (col_model.Value >= 0) newdr["Model"] = dr[(int)col_model.Value].ToString().Trim();
if (col_boardname.Value >= 0) newdr["BoardName"] = dr[(int)col_boardname.Value].ToString().Trim();
if (col_boardvender.Value >= 0) newdr["BoardVender"] = dr[(int)col_boardvender.Value].ToString().Trim();
if (col_reason.Value >= 0) newdr["원인"] = dr[(int)col_reason.Value].ToString().Trim();
if (col_result.Value >= 0) newdr["결과"] = dr[(int)col_result.Value].ToString().Trim();
if (col_sn.Value >= 0) newdr["SN"] = dr[(int)col_sn.Value].ToString().Trim();
var s_qty = dr[(int)col_qty.Value].ToString().Trim();
var i_qty = 0;
if (s_qty.isEmpty()) s_qty = "0";
int.TryParse(s_qty, out i_qty);
if (col_qty.Value >= 0) newdr["QTY"] = i_qty;// int.Parse(s_qty);
var s_nprice = dr[(int)col_pricen.Value].ToString().Trim();
var f_nprice = 0f;
if (s_nprice.isEmpty()) s_nprice = "0";
float.TryParse(s_nprice, out f_nprice);
if (col_pricen.Value >= 0) newdr["NPrice"] = f_nprice;// float.Parse(s_nprice);
var s_oprice = dr[(int)col_pricen.Value].ToString().Trim();
var f_oprice = 0f;
if (s_oprice.isEmpty()) s_oprice = "0";
float.TryParse(s_oprice, out f_oprice);
if (col_priceo.Value >= 0) newdr["OPrice"] = f_oprice;// float.Parse(s_oprice);
var s_rcost = dr[(int)col_pricerep.Value].ToString().Trim();
if (s_rcost.isEmpty()) s_rcost = "0";
float f_rcost = 0f;
float.TryParse(s_rcost, out f_rcost);
if (col_pricerep.Value >= 0) newdr["RepairCost"] = f_rcost;// float.Parse(s_rcost);
newdr["CostReduction"] = newdr.OPrice - newdr.RepairCost;// (float)(newdr["OPrice"]) - (float)(newdr["RepairCost"]);
if (col_extsou.Value >= 0) newdr["외주업체"] = dr[(int)col_extsou.Value].ToString().Trim();
if (col_repairtime.Value >= 0)
{
var s_rtime = dr[(int)col_repairtime.Value].ToString().Trim();
if (s_rtime.isEmpty()) s_rtime = "0";
newdr["RepairTime"] = float.Parse(s_rtime);
}
if (col_engineer.Value >= 0) newdr["uid"] = dr[(int)col_engineer.Value].ToString().Trim();
dt.Rows.Add(newdr);
}
var taE = new dsPRJTableAdapters.EETGW_JobReport_EBoardTableAdapter();
//과거데이터 삭제
if (checkBox1.Checked) taE.DeleteImport(FCOMMON.info.Login.gcode,
FCOMMON.info.Login.no,
dtPdate.Value.ToShortDateString(),
dtPdate.Value.ToShortDateString());
taE.Update((dsPRJ.EETGW_JobReport_EBoardDataTable)dt);
dt.AcceptChanges();
FCOMMON.Util.MsgI("Save OK");
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
var dlg = FCOMMON.Util.MsgQ("데이터의 SID를 ITEM정보와 확인하여 업데이트 합니다.");
if (dlg != System.Windows.Forms.DialogResult.Yes) return;
var i = FCOMMON.DBM.UpdateItemIndexbySID();
FCOMMON.Util.MsgI(i.ToString() + "건의 자료가 업데이트 되었습니다.");
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
private void numericUpDown13_ValueChanged(object sender, EventArgs e)
{
}
}
}