使用LINQ to XML进行分组。创建xls小计

时间:2013-12-11 11:01:40

标签: linq-to-xml xls

我正在尝试使用这种格式获取xls文件,Totals的红色行。

enter image description here

我使用XDocument和XElement中的下一个代码获取详细信息(“工作表”,...)

from item in lista
select
(new XElement("Row",new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), item.project)),
                    new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), item.type)),
                    new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), item.subtype)),
                    new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "Number"), item.ppto)),
                    new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "Number"), item.amount))
            )
)

如何按项目,类型和子类型获取总计分组的节点?

此致

1 个答案:

答案 0 :(得分:0)

这里我发布了详细的XML代码。在代码中,您可以看到 lista 的来源。 目标是准备用Excel打开的XML文件。代码运行良好,但我需要添加小计(图像中的红线) 此致,玛丽亚

namespace reportsToXLS
{
    public partial class myMvtos : System.Web.UI.Page
    {
        protected void btnExportDatos_Click(object sender, EventArgs e)
        {
            OleDbConnection con = null;
            OleDbCommand cm = null;
            OleDbDataReader dr = null;

            string sql = " SELECT project,type,subtype,ppto,amount from table_mvtos"; 

            List<Movimiento> lstMovimientos = new List<Movimiento>();

            con = Conexion.Obtain("myConection");

            con.Open();
            cm = new OleDbCommand(sql, con);
            cm.CommandTimeout = 240;
            dr = cm.ExecuteReader();

            while (dr.Read())
            {
                Movimiento mov = new Movimiento();
                mov.project = dr.IsDBNull(dr.GetOrdinal("project")) ? "" :  dr.GetString(dr.GetOrdinal("project"));
                mov.type = dr.GetString(dr.GetOrdinal("type"));
                mov.subtype = dr.IsDBNull(dr.GetOrdinal("subtype")) ? "" : dr.GetString(dr.GetOrdinal("subtype")).Trim();
                mov.ppto = dr.IsDBNull(dr.GetOrdinal("ppto"))?0: Convert.ToDouble(dr.GetDecimal(dr.GetOrdinal("ppto")));
                mov.amount = dr.IsDBNull(dr.GetOrdinal("amount")) ? 0 : Convert.ToDouble(dr.GetDecimal(dr.GetOrdinal("amount")));

                lstMovimientos.Add(mov);

            }

            leerDatos(lstMovimientos);
        }


        protected void leerDatos(List<Movimiento> lista)
        {
            string nombreFichero;
            string ruta;

            try
            {
                XNamespace aw = "urn:schemas-microsoft-com:office:spreadsheet";
                XNamespace o = "urn:schemas-microsoft-com:office:office";
                XNamespace x = "urn:schemas-microsoft-com:office:excel";
                XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
                XNamespace html = "http://www.w3.org/TR/REC-html40";

                XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                   new XElement("Workbook",
                      new XAttribute("Xmlns", "urn:schemas-microsoft-com:office:spreadsheet"),
                      new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office"),
                      new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel"),
                      new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet"),
                      new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40"),
                      new XElement("DocumentProperties", new XAttribute("Xmlns", "urn=schemas-microsoft-com:office:excel"),
                                                         new XElement("Author", "María Pedreira"),
                                                         new XElement("Created", DateTime.Today),
                                                         new XElement("Company", "Coremain SLU")
                                  ),
                      new XElement("ExcelWorkBook",
                                    new XAttribute("Xmlns", "urn=schemas-microsoft-com:office:excel"),
                                    new XElement("WindowHeight", "16795"),
                                    new XElement("WindowWidth", "8460"),
                                    new XElement("WindowTopX", "120"),
                                    new XElement("WindowTopY", "15"),
                                    new XElement("ProtectStructure", "False"),
                                    new XElement("ProtectWindows", "False")
                                  ),
                      new XElement("Styles", new XElement("Style", new XAttribute(ss + "ID", "Default"), new XAttribute(ss + "Name", "Normal"),
                                                            new XElement("Alignment", "", new XAttribute(ss + "Vertical", "Bottom")),
                                                            new XElement("Borders", ""),
                                                            new XElement("Font", ""),
                                                            new XElement("Interior", ""),
                                                            new XElement("NumberFormat", ""),
                                                            new XElement("Protection", "")
                                                         ),
                                            new XElement("Style", new XAttribute(ss + "ID", "s21"), new XElement("Font", "", new XAttribute(x + "Family", "Swiss"), new XAttribute(ss + "Bold", "1")))
                                  ),
                      new XElement("Worksheet",
                                    new XAttribute(ss + "Name", "Sheet1"),
                                    new XElement("Table",
                                                          new XElement("Row",  new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), "PROJECT")),
                                                                                new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), "TYPE")),
                                                                                new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), "SUBTYPE")),
                                                                                new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), "PPTO")),
                                                                                new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), "AMOUNT"))
                                                                        ),
                                                            from item in lista
                                                            select
                                                            new XElement("Row", new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), item.project)),
                                                                                new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "String"), item.type)),
                                                                                new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "Number"), item.ppto)),
                                                                                new XElement("Cell", new XElement("Data", new XAttribute(ss + "Type", "Number"), item.amount))
                                                                        )
                                                 ),
                                    new XElement("WorksheetOptions",
                                                          new XAttribute("Xmlns", "urn=schemas-microsoft-com:office:excel"),
                                                          new XElement("Print", new XElement("ValidPrinterInfo", ""),
                                                                                                        new XElement("HorizontalResolution", "600"),
                                                                                                        new XElement("VerticalResolution", "600")),
                                                                                 new XElement("Selected", ""),
                                                                                 new XElement("Panes", new XElement("Pane", new XElement("Number", "3"),
                                                                                                                            new XElement("ActiveRow", "5"),
                                                                                                                            new XElement("ActiveCol", "1"))),
                                                                                 new XElement("ProtectObjects", "False"), new XElement("ProtectScenarios", "False")
                                               )
                                )
                  )
                );

                nombreFichero = DateTime.Now.ToString("yyyyMMddHHmmss") + "_prueba.xls";
                ruta = @"C:\" + nombreFichero;
                xdoc.Save(ruta);

                FileInfo file = new FileInfo(ruta);
                if (file.Exists)
                {
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.ClearContent();
                    Response.AddHeader("content-disposition", "attachment; filename=" + nombreFichero);
                    Response.AddHeader("Content-Type", "application/Excel");
                    //Response.ContentType = "application/vnd.xls";
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.WriteFile(file.FullName);
                    Response.End();
                }
                else
                {
                    Response.Write("This file does not exist.");
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

    public class Movimiento
    {
        private int anho;
        private string project;
        private string type;
        private string subtype;
        private double ppto;
        private double amount;

        public int Anho
        { get { return anho; } set { anho = value; } }
        public string  Project
        { get { return project; } set { project = value; } }
        public string Type
        { get { return type; } set { type = value; } }
        public int Subtype
        { get { return subtype; } set { subtype = value; } }
        public double Ppto
        { get { return ppto; } set { ppto=value; } }
        public double Amount
        { get { return amount; } set {  amount=value; } }

    }
}