2013-12-11 47 views
0

我正在嘗試使用此格式獲取xls文件,總計爲紅色行。LINQ to XML分組。創建xls小計

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

我如何獲得總計節點分組的項目,類型和子類型?

問候

+0

我不太明白你搭售做什麼。你是從XML或其他方式創建一個xls文件? 'lista'在你的代碼示例中來自哪裏?也許你可以多給你一些你已經嘗試過的代碼以及XML的樣子。 – CoderDennis

回答

0

我在這裏張貼的細節我的XML代碼。在代碼中你可以看到lista來自哪裏。 目標是準備用Excel打開的XML文件。該代碼運行良好,但I'll需要添加分類彙總(紅色線的圖像) 問候,瑪麗亞

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

    } 
} 
+0

這應該是作爲編輯添加到問題而不是張貼爲答案 – CoderDennis