FarPoint.Win.Spread 常规操作

2021-02-17 11:19

阅读:807

            FarPoint.Win.Spread.FpSpread fSpread = new FarPoint.Win.Spread.FpSpread();
           
//设置 行数、列数
            fSpread.ActiveSheet.Rows.Count = 90;
            fSpread.ActiveSheet.Columns.Count = 10;

            //获取行索引  列索引
            int rowIndex = fSpread.ActiveSheet.ActiveRowIndex;
            int colIndex = fSpread.ActiveSheet.ActiveColumnIndex;

            //追加行 追加列

            fSpread.ActiveSheet.Rows.Add(rowIndex,1);

            fSpread.ActiveSheet.Columns.Add(colIndex,1);

            //指定单元格获取焦点
            fSpread.ActiveSheet.SetActiveCell(rowIndex, colIndex);

            //隐藏列标题和序号
            fSpread.ActiveSheet.ColumnHeader.Visible = false;
            fSpread.ActiveSheet.RowHeaderVisible = false;
            fSpread.ActiveSheet.RowHeaderColumnCount = 0;
            fSpread.ActiveSheet.ColumnHeaderRowCount = 0;

            //不显示网格线            
            //方法一、属性:
            //GridShowHoriz = false;
            //GridShowHoriz = false;
            //方法二
            fSpread.ActiveSheet.HorizontalGridLine = new FarPoint.Win.Spread.GridLine(FarPoint.Win.Spread.GridLineType.None);
            fSpread.ActiveSheet.VerticalGridLine = new FarPoint.Win.Spread.GridLine(FarPoint.Win.Spread.GridLineType.None);

            //不可编辑
            fSpread.ActiveSheet.OperationMode = FarPoint.Win.Spread.OperationMode.ReadOnly;
            /*    
编辑模式
            0 - Normal               默认的表单操作都是可用的
 
            1 - ReadOnly            仅仅读的
 
            2 - RowMode            用户能够选择表单中的一行。而且能够通过双击来编辑该行任一单独的单元格
 
            3 - SingleSelect        用户仅能选择一行,而且不能编辑不论什么的单元格
 
            4 - MultiSelect         用户仅能选择多个连续行,可是不能编辑不论什么一个单元格
 
            5 - ExtendedSelect   用户仅能选择多个非连续行,可是不能编辑不论什么一个单元格*/

            //滚动栏(水平、垂直)  显示方式
            // Always      依据情况超出显示范围 显示滚动栏
            // AsNeeded  一直显示
            // Never        从不显示
            fSpread.HorizontalScrollBarPolicy = FarPoint.Win.Spread.ScrollBarPolicy.AsNeeded;
            fSpread.VerticalScrollBarPolicy = FarPoint.Win.Spread.ScrollBarPolicy.AsNeeded;

           //左右居中
            fSpread.ActiveSheet.Cells[0, 0].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
            //上下居中
            fSpread.ActiveSheet.Cells[0, 0].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;

            //单元格边框设置
            //边 的类型
            FarPoint.Win.ComplexBorderSide cBorderSide = new FarPoint.Win.ComplexBorderSide(FarPoint.Win.ComplexBorderSideStyle.ThinLine);
            //边框
            FarPoint.Win.ComplexBorder cBorder = new FarPoint.Win.ComplexBorder(cBorderSide, cBorderSide, cBorderSide, cBorderSide);
            fSpread.ActiveSheet.Cells[0, 0].Border = cBorder;

            //单元格 类型 比如:金额列  有千分位分隔符、两位小数
            FarPoint.Win.Spread.CellType.NumberCellType nct = new FarPoint.Win.Spread.CellType.NumberCellType();           
            nct.ShowSeparator = true;//分隔符
            nct.DecimalPlaces = 2;//小数位数
            nct.LeadingZero = FarPoint.Win.Spread.CellType.LeadingZero.UseRegional;
            fSpread.ActiveSheet.Cells[0, 0].CellType = nct;

            //设置字体
            fSpread.ActiveSheet.Cells[0, 0, 10, 10].Font = new System.Drawing.Font(fSpread.Font.Name, 10);
            fSpread.ActiveSheet.Cells[0, 0, 10, 10].Font = new System.Drawing.Font("Gerorgia", 10);

            //删除行   从索引为6開始删除2行
            fSpread.ActiveSheet.Rows.Remove(6, 2);
            //删除列   从索引为3開始删除1列
            fSpread.ActiveSheet.Columns.Remove(3, 1);

            //赋值
            fSpread.ActiveSheet.Cells[0, 0].Value = "赋值";

            //合并行  占3列2行
            fSpread.ActiveSheet.Cells[0, 0].ColumnSpan = 3;
            fSpread.ActiveSheet.Cells[0, 0].RowSpan = 2;
          
         
   //打印时 左右上下边距
            fSpread.ActiveSheet.PrintInfo.Margin.Left = 20;
            fSpread.ActiveSheet.PrintInfo.Margin.Right = 20;
            fSpread.ActiveSheet.PrintInfo.Margin.Top = 20;
            fSpread.ActiveSheet.PrintInfo.Margin.Bottom = 0;

            //打印时 纸张方向为横向
            fSpread.ActiveSheet.PrintInfo.Orientation = FarPoint.Win.Spread.PrintOrientation.Landscape;
            //纵向
            fSpread.ActiveSheet.PrintInfo.Orientation=FarPoint.Win.Spread.PrintOrientation.Portrait;
           
            //缩放比例
            fSpread.ActiveSheet.PrintInfo.ZoomFactor = 50/100;

                //纸张类型

                 SetPaperSize(fSpread,"A4");

        ///


        /// 设置纸张类型
        ///

        ///
        /// 纸张类型名称 例:A3,A4
        private void SetPaperSize(FarPoint.Win.Spread.FpSpread fspread,string PaperName)
        {
            System.Drawing.Printing.PrintDocument pdocumnet = new System.Drawing.Printing.PrintDocument();
            System.Drawing.Printing.PrinterSettings.PaperSizeCollection ps = pdocumnet.PrinterSettings.PaperSizes;
            foreach (System.Drawing.Printing.PaperSize p in ps)
            {
                if (p.PaperName.Equals(PaperName))
                {
                    fspread.ActiveSheet.PrintInfo.PaperSize = p;
                    return;
                }
            }
        }

//列宽自适应

 FarPoint.Win.Spread.CellType.EnhancedColumnHeaderRenderer ecr = new FarPoint.Win.Spread.CellType.EnhancedColumnHeaderRenderer();
            ecr.WordWrap = false;
            fSpread.Sheets[0].ColumnHeader.Columns[0].Renderer = ecr;
            this.fSpread.Sheets[0].DataAutoSizeColumns = true;
            this.fSpread.Sheets[0].Columns[0].Width = this.fSpread.Sheets[0].Columns[0].GetPreferredWidth();


评论


亲,登录后才可以留言!