MyXls export Excel's various settings

Excel is a MyXls open source library operation, set the font support, column widths, row heights (implemented by BOSSMA), merged cell borders, background colors, data type, wrap, alignment and the like.
This article describes how to set the style attributes MyXls by various detail by way of example, and attached to the source code for the sample program.


// prepare Test Data
List & lt; PersonInfo & gt; List = new new List & lt; PersonInfo & gt; ();
for (int I =. 1; I & lt; = 200 is; I ++)
{
    PersonInfo Person = new new PersonInfo ()
    {
        RealName of = "Zhang" + i ,
        Gender = (i% 2 == 0 "male"?: "female"),
        Age = 20 + (i% 3)
    };

    list.add (the Person);
}

int recordCount = 200; // to export record number of
int maxRecordCount = 100; // table for each sheet maximum number of records
int sheetCount = 1; // sheet table number

XlsDocument xls = new XlsDocument ();
xls.FileName = "MyXls-" + DateTime.Now.ToString ( "yyyyMMddHHmmss") + ".xls";

the number of sheet to be displayed calculation data table @
IF (recordCount & gt; MaxRecordCount)
{
    SheetCount = (int) the Math. Ceiling ((decimal) recordCount / (decimal) MaxRecordCount);
}

// style Sheet title
XF titleXF = xls.NewXF (); // generates a XF examples xls, XF is the cell formatting objects
titleXF.HorizontalAlignment = HorizontalAlignments.Centered ; // set the text centered
titleXF.VerticalAlignment = VerticalAlignments.Centered; // vertically centered
titleXF.UseBorder = true; // use the border 
titleXF.TopLineStyle = 1; // the border style
titleXF.TopLineColor = Colors.Black; // the border color
titleXF.LeftLineStyle = 1; // left border style
titleXF.LeftLineColor = Colors.Black; // left border color
titleXF.RightLineStyle = 1; // right border style
titleXF.RightLineColor = Colors.Black; // right border color
titleXF.Font.FontName = "Times New Roman"; // font
titleXF.Font.Bold = true; // whether to raise Chu
titleXF.Font.Height = 12 * 20; // word size (the size in units of 1/20 point)

// column header style
XF columnTitleXF = xls.NewXF (); // generates a XF examples xls, XF is the cell format object is
columnTitleXF.HorizontalAlignment = HorizontalAlignments.Centered; // set the text centered
columnTitleXF.VerticalAlignment = VerticalAlignments.Centered; // vertically centered
columnTitleXF.UseBorder = true; // use the border 
columnTitleXF.TopLineStyle = 1; // the border style
columnTitleXF.TopLineColor = Colors.Black; // color border on
columnTitleXF.BottomLineStyle = 1; // the border style
columnTitleXF.BottomLineColor = Colors.Black; // the border color
columnTitleXF.LeftLineStyle = 1; // left border style
columnTitleXF.LeftLineColor = Colors.Black; // left border color
columnTitleXF.Pattern = 1; // cell fill style. If set to 0, the filler is a solid color (colorless), a representative of a solid color without a gap 
columnTitleXF.PatternBackgroundColor = Colors.Red; // filled background 
columnTitleXF.PatternColor = Colors.Default2F; // fill the background color

/ / style data cells
XF dataXF = xls.NewXF (); // a xls XF generated for example, XF is the cell formatting objects
dataXF.HorizontalAlignment = HorizontalAlignments.Centered; // set the text centered
dataXF.VerticalAlignment = VerticalAlignments.Centered ; // vertical center
dataXF.UseBorder = true; // use the border 
dataXF.LeftLineStyle = 1; // left border style
dataXF.LeftLineColor = Colors.Black; // left border color
dataXF.BottomLineStyle = 1; // the border style
dataXF.BottomLineColor = Colors.Black; // bottom border color
dataXF.Font.FontName = "Times New Roman";
dataXF.Font.Height = 9 * 20; // set word size (font size is 1/20 point units a)
dataXF.UseProtection = false; // default is protected, you need to enable the export editor can edit
dataXF.TextWrapRight = true; // wrap

// traverse create Sheet
for (int i = 1; i & lt; SheetCount =; I ++)
{
    // Sheet calculated according to the number, one creates a
    // row and column is set to the specified Sheet required, and the object can not be reused for each set (as may be provided or the start and end lines column, it is not much need to reuse, this should be a policy issue)
    Worksheet sheet;
    IF (SheetCount == 1)
    {
        sheet = xls.Workbook.Worksheets.Add ( "personnel information table");
    }
    the else
    {
        sheet = xls.Workbook.Worksheets.Add ( "personnel information table -" + I);
    }

    // Set the number row
    ColumnInfo col0 = new ColumnInfo (xls, sheet); // column objects 
    col0.ColumnIndexStart = 0; // start as the first column, the index starts from 0
    col0.ColumnIndexEnd = 0; // end columns for the first column, the index starts from 0
    col0.Width = 8 * 256; // column width measuring unit 1/256 character wide
    sheet.AddColumnInfo (col0); // attach the sheet to the page format

    // name column provided
    ColumnInfo col1 = new ColumnInfo (xls, sheet); // column objects 
    col1.ColumnIndexStart = 1; // start as the first two, the index starts from 0
    col1.ColumnIndexEnd = 1; // as the first termination 2 index starting from 0
    col1.Width = 16 * 256; // column width measuring unit 1/256 character wide
    sheet.AddColumnInfo (col1); // attach the sheet to the page format

    // gender column provided
    ColumnInfo col2 = new ColumnInfo (xls, sheet); // column objects 
    col2.ColumnIndexStart = 2; // column 3 as the start, the start index from 0
    col2.ColumnIndexEnd = 2; // terminated as the first three, the index starts from 0
    col2.Width = 16 * 256; // column width measuring unit 1/256 character wide
    sheet.AddColumnInfo (col2); // put attached to the sheet format page

    // set the age column
    ColumnInfo col3 = new ColumnInfo (xls, sheet); // column objects 
    col3.ColumnIndexStart = 3; // start as the first four, the index starts from 0
    col3.ColumnIndexEnd = 3; // terminated as the first four, the index starts from 0
    col3.Width = 16 * 256; // column width measuring unit 1/256 character wide
    sheet.AddColumnInfo (col3); // attach the sheet to form on page

    // line disposed
    rowInfo rol1 = new rowInfo (); // row object
    rol1.RowHeight = 16 * 20; // the line height
    rol1.RowIndexStart = 3; // set row starting column index starts from 1
    rol1. RowIndexEnd = (ushort) (maxRecordCount + 2); // set the row end column
    sheet.AddRowInfo (rol1); // set the page attached to the sheet

    // merge cells
    //sheet.Cells.Merge(1, 1, 1,. 4);
    the MergeArea titleArea the MergeArea new new = (1, 1, 1,. 4); // example a merged cell (1 combined first row, column 1 to row 1, column. 4) 
    sheet.AddMergeArea (titleArea); // filling merged cell 

    // begin populating data into cells
    cells = sheet.Cells cells;

    // Sheet header row, the rows and columns the indexes are from a starting
    cell cell = cells.Add (1, 1 , " personnel information tables", titleXF);
    cells.Add (1, 2, "", titleXF); // the merged cell is still to set each cell, only valid style
    cells.Add (1, 3, "" , titleXF); // still need to set each cell of the combined cell style valid
    cells.Add (1, 4, " ", titleXF); // still need to set each cell, the combined pattern is valid cell
    sheet.Rows [1] .RowHeight = 40 * 20; // the specified row set the row height

    // column header row
    cells .Add (2, 1, "serial number", columnTitleXF);
    cells.Add (2, 2, 'name',columnTitleXF);
    cells.Add (2, 3, "gender", columnTitleXF);

    // rightmost column of the right border need, by modifying the style columnTitleXF manner, may also be implemented by setting the cell properties manner.
    =. 1 columnTitleXF.RightLineStyle;
    columnTitleXF.RightLineColor = Colors.Black;
    cells.Add (2,. 4, "Age", columnTitleXF);

    sheet.Rows [2] = 18 is * 20 is .RowHeight; // set the row designated row high

    // row index
    int rowIndex =. 3;

    for (int J = 0; J & lt; MaxRecordCount; J ++)
    {
        // index of the current record in the data set
        int K = (I -. 1) + J * MaxRecordCount;

        // If the maximum number of the recording sheet reaches out
        IF (K & gt; = recordCount)
        {
            BREAK;
        }

        // set the value of the cell
        cells.Add (rowIndex, 1, k + 1, dataXF);
        cells.Add (rowIndex, 2, List [K] .RealName, dataXF);
        cells.Add (rowIndex,. 3, List [K] .Gender, dataXF);

        // needed right border rightmost column, by giving Cell mode setting properties achieved, because not all cells need to set, not by modifying the style dataXF way
        the cell lastCell = cells.Add (rowIndex,. 4, List [K] .Age, dataXF);
        lastCell.RightLineStyle. 1 = ;
        lastCell.RightLineColor = Colors.Black;

        // line number increment
        rowIndex ++;
    }
}

// output Excel file in a browser
xls.Send ();

Guess you like

Origin www.cnblogs.com/Fooo/p/11613143.html