您的位置:首页 > 其它

NPOI 1.2.3教程 -16 合并单元格MergeCells

2011-01-23 16:04 387 查看
using System;
using System.Text;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.HSSF.UserModel.Contrib;
using NPOI.SS.Util;
using NPOI.SS.UserModel;

namespace MergeCellsInXls
{
class Program
{
static void Main(string[] args)
{
InitializeWorkbook();

Sheet sheet = hssfworkbook.CreateSheet("new sheet");

Row row = sheet.CreateRow(0);
row.HeightInPoints = 30;

Cell cell = row.CreateCell(0);
//set the title of the sheet
cell.SetCellValue("Sales Report");

CellStyle style = hssfworkbook.CreateCellStyle();
style.Alignment = HorizontalAlignment.CENTER;
//create a font style
Font font = hssfworkbook.CreateFont();
font.FontHeight = 20*20;
style.SetFont(font);
cell.CellStyle = style;

//merged cells on single row
//ATTENTION: don't use Region class, which is obsolete
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 5));

//merged cells on mutiple rows
CellRangeAddress region = new CellRangeAddress(2, 4, 2, 4);
sheet.AddMergedRegion(region);

//set enclosed border for the merged region
((HSSFSheet)sheet).SetEnclosedBorderOfRegion(region, CellBorderType.DOTTED, NPOI.HSSF.Util.HSSFColor.RED.index);

WriteToFile();
}

static HSSFWorkbook hssfworkbook;

static void WriteToFile()
{
//Write the stream data of workbook to the root directory
FileStream file = new FileStream(@"test.xls", FileMode.Create);
hssfworkbook.Write(file);
file.Close();
}

static void InitializeWorkbook()
{
hssfworkbook = new HSSFWorkbook();

//Create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI Team";
hssfworkbook.DocumentSummaryInformation = dsi;

//Create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "NPOI SDK Example";
hssfworkbook.SummaryInformation = si;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: