NPOI导出Excel文件,对单元格的一些设置
1 HSSFWorkbook book = new HSSFWorkbook(); 2 MemoryStream ms = new MemoryStream(); 3 ISheet sheet = book.CreateSheet("sheet1"); 4 ICellStyle cellStyle = book.CreateCellStyle(); 5 6 // 首列 7 NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(0); 8 row1.CreateCell(0).SetCellValue(System.DateTime.Now.Year+"年XXXX"); 9 //设置单元格的样式:水平对齐居中 10 row1.Cells[0].CellStyle.VerticalAlignment = VerticalAlignment.Justify;//垂直对齐(默认应该为center,如果center无效则用justify) 11 row1.Cells[0].CellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//水平对齐 12 CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 8);//开始行的索引,结束行的索引,开始列的索引,结束列的索引 13 14 sheet.AddMergedRegion(cellRangeAddress);//要合并单元格所在的sheet
参考网址:http://www.jb51.net/article/36002.htm
1 NPOI.SS.UserModel.IRow row = sheet.CreateRow(1); 2 row.CreateCell(0).SetCellValue("序号"); 3 row.CreateCell(1).SetCellValue("单位"); 4 row.CreateCell(2).SetCellValue("姓名"); 5 row.CreateCell(3).SetCellValue("性别"); 6 row.CreateCell(4).SetCellValue("年龄"); 7 row.CreateCell(5).SetCellValue("民族"); 8 row.CreateCell(6).SetCellValue("培训班名称"); 9 row.CreateCell(7).SetCellValue("电报号"); 10 row.CreateCell(8).SetCellValue("培训时间"); 11 sheet.SetColumnWidth(1, 30 * 256);//设置列宽 12 sheet.SetColumnWidth(6, 30*256); 13 sheet.SetColumnWidth(7, 30 * 256); 14 sheet.SetColumnWidth(8, 50*256);
参考网址:http://www.jb51.net/article/55407.htm