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; } } }
相关文章推荐
- NPOI 1.2.3教程 -5 分拆凍結 SplitAndFreeze
- NPOI 1.2.3教程 -18 分组Group
- NPOI 1.2.3教程 -6 重复Repeat
- NPOI 1.2.3教程 -19 读取模板Template
- NPOI 1.2.3教程 -20 填充背景FillBackground
- NPOI 1.2.3教程 -21 下载Download
- NPOI 1.2.3教程 -22 画图Drawing
- NPOI 1.2.3教程 -7 单元格高度和宽度HeightAndWidth
- NPOI 1.2.3教程 -23 页眉页脚HeaderFooter
- NPOI 1.2.3教程 -8 打印Print
- NPOI 1.2.3教程 -24 下拉框DropDownList
- NPOI 1.2.3教程 -9 格式化Format
- NPOI 1.2.3教程 -25 条件样式ConditionalFormat
- NPOI 1.2.3教程 -10批注Comment
- NPOI 1.2.3教程 -26 字体Font
- NPOI 1.2.3教程 -11 边框样式BorderStyle
- NPOI 1.2.3教程 -27 超级链接Hyperlink
- NPOI 1.2.3教程 -12 对齐Alignment
- NPOI 1.2.3教程 -13 活动单元格ActiveCell
- NPOI 1.2.3教程 - 1 认识NPOI