B-Arts DocumentationIndice generale

BExcel - Esempi

Esportazione formattata da DataSet

using B.BOffice;
using System.Data;

DataTable orders = new DataTable("Orders");
orders.Columns.Add("OrderNumber", typeof(string));
orders.Columns.Add("Customer", typeof(string));
orders.Columns.Add("Total", typeof(double));
orders.Rows.Add("ORD-001", "Cliente S.p.A.", 1250.50m);

DataSet data = new DataSet();
data.Tables.Add(orders);

BExcel excel = new BExcel(data, TblName: new[] { "Ordini" });

excel.Columns.Add(new BExcelDataColumn("OrderNumber", "Orders", 90, "Numero"));
excel.Columns.Add(new BExcelDataColumn("Customer", "Orders", 180, "Cliente"));
excel.Columns.Add(new BExcelDataColumn(
  "Total", "Orders", 90, "Totale",
  BExcelDataColumn.eTipoDato.Number));

BExcelStyle headerStyle = new(BExcelStyle.eActions.Header, "Header")
{
  BackColor = "#1F4E78",
  HAlignment = BExcelStyle.eHorizontalAlignment.Center,
  FontStyle = new BExcelFontStyle("Calibri", size: 11, isBold: true)
  {
    Color = "#FFFFFF"
  }
};

BExcelStyle rowStyle = new(BExcelStyle.eActions.Row, "Data")
{
  BorderAll = new BExcelBorderStyle(
    BExcelBorderStyle.eBorder.Left,
    BExcelBorderStyle.eLineStyle.Continuous,
    w: 1,
    lineColor: "#D9E2F3")
};

excel.Styles.Add(headerStyle);
excel.Styles.Add(rowStyle);

bool saved = excel.SaveFile(outputXmlPath);

Questa modalità genera un foglio per ogni DataTable. Columns stabilisce ordine, alias, larghezza e tipo dei dati; in assenza di configurazione vengono usate tutte le colonne della tabella.

Regola di formattazione condizionale B-Arts

BExcelStyle highValue = new(BExcelStyle.eActions.Role, "HighValue", "Orders")
{
  OrderRoleIndex = 0,
  BackColor = "#FFF2CC",
  Role = new BExcelRole(BExcelRole.eDestination.Row)
  {
    CheckRole = row => BConvert.ToDouble(row?["Total"]) >= 1000d
  }
};

highValue.InheritsBy(rowStyle);
excel.Styles.Add(highValue);

Le regole Role vengono valutate in ordine di OrderRoleIndex; la prima che restituisce true determina lo stile. Una regola può riguardare l'intera riga oppure una colonna specifica.

Costruzione manuale disconnessa

BExcel excel = new BExcel();

BExcelSheet sheet = new BExcelSheet("Riepilogo");
sheet.Columns.Add(new BExcelDataColumn("Label", 180));
sheet.Columns.Add(new BExcelDataColumn("Value", 90));

BExcelRow title = new BExcelRow(Height: 28, StyleID: "Title");
title.Cells.Add(new BExcelCell("Riepilogo ordini") { MergeAcross = 1 });

BExcelRow values = new BExcelRow();
values.Cells.Add(new BExcelCell("Totale ordini"));
values.Cells.Add(new BExcelCell("1250,50", StyleID: "Currency"));

sheet.Rows.Add(title);
sheet.Rows.Add(values);
excel.Sheets.Add(sheet);

excel.Styles.Add(new BExcelStyle(BExcelStyle.eActions.Row, "Title")
{
  FontStyle = new BExcelFontStyle("Calibri", size: 14, isBold: true)
});

string spreadsheetXml = excel.GetXmlExcel();
byte[]? content = excel.GetByteArray();
BFile file = excel.GetBFile("Riepilogo.xml");

Il costruttore vuoto abilita ModeDisconneted e usa Sheets. In questa modalità celle, righe e fogli vengono assemblati manualmente.

Formato prodotto

BExcel produce SpreadsheetML 2003: un documento XML con namespace urn:schemas-microsoft-com:office:spreadsheet, apribile da Microsoft Excel. Non produce un pacchetto Office Open XML .xlsx, anche se il nome predefinito di GetBFile è BExcel.Xlsx. Usare preferibilmente un'estensione coerente come .xml quando il sistema destinatario la consente.

I valori vengono inseriti direttamente nell'XML: i dati contenenti &, < o > devono essere già compatibili con XML. Controllare ExceptionList quando l'origine è una connessione o un dataset con più result set.