private void SetEstiloExcel() { //estilo de cabecera SLStyle rowHeaderStyle = new SLStyle(); rowHeaderStyle.SetBottomBorder(DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Thick, Color.Crimson); rowHeaderStyle.SetFont("Century Gothic", 12); slMusifan.SetRowStyle(1, rowHeaderStyle); //estilo de filas SLStyle allRowStyle = new SLStyle(); allRowStyle.SetBottomBorder(DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Medium, Color.DarkGray); slMusifan.SetRowStyle(2, Comercio.MisProductos.Count, allRowStyle); //estilo de columnas SLStyle columnStyle = new SLStyle(); columnStyle.SetLeftBorder(DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Medium, Color.Black); slMusifan.SetColumnStyle(1, 13, columnStyle); //expandir columnas automaticamente en base a las celdas slMusifan.AutoFitColumn(1, 13); //fijo la primer fila slMusifan.FreezePanes(1, 0); }
private SLStyle colunaStyleAux() { SLStyle style = new SLStyle(); style.SetFont("Microsoft Sans Serif", 10); style.SetHorizontalAlignment(HorizontalAlignmentValues.Left); return(style); }
private SLStyle cabecalhoStyleAux() { SLStyle sr = new SLStyle(); sr.SetFontBold(true); sr.SetFont("Microsoft Sans Serif", 10); sr.SetHorizontalAlignment(HorizontalAlignmentValues.Center); return(sr); }
static void makeStickers(List <Unit> units, string spName) { using (SLDocument doc = new SLDocument()) { var style_12 = new SLStyle(); style_12.SetFont("arial", 12); style_12.SetHorizontalAlignment(DocumentFormat.OpenXml.Spreadsheet.HorizontalAlignmentValues.Center); style_12.SetVerticalAlignment(DocumentFormat.OpenXml.Spreadsheet.VerticalAlignmentValues.Center); SLPageSettings ps = new SLPageSettings(); ps.TopMargin = 0; ps.BottomMargin = 0; ps.LeftMargin = 0; ps.RightMargin = 0; doc.SetPageSettings(ps); int col = 1; int row = 1; foreach (Unit unit in units) { for (int qty = 0; qty < unit.qty; qty++) { if (col == 5) { col = 1; row++; } doc.SetColumnWidth(col, COLUMN_WIDTH_FOR_ARIAL_14); doc.SetRowHeight(row, ROW_HEIGHT_FOR_ARIAL_14); doc.SetCellStyle(row, col, style_12); doc.SetCellValue(row, col++, unit.GetSticker()); } } using (var dlg = new SaveFileDialog()) { dlg.Title = "Сохранение файла наклеек"; dlg.FileName = "Наклейки -- " + spName.Split('\\').Last(); var res = dlg.ShowDialog(); if (res != DialogResult.OK) { return; } doc.SaveAs(dlg.FileName); Console.WriteLine("Сохранено в " + dlg.FileName); } } }
/// ----------------------------------------------------------------------------------------------- /// <summary> /// This setting creator has user-defined styles and columns for the four data-tables in /// mock data; Directors, Managers, Team Leads & Associates. /// Displays a variety of ways to access the Export library with Constructor and /// Property Dependency Injection. /// </summary> /// <returns>Settings: Custom Styling</returns> /// ----------------------------------------------------------------------------------------------- public static Settings SetupCustomStyles() { try { var childList = new List <ChildSetting>(); /* ------------------------------------------------------------- * Setup the column header base style for the child datasets * -----------------------------------------------------------*/ var baseColumnHeaderStyle = new SLStyle(); baseColumnHeaderStyle.SetHorizontalAlignment(HorizontalAlignmentValues.Center); baseColumnHeaderStyle.SetVerticalAlignment(VerticalAlignmentValues.Center); baseColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.DimGray, Color.White); baseColumnHeaderStyle.SetBottomBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.SetTopBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.SetVerticalBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.Border.SetRightBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.Border.SetLeftBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.SetFont("Britannic Bold", 12); baseColumnHeaderStyle.SetFontColor(Color.White); baseColumnHeaderStyle.SetFontBold(true); /* ------------------------------------------------------------- * Setup the odd row style for the child datasets * -----------------------------------------------------------*/ var oddRowStyle = new SLStyle(); oddRowStyle.SetHorizontalAlignment(HorizontalAlignmentValues.Left); oddRowStyle.SetVerticalAlignment(VerticalAlignmentValues.Center); oddRowStyle.Fill.SetPattern(PatternValues.Solid, Color.White, Color.Black); oddRowStyle.SetFont("Helvetica", 10); oddRowStyle.SetFontColor(Color.Black); /* ------------------------------------------------------------- * Setup the even row style derived from the odd, * change only what is necessary. * -----------------------------------------------------------*/ var evenRowStyle = oddRowStyle.Clone(); evenRowStyle.Fill.SetPattern(PatternValues.Solid, Color.WhiteSmoke, Color.Black); /* ------------------------------------------------------------ * Create the user-defined columns with property dependency * injection for the base dataset. * With this method hover the cursor over the property and * intellisense will show the comments for it. * ----------------------------------------------------------*/ var columns = new List <Column> { // Since this id column is not set to visible, you can just leave it out and it will be ignored new Column { BoundColumnName = "DID", UserDefinedColumnName = "ID", NumberFormat = NumberFormats.General, HorizontalAlignment = HorizontalAlignmentValues.Center, ShowField = false, FieldOrder = 0 }, new Column { BoundColumnName = "SheetName", UserDefinedColumnName = "Director", NumberFormat = NumberFormats.General, HorizontalAlignment = HorizontalAlignmentValues.Left, ShowField = true, FieldOrder = 1 }, new Column { BoundColumnName = "Age", UserDefinedColumnName = "Chronology", NumberFormat = NumberFormats.Decimal0, HorizontalAlignment = HorizontalAlignmentValues.Center, ShowField = true, FieldOrder = 2 }, new Column { BoundColumnName = "Income", UserDefinedColumnName = "Compensation", NumberFormat = NumberFormats.Accounting2Red, HorizontalAlignment = HorizontalAlignmentValues.Right, ShowField = true, FieldOrder = 3 }, new Column { BoundColumnName = "Member", UserDefinedColumnName = "Member ?", NumberFormat = NumberFormats.General, HorizontalAlignment = HorizontalAlignmentValues.Center, ShowField = true, FieldOrder = 4 }, new Column { BoundColumnName = "Registered", UserDefinedColumnName = "Date Registered", NumberFormat = NumberFormats.DateShort5, HorizontalAlignment = HorizontalAlignmentValues.Center, ShowField = true, FieldOrder = 5 } }; /* ------------------------------------------------------------- * Define and style base child settings. * This Child will always be present, it represents the * primary dataset for every export and is not really a child. * Using Property Injection Technique * -----------------------------------------------------------*/ childList.Add(new ChildSetting { // Optional name SheetName = "Directors", // Set column visibility ShowColumnHeader = true, // Make the base column header row a little larger // so it will stand out. Value is in pixels ColumnHeaderRowHeight = 25, // Setup the style for Column Headers ColumnHeaderStyle = baseColumnHeaderStyle, // Row and Alternating Row Styles // If set to false then the odd row style will be overall row style ShowAlternatingRows = false, // Setup the style for all rows OddRowStyle = oddRowStyle, EvenRowStyle = null, // Add the user-defined columns UserDefinedColumns = columns }); /* ------------------------------------------------------------ * The first child column headers stylings will be derived * from the base, change only what needs to be changed. * ----------------------------------------------------------*/ var firstColumnHeaderStyle = baseColumnHeaderStyle.Clone(); firstColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.DarkGray, Color.Black); firstColumnHeaderStyle.SetBottomBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.SetTopBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.SetVerticalBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.Border.SetRightBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.Border.SetLeftBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.SetFont("Helvetica", 10); firstColumnHeaderStyle.SetFontColor(Color.Black); /* ------------------------------------------------------------ * Create the user-defined columns with constructor dependency * injection for the base dataset. * Hover the cursor over the property and intellisense will * show the comments for it. * ----------------------------------------------------------*/ columns = new List <Column> { new Column ( "SheetName", "Managers", NumberFormats.General, HorizontalAlignmentValues.Left, true, 1 ), new Column ( "Age", "Age", NumberFormats.Decimal0, HorizontalAlignmentValues.Center, true, 2 ), new Column ( "Income", "Compensation", NumberFormats.Currency0Black, HorizontalAlignmentValues.Right, true, 3 ), new Column ( "Registered", "Date Registered", NumberFormats.DateShort1, HorizontalAlignmentValues.Center, true, 5 ) }; /* ------------------------------------------------------------- * Define and add the first child * Using Constructor dependency injection * -----------------------------------------------------------*/ childList.Add(new ChildSetting ( "Managers", // SheetName true, // Show Column Headers 1, // Column Offset to the Right null, // Column Header Row Height firstColumnHeaderStyle, // Column Header Style false, // Show Alternating Rows, false will default to Odd oddRowStyle, // Odd Row Style null, // Even Row Style columns // User-Defined Column (UDCs) )); /* ------------------------------------------------------------- * The second child column headers stylings will be derived * from the first, change only what needs to be changed. * -----------------------------------------------------------*/ var secondColumnHeaderStyle = firstColumnHeaderStyle.Clone(); secondColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.CadetBlue, Color.White); secondColumnHeaderStyle.SetFontColor(Color.White); /* ------------------------------------------------------------- * Define and add the second child * Using Constructor dependency injection * -----------------------------------------------------------*/ childList.Add(new ChildSetting( "Team Leads", // SheetName true, // Show Column Headers 2, // Column Offset to the Right null, // Column Header Row Height secondColumnHeaderStyle, // Column Header Style false, // Show Alternating Rows, false will default to Odd oddRowStyle, // Odd Row Style null, // Even Row Style new List <Column> // User-Defined Column (UDCs) { new Column("TLID", "Team Lead ID", NumberFormats.General, HorizontalAlignmentValues.Left, true, 6), new Column("Registered", "Registration Date", NumberFormats.UserDefined, HorizontalAlignmentValues.Center, true, 2, "d-mmm-yy"), new Column("SheetName", "Team Leads", NumberFormats.General, HorizontalAlignmentValues.Left, true, 0), new Column("Age", "How Old?", NumberFormats.General, HorizontalAlignmentValues.Center, true, 1), new Column("Member", "Member?", NumberFormats.General, HorizontalAlignmentValues.Center, true, 3), new Column("Income", "Income", NumberFormats.Accounting2Red, HorizontalAlignmentValues.Right, true, 4), new Column("MID", "Foreign Key", NumberFormats.General, HorizontalAlignmentValues.Right, false) } )); /* ------------------------------------------------------------- * The third child column headers stylings will be derived * from the first, change only what needs to be changed. * -----------------------------------------------------------*/ var thirdColumnHeaderStyle = firstColumnHeaderStyle.Clone(); thirdColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.Aqua, Color.Black); thirdColumnHeaderStyle.SetFont("Blackadder ITC", 11); thirdColumnHeaderStyle.SetFontColor(Color.Black); /* ------------------------------------------------------------- * Define and add the third child * Constructor Injection on all * -----------------------------------------------------------*/ childList.Add(new ChildSetting("Associates", true, 3, 30, thirdColumnHeaderStyle, true, oddRowStyle, evenRowStyle, new List <Column> { new Column("Registered", "Date", NumberFormats.TimeStamp124, HorizontalAlignmentValues.Left, true, 3), new Column("Member", "Member?", NumberFormats.General, HorizontalAlignmentValues.Center, true, 2), new Column("SheetName", "Associate", NumberFormats.General, HorizontalAlignmentValues.Left, true, 0) } )); /* ------------------------------------------------------------- * Setup and return the primary container for the child datasets * Using Constructor Injection as well * -----------------------------------------------------------*/ return(new Settings("Organization", childList)); } catch (Exception ex) { Assert.Fail("Exception Fail: " + ex.Message); } return(null); }
/// ----------------------------------------------------------------------------------------------- /// <summary> /// Sets up the default styling when user does not predefine styles with a Settings configuration /// </summary> /// <returns>Settings: Default Styling</returns> /// ----------------------------------------------------------------------------------------------- public static Settings SetupDefaultStyles() { try { /* ------------------------------------------------------------- * Setup primary container for the child datasets * -----------------------------------------------------------*/ var settings = new Settings { // Optional name Name = "Default Settings Container" }; /* ------------------------------------------------------------- * Setup the column header base style for the child datasets * -----------------------------------------------------------*/ var baseColumnHeaderStyle = new SLStyle(); baseColumnHeaderStyle.SetHorizontalAlignment(HorizontalAlignmentValues.Center); baseColumnHeaderStyle.SetVerticalAlignment(VerticalAlignmentValues.Center); baseColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.DimGray, Color.White); baseColumnHeaderStyle.SetBottomBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.SetTopBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.SetVerticalBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.Border.SetRightBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.Border.SetLeftBorder(BorderStyleValues.Medium, Color.Black); baseColumnHeaderStyle.SetFont("Helvetica", 11); baseColumnHeaderStyle.SetFontColor(Color.White); baseColumnHeaderStyle.SetFontBold(true); /* ------------------------------------------------------------- * Setup the odd row style for the child datasets * -----------------------------------------------------------*/ var oddRowStyle = new SLStyle(); oddRowStyle.SetHorizontalAlignment(HorizontalAlignmentValues.Left); oddRowStyle.SetVerticalAlignment(VerticalAlignmentValues.Center); oddRowStyle.Fill.SetPattern(PatternValues.Solid, Color.White, Color.Black); oddRowStyle.SetFont("Helvetica", 10); oddRowStyle.SetFontColor(Color.Black); /* ------------------------------------------------------------- * Setup the even row style derived from the odd, * change only what is necessary. * -----------------------------------------------------------*/ var evenRowStyle = oddRowStyle.Clone(); evenRowStyle.Fill.SetPattern(PatternValues.Solid, Color.WhiteSmoke, Color.Black); /* ------------------------------------------------------------- * Define and style base child settings. * This Child will always be present, it represents the * primary dataset for every export. * -----------------------------------------------------------*/ settings.ChildSettings.Add(new ChildSetting ( // SheetName (Optional) "Default Base Child Settings", // Set Overall Column Visibility true, // Column offset to the right 0, // Make the base column header row a little larger // so it will stand out. Value is in pixels 25, // Setup the style for Column Headers baseColumnHeaderStyle, // Row and Alternating Row Styles // If set to false then the odd row style will be overall row style true, // Setup the style for odd & even rows oddRowStyle, evenRowStyle, // No User-Defined column headers null )); /* ------------------------------------------------------------ * The first child column headers stylings will be derived * from the base, change only what needs to be changed. * ----------------------------------------------------------*/ var firstColumnHeaderStyle = baseColumnHeaderStyle.Clone(); firstColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.DarkGray, Color.Black); firstColumnHeaderStyle.SetBottomBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.SetTopBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.SetVerticalBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.Border.SetRightBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.Border.SetLeftBorder(BorderStyleValues.Thin, Color.DarkSlateGray); firstColumnHeaderStyle.SetFont("Helvetica", 10); firstColumnHeaderStyle.SetFontColor(Color.Black); /* ------------------------------------------------------------- * Define and add the stylings for the first child, which is * a child of the base data-set * -----------------------------------------------------------*/ settings.ChildSettings.Add(new ChildSetting ( "Default First Child Settings", true, null, null, firstColumnHeaderStyle, true, oddRowStyle, evenRowStyle, null )); /* ------------------------------------------------------------- * The second child column headers stylings will be derived * from the first, change only what needs to be changed. * -----------------------------------------------------------*/ var secondColumnHeaderStyle = firstColumnHeaderStyle.Clone(); secondColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.CadetBlue, Color.White); secondColumnHeaderStyle.SetFontColor(Color.White); /* ------------------------------------------------------------- * Define and add the stylings for the second child, which is * a child of the first data-set * -----------------------------------------------------------*/ settings.ChildSettings.Add(new ChildSetting ( "Default Second Child Settings", true, null, null, secondColumnHeaderStyle, true, oddRowStyle, evenRowStyle, null )); /* ------------------------------------------------------------- * The third child column headers stylings will be derived * from the first, change only what needs to be changed. * -----------------------------------------------------------*/ var thirdColumnHeaderStyle = firstColumnHeaderStyle.Clone(); thirdColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.Aqua, Color.Black); thirdColumnHeaderStyle.SetFontColor(Color.Black); /* ------------------------------------------------------------- * Define and add the stylings for the third child, which is * a child of the second data-set * -----------------------------------------------------------*/ settings.ChildSettings.Add(new ChildSetting ( "Default Third Child Settings", true, null, null, thirdColumnHeaderStyle, true, oddRowStyle, evenRowStyle, null )); /* ------------------------------------------------------------- * The forth child column headers stylings will be derived * from the first, change only what needs to be changed. * -----------------------------------------------------------*/ var fourthColumnHeaderStyle = firstColumnHeaderStyle.Clone(); fourthColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.Chartreuse, Color.Black); fourthColumnHeaderStyle.SetFontColor(Color.Black); /* ------------------------------------------------------------- * Define and add the stylings for the fourth child, which is * a child of the third data-set * -----------------------------------------------------------*/ settings.ChildSettings.Add(new ChildSetting ( "Default Fourth Child Settings", true, null, null, fourthColumnHeaderStyle, true, oddRowStyle, evenRowStyle, null )); /* ------------------------------------------------------------- * If five deep isn't enough let's add a sixth one. * The fifth child column headers stylings will be derived * from the first, change only what needs to be changed. * -----------------------------------------------------------*/ var fifthColumnHeaderStyle = firstColumnHeaderStyle.Clone(); fifthColumnHeaderStyle.Fill.SetPattern(PatternValues.Solid, Color.BlueViolet, Color.Black); fifthColumnHeaderStyle.SetFontColor(Color.White); /* ------------------------------------------------------------- * Define and add the stylings for the fifth child, which is * a child of the fourth data-set * -----------------------------------------------------------*/ settings.ChildSettings.Add(new ChildSetting ( "Default Fifth Child Settings", true, null, null, fifthColumnHeaderStyle, true, oddRowStyle, evenRowStyle, null )); return(settings); } catch (Exception ex) { Log.Error("SpreadsheetLightWrapper.Export.DefaultStyles.SetupDefaultStyles -> " + ex.Message + ": " + ex); } return(null); }
static void Main(string[] args) { int i, j; //double fValue; Random rand = new Random(); double[] doubledata = new double[] { rand.Next(10), rand.NextDouble(), 2.3, 4.5, 6.9 }; using (SLDocument sl = new SLDocument()) { sl.RenameWorksheet(SLDocument.DefaultFirstSheetName, "Random"); for (i = 1; i <= 16; ++i) { for (j = 1; j <= 6; ++j) { switch (rand.Next(5)) { case 0: case 1: sl.SetCellValue(i, j, doubledata[rand.Next(doubledata.Length)]); break; case 2: case 3: sl.SetCellValue(i, j, rand.NextDouble() * 1000.0 + 350.0); break; case 4: if (rand.NextDouble() < 0.5) { sl.SetCellValueNumeric(i, j, "3.1415926535898"); } else { sl.SetCellValueNumeric(i, j, "2.7182818284590"); } break; } } } //sl.Filter("A1", "F1"); //sl.FlattenAllSharedCellFormula(); //sl.DrawBorderGrid //sl.SetCellValue("C6", "This is at C6!"); //sl.SetCellValue(SLConvert.ToCellReference(7, 4), string.Format("=SUM({0})", SLConvert.ToCellRange(2, 1, 2, 20))); //sl.SetCellValue("C8", new DateTime(3141, 5, 9)); //SLStyle style = sl.CreateStyle(); //style.FormatCode = "d-mmm-yyyy"; //sl.SetCellStyle("C8", style); //for (int i = 1; i <= 20; ++i) sl.SetCellValue(2, i, i); sl.SaveAs("Miscellaneous1.xlsx"); SLDocument tl = new SLDocument("Miscellaneous1.xlsx", "Sheet"); //以下两个方法功能一致 tl.SetCellValue("G1", "=SUM(A1:F2)"); tl.SetCellValue(SLConvert.ToCellReference(2, 7), string.Format("=SUM({0})", SLConvert.ToCellRange(1, 1, 2, 6))); tl.SetCellValue("G3", "=A2-A3"); //tl.SetCellValueNumeric("G6", "=AVERAGE(A1:F2)"); //tl.SetCellValue("G2", string.Format(" =SUM({0})", SLConvert.ToCellRange(1, 1, 2, 6))); //以下两个方法功能一致 tl.SetCellValue("G4", StringValue.ToString("=AVERAGE(A1:F2)")); tl.SetCellValue("G5", String.Format("=AVERAGE({0})", SLConvert.ToCellRange(1, 1, 2, 6))); tl.SetCellValue("G6", "So this is the random number table"); tl.SetCellValue("G7", "The time is "); tl.SetCellValue("H7", DateTime.Now.ToString()); //获取当前日期和时间 tl.AddWorksheet("Secret"); //添加工作表 tl.SelectWorksheet("Random"); //选择Random工作表 tl.RenameWorksheet("Secret", "again"); //重命名Secret SLStyle style = tl.CreateStyle(); //设置单元格格式 style.SetFont("Impact", 24); style.Font.Underline = UnderlineValues.Single; tl.SetCellStyle(1, 7, style); tl.SetCellStyle("G6", style); //设置EXCEL属性 tl.DocumentProperties.Creator = "ZhouL"; tl.DocumentProperties.ContentStatus = "Secret"; tl.DocumentProperties.Title = "Random number table"; tl.DocumentProperties.Description = "Get data and manipulate it and export it"; tl.SaveAs("MiscellaneousModified.xlsx"); } Console.WriteLine("End of program"); Console.ReadLine(); }