随笔只用来个人记录日常。
sheet中的row,cell下标都是从0开始。
bool result = true; string fileNanme = ""; int CellCount = 10;//通过它来循环 表头 #region 打开保存窗口 然后保存数据 SaveFileDialog fileDialog = new SaveFileDialog(); fileDialog.DefaultExt = ".xlsx"; fileDialog.Filter = "Excel文件|*.xlsx"; fileDialog.ShowDialog(); fileNanme = fileDialog.FileName; #endregion #region 第一行的样式 IWorkbook workbook = new XSSFWorkbook(); ICellStyle cellStyle = workbook.CreateCellStyle(); //声明cell样式 标题的样式 cellStyle.Alignment = HorizontalAlignment.Right; //水平 靠右 cellStyle.VerticalAlignment = VerticalAlignment.Center; //垂直样式 居中 IFont font = workbook.CreateFont();//字体 font.FontHeightInPoints = 11; //字体大小 cellStyle.SetFont(font);//加入单元格 #endregion #region 标题加粗样式 ICellStyle CellBold = workbook.CreateCellStyle(); CellBold.Alignment = HorizontalAlignment.Center;//居中对齐 水平 CellBold.VerticalAlignment = VerticalAlignment.Center; //垂直 剧中 IFont FontBold = workbook.CreateFont();//创建字体 FontBold.FontHeightInPoints = 18; CellBold.SetFont(FontBold); #endregion #region 普通字体 ICellStyle CellCon = workbook.CreateCellStyle(); CellCon.Alignment = HorizontalAlignment.Center; CellCon.VerticalAlignment = VerticalAlignment.Center; IFont FontCon = workbook.CreateFont(); FontCon.FontName = "宋体"; FontCon.FontHeightInPoints = 11; CellCon.SetFont(FontCon); #endregion ISheet sheet = workbook.CreateSheet("sheet1");//创建sheet1表格 #region 第一行标题 IRow row = sheet.CreateRow(0); //创建第0行 row.HeightInPoints = 20;//行高 #region 第一行 ICell cell0 = row.CreateCell(0);//创建单元格 cell0.SetCellValue(reportName);//赋值 标题名 cell0.CellStyle = cellStyle; sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, CellCount)); #endregion #endregion #region 第二行 #region //粗体 IRow row1 = sheet.CreateRow(1); ICell cell1 = row1.CreateCell(0); cell1.SetCellValue("稳定性测试报告"); cell1.CellStyle = CellBold; sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 7)); #endregion ICell cell2 = row1.CreateCell(8); cell2.SetCellValue("实验记录本编号"); cell2.CellStyle = CellCon; sheet.AddMergedRegion(new CellRangeAddress(1, 1, 8, 9)); sheet.SetColumnWidth(1, 250);//////////////////////////这个是设置列的宽度 ICell cell3 = row1.CreateCell(10); cell3.SetCellValue(orderNo); cell3.CellStyle = CellCon; sheet.AddMergedRegion(new CellRangeAddress(1, 1, 10, 10)); #endregion #region 第三行 IRow row2 = sheet.CreateRow(2); ICell cell3A = row2.CreateCell(0); cell3A.CellStyle = CellCon; cell3A.SetCellValue("产品名称: 半成品代码:配方号:/ 批号:9E6041"); sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 8)); ICell cell3B = row2.CreateCell(9); cell3B.CellStyle = CellCon; cell3B.SetCellValue("测试开始"); sheet.AddMergedRegion(new CellRangeAddress(2, 2, 9, 9)); ICell cell3C = row2.CreateCell(10); cell3C.CellStyle = CellCon; cell3C.SetCellValue(DateTime.Now.ToString("yyyy-MM-dd")); sheet.AddMergedRegion(new CellRangeAddress(2, 2, 10, 10)); #endregion #region 第四行 IRow row3 = sheet.CreateRow(3); ICell cell4A = row3.CreateCell(0); cell3A.CellStyle = CellCon; cell3A.SetCellValue("测试阶段:□S1A ■S2A □S3A, 打样量(batch Size): 5 kg,测试包装形式:玻璃瓶 ,测试规格:250 g/ml"); sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 8)); ICell cell4B = row3.CreateCell(9); cell3B.CellStyle = CellCon; cell3B.SetCellValue("比重"); sheet.AddMergedRegion(new CellRangeAddress(2, 2, 9, 9)); ICell cell4C = row3.CreateCell(10); cell3C.CellStyle = CellCon; cell3C.SetCellValue("/"); sheet.AddMergedRegion(new CellRangeAddress(2, 2, 10, 10)); #endregion #region 第五行 合并第7行 IRow row4 = sheet.CreateRow(4); ICell cell5A = row4.CreateCell(0); cell5A.CellStyle = CellCon; cell5A.SetCellValue("测试条件"); sheet.AddMergedRegion(new CellRangeAddress(4, 6, 0, 0)); ICell cell5B = row4.CreateCell(1); cell5B.CellStyle = CellCon; cell5B.SetCellValue("周期(周)"); sheet.AddMergedRegion(new CellRangeAddress(4, 6, 1, 1)); row4.CreateCell(2).SetCellValue("测试项目"); row4.CreateCell(3).SetCellValue("外观*"); row4.CreateCell(4).SetCellValue("颜色*"); row4.CreateCell(5).SetCellValue("气味*"); row4.CreateCell(6).SetCellValue("粘度/硬度"); row4.CreateCell(7).SetCellValue("pH值"); row4.CreateCell(8).SetCellValue("防腐剂/活性物1"); row4.CreateCell(9).SetCellValue("防腐剂/活性物2"); row4.CreateCell(10).SetCellValue("防腐剂/活性物3"); IRow row5 = sheet.CreateRow(5); row5.CreateCell(2).SetCellValue("SPEC"); row5.CreateCell(3).SetCellValue("均匀料体,符合标样 "); row5.CreateCell(4).SetCellValue("浅粉色,符合标样 "); row5.CreateCell(5).SetCellValue("符合标样"); row5.CreateCell(6).SetCellValue("40000-100000 S6,5rpm"); row5.CreateCell(7).SetCellValue("6.5-8.0直测"); row5.CreateCell(8).SetCellValue("/"); row5.CreateCell(9).SetCellValue("/"); row5.CreateCell(10).SetCellValue("/"); IRow row6 = sheet.CreateRow(6); row6.CreateCell(2).SetCellValue("方法编号"); row6.CreateCell(3).SetCellValue("GCM085-1"); row6.CreateCell(4).SetCellValue("GCM086-1"); row6.CreateCell(5).SetCellValue("GCM006-1"); row6.CreateCell(6).SetCellValue("GCM0011-1"); row6.CreateCell(7).SetCellValue("/"); row6.CreateCell(8).SetCellValue("/"); row6.CreateCell(9).SetCellValue("/"); row6.CreateCell(10).SetCellValue("/"); #endregion #region 给所有单元格增加宽度 sheet.SetColumnWidth(0, 18 * 256); sheet.SetColumnWidth(1, 50 * 256); sheet.SetColumnWidth(2, 16 * 256); sheet.SetColumnWidth(3, 14 * 256); sheet.SetColumnWidth(4, 14 * 256); sheet.SetColumnWidth(5, 16 * 256); sheet.SetColumnWidth(6, 23 * 256); sheet.SetColumnWidth(7, 14 * 256); sheet.SetColumnWidth(8, 14 * 256); sheet.SetColumnWidth(9, 14 * 256); sheet.SetColumnWidth(10, 14 * 256); #endregion //保存 FileStream file = new FileStream(fileNanme, FileMode.Create); workbook.Write(file); file.Close(); return result;
完成效果。