Example #1
0
        public int Initial(string strCfgFile,
                           out string strError)
        {
            strError = "";

            this.CfgFile = strCfgFile;

            this._cfgDom = new XmlDocument();
            try
            {
                _cfgDom.Load(strCfgFile);
            }
            catch (FileNotFoundException)
            {
                strError = "配置文件 '" + strCfgFile + "' 没有找到";
                return(-1);
            }
            catch (Exception ex)
            {
                strError = "报表配置文件 " + strCfgFile + " 打开错误: " + ex.Message;
                return(-1);
            }

            XmlNodeList nodes = _cfgDom.DocumentElement.SelectNodes("columns/column");

            if (nodes.Count > 0)
            {
                columns_dom = new XmlDocument();
                columns_dom.AppendChild(columns_dom.CreateElement("columns"));
            }

            {
                int i = 0;
                foreach (XmlElement node in nodes)
                {
                    string strName  = node.GetAttribute("name");
                    string strType  = node.GetAttribute("type");
                    string strAlign = node.GetAttribute("align");
                    string strSum   = node.GetAttribute("sum");
                    string strClass = node.GetAttribute("class");
                    string strEval  = node.GetAttribute("eval");

                    PrintColumn000 col = new PrintColumn000();
                    this.Columns.Add(col);

                    col.ColumnNumber = i;
                    col.Title        = strName;
                    col.Eval         = strEval;

                    if (string.IsNullOrEmpty(strClass) == true)
                    {
                        strClass = "c" + (i + 1).ToString();
                    }
                    col.CssClass = strClass;

                    col.Sum = StringUtil.GetBooleanValue(strSum, false);

                    col.DataType = GetColumnDataType(strType);

                    XmlElement column = columns_dom.CreateElement("column");
                    columns_dom.DocumentElement.AppendChild(column);
                    column.SetAttribute("class", strClass);
                    column.SetAttribute("align", strAlign);
                    if (string.IsNullOrEmpty(strType) == false)
                    {
                        column.SetAttribute("type", strType);
                    }
                    i++;
                }
            }

#if NO
            string strTitle = _cfgDom.DocumentElement.GetAttribute("title").Replace("\\r", "\r");
            strTitle = Global.MacroString(macro_table, strTitle);

            string strComment = _cfgDom.DocumentElement.GetAttribute("titleComment").Replace("\\r", "\r");
            strComment = Global.MacroString(macro_table, strComment);
#endif

            return(0);
        }
Example #2
0
        // 输出 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;  //  "&nbsp;";
                        }
                    }

                    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>
        }