zoukankan      html  css  js  c++  java
  • MyXls初级教程

    这些天使用MyXls导出Excel报表(因为Apose.Cells要收费)。感觉MyXls虽然功能远没有Cells强大,但是胜在开源、免费而且性能稳定可靠。用作出一般情况下的报表。足矣!

    记下几个初级使用方法,希望能够给初入门的人一点帮助:

    1.创建一个Excel文档:

    Code
    XlsDocument xls = new XlsDocument();

     

    2.创建一个WorkSheet:

    Code
    Worksheet ws = xls.Workbook.Worksheets.Add("WorkSheet1");

     

    3.指定列格式:

    Code
    ColumnInfo colInfo = new ColumnInfo(xls, ws);
    colInfo.ColumnIndexStart 
    = 0;
    colInfo.ColumnIndexEnd 
    = 17;
    colInfo.Width 
    = 15 * 256;
    ws.AddColumnInfo(colInfo);

     

    列格式必须每次都要重新定义,一个列格式不能重复使用。

     

    4.指定单元格样式:

    复制代码
    Code
    XF xf = xls.NewXF();
    xf.HorizontalAlignment 
    = HorizontalAlignments.Centered;
    xf.VerticalAlignment 
    = VerticalAlignments.Centered;
    xf.Pattern 
    = 1;
    xf.PatternColor 
    = Colors.Default30;
    xf.UseBorder 
    = true;
    xf.TopLineStyle 
    = 1;
    xf.TopLineColor 
    = Colors.Black;
    xf.BottomLineStyle 
    = 1;
    xf.BottomLineColor 
    = Colors.Black;
    xf.LeftLineStyle 
    = 1;
    xf.LeftLineColor 
    = Colors.Black;
    xf.RightLineStyle 
    = 1;
    xf.RightLineColor 
    = Colors.Black;
    xf.Font.Bold 
    = true;
    xf.Font.Height 
    = 11 * 20;
    xf.Font.ColorIndex 
    = 1;
    复制代码

     

    5.给单元格赋值:

    Code
    ws.Cells.Add(23"金额(万元)", xf);

     

    6.合并单元格:

    Code
    ws.Cells.Merge(1222);
    //或者
    ws.AddMergeArea(new MergeArea(1211));

     

    7.MyXls合并单元格有个bug,就是合并后只是第一个单元格有样式,其余的样式丢失。所以写了个函数来合并:

    复制代码
    Code
    MergeRegion(ref ws, xf, "机构"1121);

    public void MergeRegion(ref Worksheet ws, XF xf, string title, int startRow, int startCol, int endRow, int endCol)
    {
          
    for (int i = startCol; i <= endCol; i++)
          {
                
    for (int j = startRow; j <= endRow; j++)
                {
                    ws.Cells.Add(j, i, title, xf);
                }
          }
          ws.Cells.Merge(startRow, endRow, startCol, endCol);
    }
    复制代码

     

    虽然效率不怎么样,但是对于出Excel报表,还OK。

    8.指定单元格格式:

    Code
    cell.Format = StandardFormats.Decimal_1;

     

    具体更多请参考源代码的StandardFormats类。

     

    9.保存或者发送Excel:

    Code
    xls.Send();
    //或者
    xls.Save();
  • 相关阅读:
    Windows 8的语音识别
    硬件驱动程序的知识点滴
    怎么将一张100KB以上大小的电子图片压缩成30KB以内
    Hadoop概念学习系列之Hadoop新手学习指导之入门需知(二十)
    Hadoop概念学习系列之Hadoop、Spark学习路线(很值得推荐)(十八)
    研究生,别再玩了,你玩不起!
    redis源代码解读之内存管理————zmalloc文件
    线段树之入门篇
    C#托付和事件
    dlmalloc 2.8.6 源代码具体解释(6)
  • 原文地址:https://www.cnblogs.com/ljl_falcon/p/2546149.html
Copyright © 2011-2022 走看看