最近制作做了几个简单的报表,写下主要的代码。以便各位参考。
1 public void FillDW(DataTable dtbResult) 2 { 3 try 4 { 5 DataRow dtFr; 6 int row = 0; 7 int i = 0, j = 0; 8 int n = 0; 9 int nowCount = 0; 10 string tempName = null; 11 decimal num = 0; 12 string temp = null; 13 string tt = null; ; 14 decimal max = 0; 15 int count = 0; 16 17 m_objViewer.dwResult.SetRedrawOff(); 18 19 for (i = 0; i < dtbResult.Columns.Count; i++) 20 { 21 tempName = "name_" + i.ToString() + "_t.text="; 22 if (i % 2 == 0) 23 { 24 temp = tempName + """ + dtbResult.Columns[i].ColumnName + """; 25 } 26 else 27 { 28 for (int k = 0; k < dtbResult.Rows.Count; k++) 29 { 30 if (decimal.TryParse(dtbResult.Rows[k][i].ToString(), out num)) 31 { 32 if (max < num) 33 max = num; 34 } 35 } 36 temp = tempName + ""数量(" + max.ToString() + ")""; 37 } 38 m_objViewer.dwResult.Modify(temp); 39 n = dtbResult.Columns[i].ColumnName.Length; 40 if (i % 2 == 0 && n>=6) 41 { 42 43 if (n > 8) 44 { 45 tt = "name_" + i.ToString() + "_t.Width= '160'"; 46 m_objViewer.dwResult.Modify(tt); 47 temp = "name_" + i.ToString() + ".Width= '160'"; 48 m_objViewer.dwResult.Modify(temp); 49 } 50 else 51 { 52 tt = "name_" + i.ToString() + "_t.Width= '120'"; 53 m_objViewer.dwResult.Modify(tt); 54 temp = "name_" + i.ToString() + ".Width= '120'"; 55 m_objViewer.dwResult.Modify(temp); 56 } 57 } 58 else 59 { 60 tt = "name_" + i.ToString() + "_t.Width= '90'"; 61 m_objViewer.dwResult.Modify(tt); 62 temp = "name_" + i.ToString() + ".Width= '90'"; 63 m_objViewer.dwResult.Modify(temp); 64 } 65 66 //显示 67 tt = "name_" + i.ToString() + "_t.visible = '1'"; 68 m_objViewer.dwResult.Modify(tt); 69 temp = "name_" + i.ToString() + ".visible = '1'"; 70 m_objViewer.dwResult.Modify(temp); 71 72 count++; 73 max = 0; 74 } 75 //for (i = dtbResult.Columns.Count; i <= 104; i++) 76 //{ 77 // //隐藏 78 // tt = "name_" + i.ToString() + "_t.Width= '0'"; 79 // m_objViewer.dwResult.Modify(tt); 80 // temp = "name_" + i.ToString() + ".Width= '0'"; 81 // m_objViewer.dwResult.Modify(temp); 82 //} 83 84 tt = "name_0_t.Width= '150'"; 85 m_objViewer.dwResult.Modify(tt); 86 temp = "name_0.Width= '150'"; 87 m_objViewer.dwResult.Modify(temp); 88 89 nowCount = j; 90 for (int intI = 0; intI < dtbResult.Rows.Count; intI++) 91 { 92 row = this.m_objViewer.dwResult.InsertRow(0); 93 dtFr = dtbResult.Rows[intI]; 94 for (i = 0; i < dtbResult.Columns.Count; i++) 95 { 96 temp = "name_" + i.ToString(); 97 m_objViewer.dwResult.SetItemString(row, temp, dtFr[i].ToString()); 98 } 99 } 100 101 temp = "(" + this.dtDateFrom.ToShortDateString(); 102 m_objViewer.dwResult.Modify("t_from.text='" + temp + "'"); 103 m_objViewer.dwResult.Modify("t_from.visible='1'"); 104 temp ="至"+ this.dtDateTo.ToShortDateString()+")"; 105 m_objViewer.dwResult.Modify("t_to.text='" + temp + "'"); 106 m_objViewer.dwResult.Modify("t_to.visible='1'"); 107 108 m_objViewer.dwResult.SetRedrawOn(); 109 m_objViewer.dwResult.Refresh(); 110 111 } 112 catch (Exception objEx) 113 { 114 MessageBox.Show(objEx.Message); 115 } 116 117 118 }
dw.LibraryList="XX.pbl"
dw.DataWindowObject="XXX";