- Code: Select all
private Workbook AsWorkbook(PhocasMaster master, Database database, bool monochrome)
{
SpireLicense();
List<string> formatting;
var dataTable = AsDataTable(master, database, out formatting);
if (dataTable.Columns["Code"] != null)
dataTable.Columns["Code"].ColumnName = database.Synonyms["Code"];
var workbook = new Workbook();
var worksheet = workbook.Worksheets[0];
worksheet.InsertDataTable(dataTable, true, 1, 1, true);
var headerRows = 1;
// add total row if required
if (!IsCompany() && !IsTransaction() && View.Mode != "Search")
{
var totalData = AsTotalData(master, database);
headerRows++;
worksheet.InsertRow(headerRows);
worksheet.InsertDataTable(totalData, false, headerRows, 1, true);
}
// add pareto row if required
if (!IsCompany() && !IsTransaction() && View.Mode != "Search" && View.ShowPareto)
{
var paretoData = AsParetoData(master, database);
headerRows++;
worksheet.InsertRow(headerRows);
worksheet.InsertDataTable(paretoData, false, headerRows, 1, true);
}
// bold header rows
for (var i = 0; i < headerRows; i++)
worksheet.Rows[i].Style.Font.IsBold = true;
// number format
CellRange formatRange;
if (String.IsNullOrEmpty(View.Dimension))
formatRange = worksheet.Range[2, 1, worksheet.LastRow, worksheet.LastColumn];
else
formatRange = worksheet.Range[2, View.PropertyLists.Single(p => p.Dimension == View.Dimension).Properties.Count + 1, worksheet.LastRow, worksheet.LastColumn];
formatRange.NumberFormat = "0.00";
if (!monochrome)
{
// Conditional Formatting
CellRange range = worksheet.Range[headerRows + 1, 1, worksheet.LastRow, worksheet.LastColumn];
// Negative Values
var negCondFormat = range.ConditionalFormats.AddCondition();
negCondFormat.FormatType = ConditionalFormatType.CellValue;
negCondFormat.FirstFormula = "0";
negCondFormat.Operator = ComparisonOperatorType.Less;
negCondFormat.FontColor = System.Drawing.Color.Red;
// Zero Values
var zeroCondFormat = range.ConditionalFormats.AddCondition();
zeroCondFormat.FormatType = ConditionalFormatType.CellValue;
zeroCondFormat.FirstFormula = "0";
zeroCondFormat.Operator = ComparisonOperatorType.Equal;
zeroCondFormat.BackColor = System.Drawing.ColorTranslator.FromHtml("#ccffcc");
workbook.CalculateAllValue();
}
for (int i = 0; i < worksheet.Columns.Count(); i++)
{
// Columns not 0-indexed
worksheet.AutoFitColumn(i + 1);
}
return workbook;
}
public MemoryStream AsXLSX(PhocasMaster master, Database database, bool monochrome)
{
var memoryStream = new MemoryStream();
var workbook = AsWorkbook(master, database, monochrome);
workbook.Version = ExcelVersion.Version2010;
workbook.SaveToStream(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}