// 输出 RML 格式的表格 // 本函数负责写入 <table> 元素 // parameters: // nTopLines 顶部预留多少行 public void OutputRmlTable(Table table, XmlTextWriter writer, int nMaxLines = -1) { // StringBuilder strResult = new StringBuilder(4096); int i, j; if (nMaxLines == -1) nMaxLines = table.Count; writer.WriteStartElement("table"); writer.WriteAttributeString("class", "table"); writer.WriteStartElement("thead"); writer.WriteStartElement("tr"); int nEvalCount = 0; // 具有 eval 的栏目个数 for (j = 0; j < this.Count; j++) { PrintColumn column = (PrintColumn)this[j]; if (column.Colspan == 0) continue; if (string.IsNullOrEmpty(column.Eval) == false) nEvalCount++; writer.WriteStartElement("th"); if (string.IsNullOrEmpty(column.CssClass) == false) writer.WriteAttributeString("class", column.CssClass); if (column.Colspan > 1) writer.WriteAttributeString("colspan", column.Colspan.ToString()); writer.WriteString(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.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; } // 内容行循环 for (i = 0; i < Math.Min(nMaxLines, table.Count); i++) { Line line = table[i]; if (engine != null) engine.SetGlobalValue("line", line); string strLineCssClass = "content"; if (this.OutputLine != null) { OutputLineEventArgs e = new OutputLineEventArgs(); e.Line = line; e.Index = i; e.LineCssClass = strLineCssClass; this.OutputLine(this, e); if (e.Output == false) continue; strLineCssClass = e.LineCssClass; } // strResult.Append("<tr class='" + strLineCssClass + "'>\r\n"); writer.WriteStartElement("tr"); writer.WriteAttributeString("class", strLineCssClass); // 列循环 for (j = 0; j < this.Count; j++) { PrintColumn column = (PrintColumn)this[j]; if (column.ColumnNumber < -1) { throw (new Exception("PrintColumn对象ColumnNumber列尚未初始化,位置" + Convert.ToString(j))); } string strText = ""; if (column.ColumnNumber != -1) { if (string.IsNullOrEmpty(column.Eval) == false) { // engine.SetGlobalValue("cell", line.GetObject(column.ColumnNumber)); strText = engine.Evaluate(column.Eval).ToString(); } else if (column.DataType == DataType.PriceDouble) { if (line.IsNull(column.ColumnNumber) == true) strText = column.DefaultValue; else { double v = line.GetDouble(column.ColumnNumber); /* NumberFormatInfo provider = new NumberFormatInfo(); provider.NumberDecimalDigits = 2; provider.NumberGroupSeparator = "."; provider.NumberGroupSizes = new int[] { 3 }; strText = Convert.ToString(v, provider); * */ strText = v.ToString("N", nfi); } } else if (column.DataType == DataType.PriceDecimal) { if (line.IsNull(column.ColumnNumber) == true) strText = column.DefaultValue; else { decimal v = line.GetDecimal(column.ColumnNumber); strText = v.ToString("N", nfi); } } else if (column.DataType == DataType.PriceDecimal) { if (line.IsNull(column.ColumnNumber) == true) strText = column.DefaultValue; else { decimal v = line.GetDecimal(column.ColumnNumber); strText = v.ToString("N", nfi); } } else if (column.DataType == DataType.Price) { // Debug.Assert(false, ""); if (line.IsNull(column.ColumnNumber) == true) strText = column.DefaultValue; // 2005/5/26 else strText = line.GetPriceString(column.ColumnNumber); } else strText = line.GetString(column.ColumnNumber, column.DefaultValue); } else { strText = line.Entry; } writer.WriteStartElement(j == 0 ? "th" : "td"); if (string.IsNullOrEmpty(column.CssClass) == false) writer.WriteAttributeString("class", column.CssClass); writer.WriteString(strText); writer.WriteEndElement(); // </td> if (this.SumLine == true && column.Sum == true && column.ColumnNumber != -1) { try { // if (column.DataType != DataType.Currency) { object v = line.GetObject(column.ColumnNumber); if (this.SumCell != null) { SumCellEventArgs e = new SumCellEventArgs(); e.DataType = column.DataType; e.ColumnNumber = column.ColumnNumber; e.LineIndex = i; e.Line = line; e.Value = v; this.SumCell(this, e); if (e.Value == null) continue; v = e.Value; } if (sums[j] == null) sums[j] = v; else { 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() + " 值的时候,抛出异常: " + ex.Message); } } } // strResult.Append("</tr>\r\n"); writer.WriteEndElement(); // </tr> } writer.WriteEndElement(); // </tbody> if (this.SumLine == true) { Line sum_line = null; if (engine != null) { // 准备 Line 对象 sum_line = new Line(0); for (j = 1; j < this.Count; j++) { PrintColumn column = (PrintColumn)this[j]; if (column.Sum == true && sums[j] != null) { sum_line.SetValue(j-1, sums[j]); } } engine.SetGlobalValue("line", sum_line); } // strResult.Append("<tr class='sum'>\r\n"); writer.WriteStartElement("tfoot"); writer.WriteStartElement("tr"); writer.WriteAttributeString("class", "sum"); for (j = 0; j < this.Count; j++) { PrintColumn column = (PrintColumn)this[j]; string strText = ""; if (j == 0) strText = "合计"; else if (string.IsNullOrEmpty(column.Eval) == false) { strText = engine.Evaluate(column.Eval).ToString(); } else if (column.Sum == true && sums[j] != null) { if (column.DataType == DataType.PriceDouble) strText = ((double)sums[j]).ToString("N", nfi); else if (column.DataType == DataType.PriceDecimal) strText = ((decimal)sums[j]).ToString("N", nfi); else if (column.DataType == DataType.Price) { strText = StatisUtil.Int64ToPrice((Int64)sums[j]); } else strText = Convert.ToString(sums[j]); if (column.DataType == DataType.Currency) { string strSomPrice = ""; string strError = ""; // 汇总价格 int nRet = PriceUtil.SumPrices(strText, out strSomPrice, out strError); if (nRet == -1) strText = strError; else strText = strSomPrice; } } else strText = column.DefaultValue; // " "; #if NO doc.WriteExcelCell( _lineIndex, j, strText, true); #endif writer.WriteStartElement(j == 0? "th" : "td"); if (string.IsNullOrEmpty(column.CssClass) == false) writer.WriteAttributeString("class", column.CssClass); writer.WriteString(strText); writer.WriteEndElement(); // </td> } // strResult.Append("</tr>\r\n"); writer.WriteEndElement(); // </tr> writer.WriteEndElement(); // </tfoot> } writer.WriteEndElement(); // </table> }