命名空间:
using NPOI.HSSF; //支持读写EXCEL2003using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;//EXCEL公用接口及公式using NPOI.XSSF.UserModel;//Excel 2007及以上格式读写
全部程序:
private void button1_Click(object sender, EventArgs e){var wk = new XSSFWorkbook();var st = wk.CreateSheet("第一页面");var r = st.CreateRow(0);//行var c = r.CreateCell(0);//列c.SetCellValue("你好");//单无格内容
//合并单元格var hb = new NPOI.SS.Util.CellRangeAddress(0, 4, 0, 3);st.AddMergedRegion(hb);var Myfont = wk.CreateFont();//创建字体var mycellstyle = wk.CreateCellStyle();Myfont.FontHeightInPoints = 15;Myfont.FontName = "微软雅黑";Myfont.Color = NPOI.HSSF.Util.HSSFColor.BlueGrey.Index ;//字体mycellstyle.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;//底色 mycellstyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index; //0-55的数字也可以mycellstyle.SetFont(Myfont);//设置字体c.CellStyle = mycellstyle;var FS = new FileStream(@"D:\2.xlsx", FileMode.Create, FileAccess.Write);//输出wk.Write(FS);写文件FS.Close();MessageBox.Show("写入ok");}}}