我在这里张贴的细节我的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; } }
}
}
我不太明白你搭售做什么。你是从XML或其他方式创建一个xls文件? 'lista'在你的代码示例中来自哪里?也许你可以多给你一些你已经尝试过的代码以及XML的样子。 – CoderDennis