123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Threading;
- using System.Windows.Forms;
- using UAS_MES_NEW.DataOperate;
- using UAS_MES_NEW.Entity;
- using UAS_MES_NEW.PublicForm;
- using UAS_MES_NEW.PublicMethod;
- namespace UAS_MES_NEW.Query
- {
- public partial class Query_SpecialReport : Form
- {
- DataHelper dh = SystemInf.dh;
- Thread InitPrint;
- public Query_SpecialReport()
- {
- InitializeComponent();
- }
- private void Query_SpecialReport_Load(object sender, EventArgs e)
- {
- pr_code.TableName = "product";
- pr_code.SelectField = "pr_code # 产品编号,pr_detail # 产品名称,pr_spec # 产品规格";
- pr_code.FormName = Name;
- pr_code.DBTitle = "物料查询";
- pr_code.SetValueField = new string[] { "pr_code" };
- pr_code.Text = "12112572";
- }
- private static string lpad(int length, string number)
- {
- while (number.Length < length)
- {
- number = "0" + number;
- }
- number = number.Substring(number.Length - length, length);
- return number;
- }
- private void inoutno_TextChanged(object sender, EventArgs e)
- {
- }
- DataTable importdata;
- private void XY_Click(object sender, EventArgs e)
- {
- ImportExcel1.Filter = "(*.xls)|*.xls";
- DialogResult result;
- result = ImportExcel1.ShowDialog();
- if (result == DialogResult.OK)
- {
- XYFilePath.Text = ImportExcel1.FileName;
- importdata = ExcelToDataTable(ImportExcel1.FileName, true);
- ExportFileDialog.Description = "选择导出的路径";
- DialogResult result1 = ExportFileDialog.ShowDialog();
- if (result1 == DialogResult.OK)
- {
- InitPrint = new Thread(ExportData);
- SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "导出贴片机数据");
- BaseUtil.SetFormCenter(stw);
- stw.ShowDialog();
- }
- }
- }
- ExcelHandler excel = new ExcelHandler();
- private void ExportData()
- {
- try
- {
- string time = System.DateTime.Now.ToString("yyyyMMddhhmmss");
- Stream fs = new FileStream(ExportFileDialog.SelectedPath + @"\" + time + pr_code.Text + ".txt", FileMode.CreateNew, FileAccess.ReadWrite);
- fs.Dispose();
- List<string> list = new List<string>();
- for (int i = 0; i < importdata.Rows.Count; i++)
- {
- string Refer = importdata.Rows[i]["Refer"].ToString();
- DataTable bom = (DataTable)dh.ExecuteSql("select bd_soncode,replace(wm_concat(bd_location||';'||nvl(bd_soncode,PRE_SONCODE)||' '||PRE_REPCODE),',',' ') from BOMDetail " +
- "LEFT JOIN bom on bd_bomid=bo_id left join Product ON bd_soncode=pr_code left join ProdReplace on pre_bdid =bd_id where " +
- "bo_mothercode='" + pr_code.Text + "' and instr(','||bd_location||',','," + Refer + ",')>0 group by bd_soncode", "select");
- if (bom.Rows.Count > 0)
- {
- if (bom.Rows[0][1].ToString() != "")
- {
- if (!list.Contains(bom.Rows[0][1].ToString()))
- {
- list.Add(bom.Rows[0][1].ToString());
- }
- }
- importdata.Rows[i][1] = bom.Rows[0][0].ToString();
- }
- Process.Text = (i + 1) + "/" + importdata.Rows.Count;
- }
- excel.ExportExcel(importdata, ExportFileDialog.SelectedPath + @"\" + time + pr_code.Text + ".xls");
- StreamWriter sw = File.AppendText(ExportFileDialog.SelectedPath + @"\" + time + pr_code.Text + ".txt");
- for (int i = 0; i < list.Count; i++)
- {
- Console.WriteLine(list[i]);
- sw.WriteLine(list[i]);
- }
- sw.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- public static bool OpenCSVFile(ref DataTable mycsvdt, string filepath)
- {
- string strpath = filepath; //csv文件的路径
- try
- {
- int intColCount = 0;
- bool blnFlag = true;
- DataColumn mydc;
- DataRow mydr;
- string strline;
- string[] aryline;
- StreamReader mysr = new StreamReader(strpath, System.Text.Encoding.UTF8);
- while ((strline = mysr.ReadLine()) != null)
- {
- aryline = strline.Split(new char[] { ',' });
- //给datatable加上列名
- if (blnFlag)
- {
- blnFlag = false;
- intColCount = aryline.Length;
- int col = 0;
- for (int i = 0; i < aryline.Length; i++)
- {
- col = i + 1;
- mydc = new DataColumn(col.ToString());
- mycsvdt.Columns.Add(mydc);
- }
- }
- //填充数据并加入到datatable中
- mydr = mycsvdt.NewRow();
- for (int i = 0; i < aryline.Length; i++)
- {
- mydr[i] = aryline[i];
- }
- mycsvdt.Rows.Add(mydr);
- }
- mysr.Close();
- return true;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message + ex.StackTrace);
- return false;
- }
- }
- private void TraceFile_Click(object sender, EventArgs e)
- {
- ImportExcel1.Filter = "(*.xls)|*.xls";
- DialogResult result;
- result = ImportExcel1.ShowDialog();
- if (result == DialogResult.OK)
- {
- TraceFilePath.Text = ImportExcel1.FileName;
- DataTable dt = ExcelToDataTable1(ImportExcel1.FileName, false);
- List<string> STF_MODEL = new List<string>();
- List<string> STF_FEEDER = new List<string>();
- List<string> STF_FEEDERNO = new List<string>();
- List<string> STF_LOCATION = new List<string>();
- List<string> STF_MACHINE = new List<string>();
- string stf_code = dt.Rows[0][9].ToString();
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- STF_MODEL.Add(dt.Rows[i][0].ToString());
- STF_FEEDER.Add(dt.Rows[i][3].ToString());
- STF_FEEDERNO.Add(dt.Rows[i][11].ToString());
- STF_LOCATION.Add(dt.Rows[i][12].ToString());
- STF_MACHINE.Add(dt.Rows[i][1].ToString());
- }
- dh.ExecuteSql("delete from SMTTRACEFILE where stf_code='" + stf_code + "'", "delete");
- dh.BatchInsert("insert into SMTTRACEFILE(STF_ID,stf_prodcode, STF_MODEL, STF_FEEDER, STF_FEEDERNO, STF_LOCATION,STF_MACHINE, STF_CODE, STF_INDATE, STF_INMAN)" +
- "values(SMTTRACEFILE_SEQ.nextval,'"+pr_code.Text+"',:STF_MODEL,:STF_FEEDER,:STF_FEEDERNO,:STF_LOCATION,:STF_MACHINE,'" + stf_code + "',sysdate,'" + User.UserName + "')",
- new string[] { "STF_MODEL", "STF_FEEDER", "STF_FEEDERNO", "STF_LOCATION", "STF_MACHINE" }, STF_MODEL.ToArray(), STF_FEEDER.ToArray(), STF_FEEDERNO.ToArray(), STF_LOCATION.ToArray(), STF_MACHINE.ToArray());
- dh.ExecuteSql("delete from SMTTRACEFILE where ( STF_MODEL is null and STF_FEEDER is null and STF_FEEDERNO is null and STF_MACHINE is null ) or stf_model in ('料槽','数量','印刷板') or STF_MACHINE in('贴片生产线:','设置:') ", "delete");
- dataGridView1.DataSource = dt;
- DataTable dt1 = (DataTable)dh.ExecuteSql("select STF_ID, STF_MODEL,STF_MACHINE, STF_FEEDER, STF_FEEDERNO, STF_LOCATION,replace(pr_orispeccode,' ','\n')pr_orispeccode,bo_mothercode,bd_baseqty,bd_soncode,bd_location, STF_CODE, STF_INDATE, STF_INMAN from SMTTRACEFILE left join (\r\nselect bo_mothercode,bd_baseqty,bd_soncode,replace(bd_location,',',' ')bd_location from BOMDetail \r\nLEFT JOIN bom on bd_bomid=bo_id left join Product ON bd_soncode=pr_code left join ProdReplace on pre_bdid =bd_id where bo_mothercode='" + pr_code.Text + "' ) on bo_mothercode=stf_prodcode and bd_soncode=STF_LOCATION \r\nleft join product on pr_code=STF_LOCATION where stf_prodcode='"+pr_code.Text+"' order by stf_id", "select");
- excel.ExportExcel_LIANGAN(dt1, "C:\\Users\\callm\\Desktop\\客户资料\\"+System.DateTime.Now.ToString("yyyyMMddhhmmss")+".xls");
- }
- }
- public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
- {
- DataTable dataTable = null;
- FileStream fs = null;
- DataColumn column = null;
- DataRow dataRow = null;
- IWorkbook workbook = null;
- ISheet sheet = null;
- IRow row = null;
- ICell cell = null;
- int startRow = 0;
- try
- {
- using (fs = File.OpenRead(filePath))
- {
- // 2007版本
- workbook = new HSSFWorkbook(fs);
- if (workbook != null)
- {
- sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
- dataTable = new DataTable();
- if (sheet != null)
- {
- int rowCount = sheet.LastRowNum;//总行数
- if (rowCount > 0)
- {
- IRow firstRow = sheet.GetRow(0);//第一行
- int cellCount = firstRow.LastCellNum;//列数
- //构建datatable的列
- if (isColumnName)
- {
- startRow = 1;//如果第一行是列名,则从第二行开始读取
- for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
- {
- cell = firstRow.GetCell(i);
- if (cell != null)
- {
- if (cell.StringCellValue != null)
- {
- column = new DataColumn(cell.StringCellValue);
- dataTable.Columns.Add(column);
- }
- }
- }
- }
- else
- {
- for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
- {
- column = new DataColumn("column" + (i + 1));
- dataTable.Columns.Add(column);
- }
- }
- //填充行
- for (int i = startRow; i <= rowCount; ++i)
- {
- row = sheet.GetRow(i);
- if (row == null) continue;
- dataRow = dataTable.NewRow();
- for (int j = row.FirstCellNum; j < cellCount; ++j)
- {
- cell = row.GetCell(j);
- if (cell == null)
- {
- dataRow[j] = "";
- }
- else
- {
- //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
- switch (cell.CellType)
- {
- case CellType.BLANK:
- dataRow[j] = "";
- break;
- case CellType.NUMERIC:
- short format = cell.CellStyle.DataFormat;
- //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
- if (format == 14 || format == 31 || format == 57 || format == 58)
- dataRow[j] = cell.DateCellValue;
- else
- dataRow[j] = cell.NumericCellValue;
- break;
- case CellType.STRING:
- dataRow[j] = cell.StringCellValue;
- break;
- case CellType.FORMULA:
- dataRow[j] = cell.StringCellValue;
- break;
- }
- }
- }
- dataTable.Rows.Add(dataRow);
- }
- }
- }
- }
- }
- return dataTable;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- if (fs != null)
- {
- fs.Close();
- }
- return null;
- }
- }
- public static DataTable ExcelToDataTable1(string filePath, bool isColumnName)
- {
- DataTable dataTable = null;
- FileStream fs = null;
- DataColumn column = null;
- DataRow dataRow = null;
- IWorkbook workbook = null;
- ISheet sheet = null;
- IRow row = null;
- ICell cell = null;
- int startRow = 0;
- try
- {
- using (fs = File.OpenRead(filePath))
- {
- // 2007版本
- workbook = new HSSFWorkbook(fs);
- if (workbook != null)
- {
- sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
- dataTable = new DataTable();
- if (sheet != null)
- {
- int rowCount = sheet.LastRowNum;//总行数
- if (rowCount > 0)
- {
- IRow firstRow = sheet.GetRow(1);//第一行
- int cellCount = firstRow.LastCellNum;//列数
- //构建datatable的列
- if (isColumnName)
- {
- startRow = 1;//如果第一行是列名,则从第二行开始读取
- for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
- {
- cell = firstRow.GetCell(i);
- if (cell != null)
- {
- if (cell.StringCellValue != null)
- {
- column = new DataColumn(cell.StringCellValue);
- dataTable.Columns.Add(column);
- }
- }
- }
- }
- else
- {
- startRow = 1;
- for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
- {
- column = new DataColumn("column" + (i + 1));
- dataTable.Columns.Add(column);
- }
- }
- //填充行
- for (int i = startRow; i <= rowCount; ++i)
- {
- row = sheet.GetRow(i);
- Console.WriteLine(i);
- if (row == null) continue;
- dataRow = dataTable.NewRow();
- for (int j = 0; j < 40; ++j)
- {
- cell = row.GetCell(j);
- if (cell == null)
- {
- dataRow[j] = "";
- }
- else
- {
- //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
- switch (cell.CellType)
- {
- case CellType.BLANK:
- dataRow[j] = "";
- break;
- case CellType.NUMERIC:
- short format = cell.CellStyle.DataFormat;
- //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
- if (format == 14 || format == 31 || format == 57 || format == 58)
- dataRow[j] = cell.DateCellValue;
- else
- dataRow[j] = cell.NumericCellValue;
- break;
- case CellType.STRING:
- dataRow[j] = cell.StringCellValue;
- break;
- case CellType.FORMULA:
- dataRow[j] = cell.StringCellValue;
- break;
- }
- }
- }
- dataTable.Rows.Add(dataRow);
- }
- }
- }
- }
- }
- return dataTable;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- if (fs != null)
- {
- fs.Close();
- }
- return null;
- }
- }
- }
- }
|