您的位置:首页 > 其它

NPOI2.2.0.0实例详解(三)—设置EXCEL列宽、行高与合并单元格

2015-12-08 11:04 891 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Eval;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;
using System.IO;
using NPOI.SS.Util;
using System.Drawing;
using NPOI.HSSF.Util;

namespace NPOI
{
class Program2
{
static void Main(string[] args)
{
//说明:设置列宽、行高与合并单元格

//1.创建EXCEL中的Workbook
IWorkbook myworkbook = new XSSFWorkbook();

//2.创建Workbook中的Sheet
ISheet mysheet = myworkbook.CreateSheet("sheet1");

//4.合并单元格区域 例: 第1行到第1行 第2列到第3列围成的矩形区域
mysheet.AddMergedRegion(new CellRangeAddress(0, 0, 1, 2));

//5.创建Row中的Cell并赋值
IRow row0 = mysheet.CreateRow(0);
row0.CreateCell(0).SetCellValue("0-0");
row0.CreateCell(1).SetCellValue("0-1");
row0.CreateCell(3).SetCellValue("0-3");

//6.设置列宽   SetColumnWidth(列的索引号从0开始, N * 256) 第二个参数的单位是1/256个字符宽度。例:将第四列宽度设置为了30个字符。
mysheet.SetColumnWidth(3, 30 * 256);

//7.设置行高   Height的单位是1/20个点。例:设置高度为50个点
row0.Height=50*20;

//5.保存
FileStream file= new FileStream(@"E:\myworkbook2.xlsx", FileMode.Create);
myworkbook.Write(file);
file.Close();
}
}
}

运行后,效果如下图所示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息