// 为一列的字符串值增量 // 本方法只能应用在string值类型的列上,否则会抛出异常 // parameters: // createValue 如果列单元不存在,则采用此值初始设置 // incValue 如果列单元已经存在,则采用原来的值后面追加此值,修改回 public void IncCurrency( int nColumn, string createValue, string incValue) { EnsureCells(nColumn); if (cells[nColumn] == null) { cells[nColumn] = createValue; } else { object oldvalue = cells[nColumn]; if (oldvalue is string) { string v = (string)oldvalue; // 连接两个价格字符串 v = PriceUtil.JoinPriceString(v, incValue); string strSumPrices = ""; string strError = ""; // 将形如"-123.4+10.55-20.3"的价格字符串归并汇总 int nRet = PriceUtil.SumPrices(v, out strSumPrices, out strError); if (nRet == 0) { v = strSumPrices; } if (nRet == -1) { throw new Exception("汇总金额字符串 '" + v + "' 时出错:" + strError); } // v += incValue; cells[nColumn] = v; } else { throw (new Exception("列" + Convert.ToString(nColumn) + "类型必须为string")); } } }
// 输出 RML 格式的表格 // 本函数负责写入 <table> 元素 // parameters: // nTopLines 顶部预留多少行 void OutputRmlTable <T>( IEnumerable <T> data_reader, object sum, XmlTextWriter writer, int nMaxLines = -1) { // StringBuilder strResult = new StringBuilder(4096); int i, j; #if NO if (nMaxLines == -1) { nMaxLines = table.Count; } #endif writer.WriteStartElement("table"); WriteAttributeString(writer, "class", "table"); writer.WriteStartElement("thead"); writer.WriteStartElement("tr"); int nEvalCount = 0; // 具有 eval 的栏目个数 for (j = 0; j < this.Columns.Count; j++) { PrintColumn000 column = this.Columns[j]; if (column.Colspan == 0) { continue; } if (string.IsNullOrEmpty(column.Eval) == false) { nEvalCount++; } writer.WriteStartElement("th"); if (string.IsNullOrEmpty(column.CssClass) == false) { WriteAttributeString(writer, "class", column.CssClass); } if (column.Colspan > 1) { WriteAttributeString(writer, "colspan", column.Colspan.ToString()); } WriteString(writer, column.Title); writer.WriteEndElement(); // </th> } writer.WriteEndElement(); // </tr> writer.WriteEndElement(); // </thead> // 合计数组 object[] sums = null; // 2008/12/1 new changed if (this.SumLine) { sums = new object[this.Columns.Count]; for (i = 0; i < sums.Length; i++) { sums[i] = null; } } NumberFormatInfo nfi = new CultureInfo("zh-CN", false).NumberFormat; nfi.NumberDecimalDigits = 2; writer.WriteStartElement("tbody"); // Jurassic.ScriptEngine engine = null; if (nEvalCount > 0 && engine == null) { engine = new Jurassic.ScriptEngine(); engine.EnableExposedClrTypes = true; } int nLineCount = 0; var handle = data_reader.GetEnumerator(); // 内容行循环 for (i = 0; ; i++) // i < Math.Min(nMaxLines, table.Count) { if (handle.MoveNext() == false) { break; } nLineCount++; #if NO if (table.HasRows == false) { break; } #endif // Line line = table[i]; if (engine != null) { engine.SetGlobalValue("line", data_reader); } string strLineCssClass = "content"; #if NO if (report.OutputLine != null) { OutputLineEventArgs e = new OutputLineEventArgs(); e.Line = line; e.Index = i; e.LineCssClass = strLineCssClass; report.OutputLine(this, e); if (e.Output == false) { continue; } strLineCssClass = e.LineCssClass; } #endif // strResult.Append("<tr class='" + strLineCssClass + "'>\r\n"); writer.WriteStartElement("tr"); WriteAttributeString(writer, "class", strLineCssClass); // 列循环 for (j = 0; j < this.Columns.Count; j++) { PrintColumn000 column = this.Columns[j]; if (column.ColumnNumber < -1) { throw (new Exception("PrintColumn对象ColumnNumber列尚未初始化,位置" + Convert.ToString(j))); } string strText = ""; if (column.ColumnNumber != -1) { // Debug.Assert(column.ColumnNumber < data_reader.FieldCount, ""); if (string.IsNullOrEmpty(column.Eval) == false) { // engine.SetGlobalValue("cell", line.GetObject(column.ColumnNumber)); engine.SetGlobalValue("rowNumber", nLineCount.ToString()); engine.SetGlobalValue("currency", new PriceUtil()); strText = engine.Evaluate(column.Eval).ToString(); } #if NO else if (column.DataType == ColumnDataType.PriceDouble) { if (data_reader.IsDBNull(column.ColumnNumber) == true) { strText = column.DefaultValue; } else { double v = data_reader.GetDouble(column.ColumnNumber); strText = v.ToString("N", nfi); } } else if (column.DataType == ColumnDataType.PriceDecimal) { if (data_reader.IsDBNull(column.ColumnNumber) == true) { strText = column.DefaultValue; } else { decimal v = data_reader.GetDecimal(column.ColumnNumber); strText = v.ToString("N", nfi); } } else if (column.DataType == ColumnDataType.PriceDecimal) { if (data_reader.IsDBNull(column.ColumnNumber) == true) { strText = column.DefaultValue; } else { decimal v = data_reader.GetDecimal(column.ColumnNumber); strText = v.ToString("N", nfi); } } else if (column.DataType == ColumnDataType.Price) { // Debug.Assert(false, ""); if (data_reader.IsDBNull(column.ColumnNumber) == true) { strText = column.DefaultValue; // 2005/5/26 } else { strText = data_reader.GetString(column.ColumnNumber); // } } else if (column.DataType == ColumnDataType.String) { // strText = data_reader.GetString(column.ColumnNumber/*, column.DefaultValue*/); // 2014/8/28 object o = data_reader.GetValue(column.ColumnNumber); if (o != null) { strText = o.ToString(); } else { strText = ""; } } #endif else { TryGetFieldValue(handle.Current, column.CssClass, out object o); // (column.ColumnNumber); if (o != null) { strText = o.ToString(); } else { strText = ""; } } } else { // strText = data_reader.GetString(0); // line.Entry; } writer.WriteStartElement(j == 0 ? "th" : "td"); if (string.IsNullOrEmpty(column.CssClass) == false) { WriteAttributeString(writer, "class", column.CssClass); } WriteString(writer, strText); writer.WriteEndElement(); // </td> if (this.SumLine == true && column.Sum == true && column.ColumnNumber != -1) { string strDebugInfo = ""; try { // if (column.DataType != DataType.Currency) { // if (column.ColumnNumber < data_reader.FieldCount) // v = data_reader.GetValue(column.ColumnNumber); TryGetFieldValue(handle.Current, column.CssClass, out object v); if (sums[j] == null) { sums[j] = v; } else { strDebugInfo = GetDebugInfo(column.DataType, sums[j], v); sums[j] = AddValue(column.DataType, sums[j], v); // sums[j] = ((decimal)sums[j]) + v; } } } catch (Exception ex) // 俘获可能因字符串转换为整数抛出的异常 { throw new Exception("在累加 行 " + i.ToString() + " 列 " + column.ColumnNumber.ToString() + " 值的时候,出现异常(strDebugInfo='" + strDebugInfo + "'): " + ExceptionUtil.GetAutoText(ex)); } } } // strResult.Append("</tr>\r\n"); writer.WriteEndElement(); // </tr> } writer.WriteEndElement(); // </tbody> if (sum != null) { writer.WriteStartElement("tfoot"); writer.WriteStartElement("tr"); WriteAttributeString(writer, "class", "sum"); for (j = 0; j < this.Columns.Count; j++) { PrintColumn000 column = this.Columns[j]; string strText = ""; if (j == 0) { strText = "合计(" + nLineCount.ToString() + "行)"; } else if (column.Sum == true) { if (string.IsNullOrEmpty(column.Eval) == false) { engine.SetGlobalValue("currency", new PriceUtil()); strText = engine.Evaluate(column.Eval).ToString(); } else if (column.Sum == true) { TryGetFieldValue(sum, column.CssClass, out object o); // (column.ColumnNumber); if (o != null) { strText = o.ToString(); } else { strText = ""; } } } writer.WriteStartElement(j == 0 ? "th" : "td"); if (string.IsNullOrEmpty(column.CssClass) == false) { WriteAttributeString(writer, "class", column.CssClass); } WriteString(writer, strText); writer.WriteEndElement(); // </td> } writer.WriteEndElement(); // </tr> writer.WriteEndElement(); // </tfoot> } else if (this.SumLine == true) { /* * SumLineReader sum_line = null; * if (engine != null) * { * // 准备 Line 对象 * sum_line = new SumLineReader(); * sum_line.FieldValues = sums; * sum_line.Read(); * engine.SetGlobalValue("line", sum_line); * engine.SetGlobalValue("rowNumber", ""); * } */ // strResult.Append("<tr class='sum'>\r\n"); writer.WriteStartElement("tfoot"); writer.WriteStartElement("tr"); WriteAttributeString(writer, "class", "sum"); for (j = 0; j < this.Columns.Count; j++) { PrintColumn000 column = this.Columns[j]; string strText = ""; if (j == 0) { strText = "合计(" + nLineCount.ToString() + "行)"; } else if (column.Sum == true) { if (string.IsNullOrEmpty(column.Eval) == false) { engine.SetGlobalValue("currency", new PriceUtil()); strText = engine.Evaluate(column.Eval).ToString(); } else if (column.Sum == true && sums[j] != null) { if (column.DataType == ColumnDataType.PriceDouble) { strText = ((double)sums[j]).ToString("N", nfi); } else if (column.DataType == ColumnDataType.PriceDecimal) { strText = ((decimal)sums[j]).ToString("N", nfi); } else if (column.DataType == ColumnDataType.Price) { strText = Int64ToPrice((Int64)sums[j]); } else { strText = Convert.ToString(sums[j]); } if (column.DataType == ColumnDataType.Currency) { // 汇总价格 int nRet = PriceUtil.SumPrices(strText, out string strSumPrice, out string strError); if (nRet == -1) { strText = strError; } else { strText = strSumPrice; } } } else { strText = column.DefaultValue; // " "; } } writer.WriteStartElement(j == 0 ? "th" : "td"); if (string.IsNullOrEmpty(column.CssClass) == false) { WriteAttributeString(writer, "class", column.CssClass); } WriteString(writer, strText); writer.WriteEndElement(); // </td> } // strResult.Append("</tr>\r\n"); writer.WriteEndElement(); // </tr> writer.WriteEndElement(); // </tfoot> } writer.WriteEndElement(); // </table> }