Bogart size,scl,yy,分sheet,进程

    Private T As Thread
1
Private Sub cmdExprot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExprot.Click 2 If Not T Is Nothing Then 3 If T.ThreadState = ThreadState.Running Then Exit Sub 4 End If 5 T = New Thread(AddressOf Export) 6 T.Start() 7 End Sub 8 9 Private Function checkParameter() As Boolean 10 If Me.cboCustomer.Text.Trim.Length = 0 Then 11 MsgBox("Customer is required.", MsgBoxStyle.Exclamation, Me.Text) 12 cboCustomer.Focus() 13 Return False 14 End If 15 If Me.cboDateFrom.Text.Trim.Length = 0 Then 16 MsgBox("CP Date From is required.", MsgBoxStyle.Exclamation, Me.Text) 17 cboDateFrom.Focus() 18 Return False 19 End If 20 If Me.cboDateTo.Text.Trim.Length = 0 Then 21 MsgBox("CP Date To is required.", MsgBoxStyle.Exclamation, Me.Text) 22 cboDateTo.Focus() 23 Return False 24 End If 25 Return True 26 End Function 27 28 Private Sub Export() 29 If checkParameter() = False Then Exit Sub 30 Try 31 gSub.setPrompt("Please wait! Loading data...") 32 lblMsg.Text = "Loading data...... " 33 lblMsg.Refresh() 34 Me.Cursor = Cursors.WaitCursor 35 36 'scl# 37 Dim sql As String = "select distinct t1.scl# from orfords t1 left join orforda t2 on t1.cstord=t2.cstord left join orflcch t3 on t1.cstord=t3.cstord and t1.deg=t3.deg and t1.com=t3.com where t2.ekey like '" + CStr(Me.cboCustomer.SelectedValue).Replace("'", "''") + "%' and date(dt#05y||'-'||dt#05m||'-'||dt#05d) between '" + Me.cboDateFrom.Text.Trim.Replace("'", "''") + "' and '" + Me.cboDateTo.Text.Trim.Replace("'", "''") + "' and shq>0 " 38 Dim tbScl As DataTable = gData.GetDataTable(sql, netConn) 39 If tbScl.Rows.Count = 0 Then 40 MessageBox.Show("no data.") 41 Exit Sub 42 End If 43 Dim header As String = "" 44 45 '---------------------進度條------------------------ 46 sql = "select distinct t1.cstord,t1.deg,t1.com,t2.clot,t2.ekey,t1.scl#,t1.scld,(dt#05y||'-'||dt#05m||'-'||dt#05d) as cpdate from orfords t1 left join orforda t2 on t1.cstord=t2.cstord left join orflcch t3 on t1.cstord=t3.cstord and t1.deg=t3.deg and t1.com=t3.com where t2.ekey like '" + CStr(Me.cboCustomer.SelectedValue).Replace("'", "''") + "%' and date(dt#05y||'-'||dt#05m||'-'||dt#05d) between '" + Me.cboDateFrom.Text.Trim.Replace("'", "''") + "' and '" + Me.cboDateTo.Text.Trim.Replace("'", "''") + "' and shq>0" 47 Dim tb As DataTable = gData.GetDataTable(sql, netConn) 48 prbProc.Value = 0 49 prbProc.Minimum = 0 50 prbProc.Maximum = tb.Rows.Count + 1 51 '---------------------進度條------------------------ 52 53 Dim xApp As Excel.Application = New Excel.Application 54 Dim xBook As Excel.Workbook 55 xBook = xApp.Workbooks.Add 56 '按scl#循環 57 Dim xsheetnum As Integer = 0 58 For Each R_scl As DataRow In tbScl.Rows 59 Dim xSheet As New Excel.Worksheet 60 xsheetnum = xsheetnum + 1 61 If xsheetnum >= 4 Then 62 xSheet = xBook.Worksheets.Add(after:=xBook.Worksheets(xBook.Worksheets.Count)) 63 Else 64 xSheet = xBook.Worksheets(xsheetnum) 65 End If 66 xSheet.Name = CStr(R_scl.Item("scl#")).Replace("/", "|") 67 xSheet.Cells.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter 68 xSheet.Cells.VerticalAlignment = Excel.XlHAlign.xlHAlignCenter 69 70 xSheet.Cells.RowHeight = 14.5 71 xSheet.Cells.EntireColumn.AutoFit() 72 xSheet.Range("A1").Resize(1, 8).RowHeight = 33 73 xApp.WindowState = Excel.XlWindowState.xlMaximized 74 '1頁寬 75 With xSheet.PageSetup 76 .LeftMargin = xApp.Application.InchesToPoints(0.25) 77 .RightMargin = xApp.Application.InchesToPoints(0.25) 78 .TopMargin = xApp.Application.InchesToPoints(0.3) 79 .BottomMargin = xApp.Application.InchesToPoints(0.3) 80 .HeaderMargin = xApp.Application.InchesToPoints(0.3) 81 .FooterMargin = xApp.Application.InchesToPoints(0.3) 82 .PrintComments = xlPrintNoComments 83 .Orientation = Excel.XlPageOrientation.xlPortrait 84 .Draft = False 85 .FirstPageNumber = xlAutomatic 86 .Order = xlDownThenOver 87 .BlackAndWhite = False 88 .Zoom = False 89 .FitToPagesWide = 1 90 .FitToPagesTall = 10000 91 .PrintErrors = xlPrintErrorsDisplayed 92 .PaperSize = 120 93 .PrintGridlines = True 94 .CenterHorizontally = True 95 End With 96 Dim len As Integer = 1 97 98 gData.ExecuteCommand("drop table sumsize_temp", SqlConnect) 99 gData.ExecuteCommand("create table sumsize_temp (sug nvarchar(50),clot char(10),lotno char(10),deg char(10),ekey nvarchar(8),colorcode nvarchar(50),color nvarchar(100),size char(10),quantity numeric)", SqlConnect) '每個scl#裏面RM在對應color和size的用量 100 header = "B&B foam article RM code,Call lot, Customer,Col nb," 101 102 '這個日期里面這個客戶的訂單里面有哪些款 103 'key:t1.cstord,t1.deg,t1.com,t1.scl#,t1.scld 104 sql = "select distinct t1.cstord,t1.deg,t1.com,t2.clot,t2.ekey,t1.scl#,t1.scld,(dt#05y||'-'||dt#05m||'-'||dt#05d) as cpdate from orfords t1 left join orforda t2 on t1.cstord=t2.cstord left join orflcch t3 on t1.cstord=t3.cstord and t1.deg=t3.deg and t1.com=t3.com where t2.ekey like '" + CStr(Me.cboCustomer.SelectedValue).Replace("'", "''") + "%' and date(dt#05y||'-'||dt#05m||'-'||dt#05d) between '" + Me.cboDateFrom.Text.Trim.Replace("'", "''") + "' and '" + Me.cboDateTo.Text.Trim.Replace("'", "''") + "' and t1.scl#='" + CStr(R_scl.Item("scl#")) + "' and shq>0 order by t1.cstord" 105 Dim tbMain As DataTable = gData.GetDataTable(sql, netConn) 106 107 '每個款到so找max(款),max(soissued)的這條so里面的rmcode 108 For Each R_main As DataRow In tbMain.Rows 109 110 lblMsg.Text = "Please wait while creating BNB Order Forecast Report " + CStr(R_main.Item("cstord")) + " of scl " + CStr(R_scl.Item("scl#")) + "...... " 111 lblMsg.Refresh() 112 prbProc.Value = prbProc.Value + 1 113 Application.DoEvents() 114 115 116 Dim styleno As String = gData.SelectValue("select isnull(max(styleno),'') from SOT_SampleHead where styleno like'" + Mid(CStr(R_main.Item("deg")), 1, 6) + "%'", SqlConnect, "") 117 Dim soissued As String = gData.SelectValue("select isnull(max(soissued),'') from SOT_SampleHead where styleno='" + styleno + "'", SqlConnect, "") 118 sql = "select distinct rmcode,itemdim1 from SOT_SampleHead t1 left join SampleOrderDetail t2 on t1.styleno=t2.styleno and t1.style2=t2.style2 and t1.stype=t2.sampletype and t1.sver=t2.sver where t1.styleno='" + styleno + "' and t1.soissued='" + soissued + "' and left(rmcode,2)='84'" 119 Dim tbSO As DataTable = gData.GetDataTable(sql, SqlConnect) 120 If tbSO.Rows.Count = 0 Then GoTo SS 121 Dim sugs As String = "" 122 For i As Integer = 0 To tbSO.Rows.Count - 1 123 If i = tbSO.Rows.Count - 1 Then 124 sugs = sugs + "'" + CStr(tbSO.Rows(i).Item("rmcode")) + "'" 125 Else 126 sugs = sugs + "'" + CStr(tbSO.Rows(i).Item("rmcode")) + "'," 127 End If 128 Next 129 '回到protex尋找這個款的max(deg)這個物料(sug,ovy)的com='FGTBC'且skey='BGFO'的所有bom3 130 '在protex中的max(deg) 131 Dim deg As String = gData.SelectValue("select max(deg) from pcfbomq where deg like '" + Mid(CStr(R_main.Item("deg")), 1, 6) + "%'", netConn, "") 132 sql = "select distinct t1.sug,t2.ovy,t2.bom3 from phfrmt t1 left join pcfcrdb t2 on t1.sug=t2.sug where t2.dcrd='" + deg + "' and t2.com='FGTBC' and t1.skey='BGFO' and t1.sug in(" + sugs + ")" 133 Dim tbRM As DataTable = gData.GetDataTable(sql, netConn) 134 If tbRM.Rows.Count = 0 Then GoTo SS 135 '為提升效率,先找出指定sug的所有ovy的bom3,再把不存在的ovy的bom3刪除 136 'For Each rm As DataRow In tbRM.Rows 137 ' Dim r() As DataRow = tbSO.Select("rmcode='" + rm.Item("sug") + "' and itemdim1='" + rm.Item("ovy") + "'") 138 ' If r.Length = 0 Then 139 ' rm.Delete() 140 ' End If 141 'Next 142 'tbRM.AcceptChanges() 143 'If tbRM.Rows.Count = 0 Then GoTo SS 144 145 Dim sizeqty(2, 10) As Object 146 Dim size(2, 10) As Object 147 Dim YY(2, 10) As Object 148 149 '----------------------------------有細數的size---------------------------------------- 150 For i As Integer = 0 To 1 151 'size quantity 152 sql = "select shq#1,shq#2,shq#3,shq#4,shq#5,shq#6,shq#7,shq#8,shq#9,shq#10 from orfords where cstord='" + CStr(R_main.Item("cstord")) + "' and deg='" + CStr(R_main.Item("deg")) + "' and scl#='" + CStr(R_main.Item("scl#")) + "' and scls='" + CStr(i) + "' and coalesce(scld,'')='" + CStr(R_main.Item("scld")) + "' and com='" + CStr(R_main.Item("com")) + "'" 153 Dim tbSizeqty As DataTable = gData.GetDataTable(sql, netConn) 154 155 'size 156 sql = " select sz01,sz02,sz03,sz04,sz05,sz06,sz07,sz08,sz09,sz10,coalesce(scld,'') as scld from pcfsclc t1 left join pcfscld t2 on t1.scl#=t2.scl# where t1.scl#='" + CStr(R_main.Item("scl#")) + "' and scls='" + CStr(i) + "' and coalesce(scld,'')='" + CStr(R_main.Item("scld")) + "'" 157 Dim tbSize As DataTable = gData.GetDataTable(sql, netConn) 158 159 For j As Integer = 0 To 9 160 sizeqty(i, j) = tbSizeqty.Rows(0).Item("shq#" + CStr(j + 1)) 161 If j = 9 Then 162 size(i, j) = CStr(tbSize.Rows(0).Item("sz10")) + CStr(tbSize.Rows(0).Item("scld")) 163 Else 164 size(i, j) = CStr(tbSize.Rows(0).Item("sz0" + CStr(j + 1))) + CStr(tbSize.Rows(0).Item("scld")) 165 End If 166 Next 167 Next 168 '----------------------------------有細數的size-------------------- 169 170 '----------------------------------RM在對應size的YY------------------------------- 171 Dim cscomd As String = "" 172 'max(bom1) 173 Dim bom1 As String = gData.SelectValue("select max(bom1) from pcfbomq where deg ='" + deg + "'", netConn, "") 174 For i As Integer = 0 To tbRM.Rows.Count - 1 175 For m As Integer = 0 To 1 176 'YY 177 '找max(deg),max(bom1)的這個物料(bom3)的YY 178 sql = "select qbrt01,qbrt02,qbrt03,qbrt04,qbrt05,qbrt06,qbrt07,qbrt08,qbrt09,qbrt10 from pcfbomq t1 where t1.deg='" + deg + "' and bom1='" + bom1 + "' and t1.scl#='" + CStr(R_main.Item("scl#")) + "' and scls='" + CStr(m) + "' and bom3='" + CStr(tbRM.Rows(i).Item("bom3")) + "' and coalesce(scld,'')='" + CStr(R_main.Item("scld")) + "'" 179 Dim tbYY As DataTable = gData.GetDataTable(sql, netConn) 180 If tbYY.Rows.Count = 0 Then GoTo SS 181 For n As Integer = 0 To 9 182 If n = 9 Then 183 YY(m, n) = tbYY.Rows(0).Item("qbrt10") 184 Else 185 YY(m, n) = tbYY.Rows(0).Item("qbrt0" + CStr(n + 1)) 186 End If 187 If Val(sizeqty(m, n)) > 0 Then 188 Dim tbCom As DataTable = gData.GetDataTable("select distinct cscomd from orflcch where cstord='" + R_main.Item("cstord") + "' and deg='" + R_main.Item("deg") + "' and ekey='" + CStr(Me.cboCustomer.SelectedValue).Replace("'", "''") + "' and com='" + CStr(R_main.Item("com")) + "'", netConn) 189 If tbCom.Rows.Count > 0 Then 190 cscomd = tbCom.Rows(0).Item("cscomd") 191 Else 192 cscomd = "" 193 End If 194 gData.ExecuteCommand("insert into sumsize_temp values('" + tbRM.Rows(i).Item("sug") + "','" + R_main.Item("clot") + "','" + R_main.Item("cstord") + "','" + R_main.Item("deg") + "','" + R_main.Item("ekey") + "','" + R_main.Item("com") + "','" + cscomd + "','" + size(m, n) + "'," + CStr(Val(sizeqty(m, n)) * Val(YY(m, n))) + ")", SqlConnect) 195 End If 196 Next 197 Next 198 Next 199 '----------------------------------RM在對應size的YY------------------------------- 200 SS: 201 Next 202 '-------------------------get report data------------------------- 203 sql = "select distinct sug,clot,ekey,colorcode,color from sumsize_temp" 204 Dim tbRow As DataTable = gData.GetDataTable(sql, SqlConnect) 205 If tbRow.Rows.Count = 0 Then GoTo SS2 206 sql = "select distinct size from sumsize_temp" 207 Dim tbColumn As DataTable = gData.GetDataTable(sql, SqlConnect) 208 For Each r As DataRow In tbColumn.Rows 209 header = header + r.Item("size") + "," 210 Next 211 212 header = header + "Requested Date,CP Date,L/D apr Date" 213 Dim head() As String = header.Split(",") 214 If head.Length > len Then len = head.Length 215 Dim data(tbRow.Rows.Count + 1, head.Length) As Object 216 217 sql = "select sug,clot,colorcode,color,size,sum(quantity) as qty from sumsize_temp group by sug,clot,colorcode,color,size" 218 Dim tbData As DataTable = gData.GetDataTable(sql, SqlConnect) 219 220 For i As Integer = 0 To tbRow.Rows.Count 221 If i = 0 Then 222 For j As Integer = 0 To head.Length - 1 223 data(i, j) = head(j) 224 Next 225 Else 226 data(i, 0) = tbRow.Rows(i - 1).Item("sug") 227 data(i, 1) = tbRow.Rows(i - 1).Item("clot") 228 data(i, 2) = tbRow.Rows(i - 1).Item("ekey") 229 data(i, 3) = tbRow.Rows(i - 1).Item("color") 230 For k As Integer = 4 To 4 + tbColumn.Rows.Count - 1 231 Dim r() As DataRow = tbData.Select("sug='" + tbRow.Rows(i - 1).Item("sug") + "' and clot='" + tbRow.Rows(i - 1).Item("clot") + "' and colorcode='" + tbRow.Rows(i - 1).Item("colorcode") + "' and size='" + head(k) + "'") 232 If r.Length = 0 Then 233 data(i, k) = 0 234 Else 235 data(i, k) = r(0).Item("qty") 236 End If 237 Next 238 'Requested Date 239 sql = "select min(cpdate) as cpdate from CPM_Detail where clot='" + CStr(tbRow.Rows(i - 1).Item("clot")) + "' and itmcde=20 and isnull(cpdate,'')<>''" 240 data(i, head.Length - 3) = gData.SelectValue(sql, SqlConnect, "") 241 'CP Date 242 sql = "select min(date(dt#05y||'-'||dt#05m||'-'||dt#05d)) from orfords t1 left join orforda t2 on t1.cstord=t2.cstord left join orflcch t3 on t1.cstord=t3.cstord and t1.deg=t3.deg where t2.ekey like '" + CStr(tbRow.Rows(i - 1).Item("ekey")).Replace("'", "''") + "%' and date(dt#05y||'-'||dt#05m||'-'||dt#05d) between '" + Me.cboDateFrom.Text.Trim.Replace("'", "''") + "' and '" + Me.cboDateTo.Text.Trim.Replace("'", "''") + "' and t2.clot='" + CStr(tbRow.Rows(i - 1).Item("clot")) + "'" 243 data(i, head.Length - 2) = gData.SelectValue(sql, netConn, "") 244 'L/D apr Date 245 sql = "select max(date(left(appdte,4)||'-'||left(right(appdte,4),2)||'-'||right(appdte,2))) as appdte from proda201.orflccb where clot=(select pyord from imfcltb where clot='" + CStr(tbRow.Rows(i - 1).Item("clot")) + "') and sug='" + CStr(tbRow.Rows(i - 1).Item("sug")) + "'" 246 data(i, head.Length - 1) = gData.SelectValue(sql, netConn, "") 247 End If 248 Next 249 '-------------------------get report data------------------------- 250 251 'fill data 252 With xSheet.Range("A2").Resize(tbRow.Rows.Count + 1, head.Length) 253 .Value = data 254 .Font.Name = "Arial" 255 .Font.Size = 10 256 .Borders.LineStyle = 1 257 End With 258 With xSheet.Range("A2").Resize(1, head.Length) 259 .Interior.Color = RGB(192, 192, 192) '底色 260 .RowHeight = 14.25 261 End With 262 263 SS2: 264 gData.ExecuteCommand("drop table sumsize_temp", SqlConnect) 265 266 '-------------------------輸出主標題------------------- 267 With xSheet.Range("A1").Resize(1, len) 268 .Merge() 269 .Value = "B&B Order Forecast Report" 270 .Font.Name = "Arial" 271 .Font.Size = 20 272 .Font.Bold = True 273 .HorizontalAlignment = Excel.Constants.xlCenter 274 .VerticalAlignment = Excel.Constants.xlCenter 275 End With 276 '-------------------------輸出主標題------------------- 277 xSheet.Range("A1").Resize(1, len).EntireColumn.AutoFit() 278 xSheet.Range("A1").Resize(1, len).EntireRow.AutoFit() 279 Next 280 281 lblMsg.Text = "Ready" 282 lblMsg.Refresh() 283 prbProc.Value = prbProc.Value + 1 284 Application.DoEvents() 285 286 287 xApp.Range("A1").Select() 288 289 xApp.ActiveWindow.Zoom = 100 290 xApp.Visible = True 291 xApp.Caption = "Bogart ProTex Report" 292 xApp.ActiveWindow.Caption = "Data Export" 293 294 xBook = Nothing 295 xApp = Nothing 296 GC.Collect() 297 Catch ex As Exception 298 ErrorMsg.Show(ex) 299 Finally 300 gSub.setPrompt("Ready") 301 lblMsg.Text = "Ready" 302 lblMsg.Refresh() 303 prbProc.Value = 0 304 Application.DoEvents() 305 Me.Cursor = Cursors.Default 306 End Try 307 End Sub
vinson
原文地址:https://www.cnblogs.com/vinsonLu/p/3025760.html