FarPoint.Win.Spread 常规操作
FarPoint.Win.Spread.FpSpread fSpread = new FarPoint.Win.Spread.FpSpread();
让某行或某列不可编辑:
vsprZENTEI1.ActiveSheet.Rows[1].Locked = true;
vsprZENTEI1.ActiveSheet.Columns[1].Locked = true;
让列头、行头不锁定。
this.fpsprCost_Sheet1.ColumnHeader.DefaultStyle.Locked = false;
this.fpsprCost_Sheet1.RowHeader.DefaultStyle.Locked = false;
//设置 行数、列数
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");
/// <summary>
/// 设置纸张类型
/// </summary>
/// <param name="fspread"></param>
/// <param name="PaperName">纸张类型名称 例:A3,A4</param>
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();