//多条打码机的数据可以写到一个文件上发送过去,不过每次发送的明细不能超过一万条 private void export(string synseq, string groupno, string linetype)//lineno打码机编号 { this.btn_export.Enabled = false; panel2.Visible = true; label2.Visible = true; progressBar1.Visible = true; label2.Text = "数据查询中,准备进行数据导出..."; label2.Refresh(); int taskseq = 0, seq = 1; String tasknum = "", cuscode = "", cusname = "", itemno = "", itemname = "", quantity = "", regioncode = "", orderdate = "", cuscodetmp = ""; List <InfoExport> list = new List <InfoExport>(); list = ExportClass.Export(Convert.ToDecimal(synseq)); //取批次号 //String batchcodesql = "select SEQ_ONEHAOGONGCHENG.Nextval from dual"; int len = list.Count; String onesynseq = "0";//一号工程批次号 //DataTable table = Db.Query(sql); //int len = table.Rows.Count; //String[] infostr = new String[len]; if (len > 0) { InitSocket(); //DataRow row = new DataRow(); //创建到处目录 String folder = "HighSpeedExportData"; if (!Directory.Exists("D:\\" + folder)) { Directory.CreateDirectory("D:\\" + folder); } int count = 0, fileSeq = 1, rowCcount = 0, bz = 0, succCount = 0; String fileNameStr = ""; String info = "", tmpInfo = "", tempCode = "", unSuccFile = ""; for (int i = 0; i < len; i++) { progressBar1.Value = ((i + 1) * 100 / len); progressBar1.Refresh(); InfoExport infos = new InfoExport(); infos = list[i]; tasknum = infos.SortNum.ToString(); cuscode = infos.CustomerCode.ToString(); cusname = infos.CustomerName.ToString(); itemno = infos.CigaretteCode.ToString(); itemname = infos.CigaretteName.ToString(); quantity = infos.PokeNum.ToString(); regioncode = infos.RegionCode.ToString(); orderdate = infos.Orderdate.ToString(); //lineno = row["SORTNAME"].ToString(); taskseq++; rowCcount = rowCcount + 1; label2.Text = "正在导出第" + fileSeq + "个文件..."; label2.Refresh(); //取下条记录比较车组与零售户 if (i + 1 < len) { cuscodetmp = list[i + 1].CustomerCode.ToString(); tempCode = list[i + 1].RegionCode.ToString(); } else { cuscodetmp = ""; tempCode = ""; } //infostr[i] = tasknum + taskseq + "," + tasknum + "," + cuscode + "," + cusname + "," + itemno + "," + itemname + "," + quantity + ",2," + seq + "," + regioncode + "," + regioncode + "," + orderdate + "," + orderdate + ",S1001,1"; tmpInfo = tmpInfo + tasknum + "," + tasknum + "," + cuscode + "," + cusname + "," + itemno + "," + itemname + "," + quantity + "," + onesynseq + "," + seq + "," + regioncode + "," + regioncode + "," + orderdate + "," + orderdate + ",1;\r\n"; if (cuscode != cuscodetmp) { taskseq = 0; seq++; } //当下个车组与当前不同时 if (!tempCode.Equals(regioncode)) { //判断累计记录数是否大于10000,大于则将之前的记录数导出,否则将记录数合并 if (rowCcount + count > 1000000000) { label2.Text = "正在压缩第" + fileSeq + "个文件..."; label2.Refresh(); //将之前的记录信息导出 DateTime dt = DateTime.Now; String time = string.Format("{0:yyyyMMddHHmmssfff}", dt); String filename = "RetailerOrder-" + time + "-" + synseq + "-" + fileSeq; fileNameStr = fileNameStr + "," + filename + ".zip"; StreamWriter sw = new StreamWriter("D:\\HighSpeedExportData\\" + filename + ".Order", false, Encoding.UTF8); sw.WriteLine(info.Substring(0, info.Length - 1)); sw.Close();//写入 //CompressFile("D:\\" + filename + ".Order"); GetFileToZip("D:\\HighSpeedExportData\\" + filename + ".Order", "D:\\HighSpeedExportData\\" + filename + ".zip", filename + ".Order"); //发送数据 label2.Text = "正在发送第" + fileSeq + "个文件"; label2.Refresh(); bz = sendFile("D:\\HighSpeedExportData\\" + filename + ".zip"); //记录发送成功数量和失败文件信息 if (bz == 0) { succCount = succCount + 1; label2.Text = "第" + fileSeq + "个文件发送完毕!"; } else { unSuccFile = unSuccFile + "," + filename + ".zip"; label2.Text = "第" + fileSeq + "个文件发送失败!"; } label2.Refresh(); //记录新车组的信息 fileSeq = fileSeq + 1; count = rowCcount; info = tmpInfo; } else { count = rowCcount + count; info = info + tmpInfo; } //判断循环是否完成(是否为最后一条记录),如果是最后一条,则将剩余记录导出 if ("".Equals(tempCode)) { //fileSeq = fileSeq + 1; label2.Text = "正在压缩第" + fileSeq + "个文件"; label2.Refresh(); DateTime dt = DateTime.Now; String time = string.Format("{0:yyyyMMddHHmmssfff}", dt); String filename = "RetailerOrder-" + time + "-" + synseq + "-" + fileSeq; fileNameStr = fileNameStr + "," + filename + ".zip"; StreamWriter sw = new StreamWriter("D:\\HighSpeedExportData\\" + filename + ".Order", false, Encoding.UTF8); sw.WriteLine(info.Substring(0, info.Length - 1)); sw.Close();//写入 //CompressFile("D:\\" + filename + ".Order"); GetFileToZip("D:\\HighSpeedExportData\\" + filename + ".Order", "D:\\HighSpeedExportData\\" + filename + ".zip", filename + ".Order"); //发送数据 //sendFile("D:\\HighSpeedExportData\\" + filename + ".zip"); label2.Text = "正在发送第" + fileSeq + "个文件"; label2.Refresh(); bz = sendFile("D:\\HighSpeedExportData\\" + filename + ".zip"); //记录发送成功数量和失败文件信息 if (bz == 0) { succCount = succCount + 1; label2.Text = "第" + fileSeq + "个文件发送完毕!"; } else { unSuccFile = unSuccFile + "," + filename + ".zip"; label2.Text = "第" + fileSeq + "个文件发送失败!"; } label2.Refresh(); } tmpInfo = ""; rowCcount = 0; } } //在弹窗前关闭控件 panel2.Visible = false; label2.Visible = false; progressBar1.Visible = false; String msg = "成功导出" + fileSeq + "个文件,成功发送" + succCount + "个文件!"; //导出的文件的所有文件名 if (!"".Equals(fileNameStr)) { fileNameStr = fileNameStr.Substring(1); } //发送失败的所有文件名 if (!"".Equals(unSuccFile)) { unSuccFile = unSuccFile.Substring(1); msg = msg + "其中发送失败文件为(" + unSuccFile + ")!"; } else { msg = msg + "文件名为(" + fileNameStr + ")!"; } MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); socketClient.Disconnect(false); socketClient.Close(); } this.btn_export.Enabled = true; }
async private void Button_Click(object sender, RoutedEventArgs e) { progressBar.Visibility = Visibility.Visible; exportBtn.IsEnabled = false; //DbAccess da = new DbAccess(); await da.InitCon(); dt = await da.ExecuteQuery(); // var masterDatas = new List<MasterData>(); await Task.Factory.StartNew(() => { // var masterDatas = new List<MasterData>(); mainMasterData = ts.GetDataTables(dt); var result = mainMasterData.SelectMany(m => m.ReferenceRanges, (MasterData, ReferenceRange) => new { MasterData, ReferenceRange }).ToList(); foreach (var item in result) { FullData f = new FullData() { PanelId = item.MasterData.PanelId, PanelName = item.MasterData.PanelName, TestId = item.MasterData.TestId, TestName = item.MasterData.TestName, LabsectionId = item.MasterData.LabsectionId, Labsection = item.MasterData.Labsection, Units = item.MasterData.Units, TubeId = item.MasterData.TubeId, TubeName = item.MasterData.TubeName, SampleTypeId = item.MasterData.SampleTypeId, SampleTypeName = item.MasterData.SampleTypeName, RefRangeType = item.ReferenceRange.RefRangeType, Sex = item.ReferenceRange.Sex, SpecialPeriod = item.ReferenceRange.SpecialPeriod, TimeScale = item.ReferenceRange.TimeScale, From = item.ReferenceRange.From, To = item.ReferenceRange.To, DiscardLow = item.ReferenceRange.DiscardLow, PanicLow = item.ReferenceRange.PanicLow, RefRange = item.ReferenceRange.RefRange, PanicHigh = item.ReferenceRange.PanicHigh, DiscardHigh = item.ReferenceRange.DiscardHigh }; fD.Add(f); } }); progressBar.Visibility = Visibility.Collapsed; using (SaveFileDialog sf = new SaveFileDialog { Filter = "Excel File|*.xlsx", Title = "Save an Excel File" }) { sf.ShowDialog(); var name = sf.FileName; if (name != "") { FileInfo f = new FileInfo(name); ExportClass ex = new ExportClass(fD, f); progressBar.Visibility = Visibility.Visible; await ex.Export(); progressBar.Visibility = Visibility.Collapsed; exportBtn.IsEnabled = true; System.Windows.MessageBox.Show("File created!"); } } }