zoukankan      html  css  js  c++  java
  • NPOI的导出、样式设置、合并相同单元格

    //创建Excel文件的对象
    HSSFWorkbook workbook = new HSSFWorkbook();
    //添加一个sheet
    ISheet sheet = workbook.CreateSheet("Sheet1");
    sheet.DefaultRowHeight = 300;
    //创建标题
    IRow rowTitle = sheet.CreateRow(0);
    
    //设置Title的样式
    rowTitle.Height = 500;
    ICellStyle styleTitle = workbook.CreateCellStyle();
    styleTitle.Alignment = HorizontalAlignment.Center;
    styleTitle.VerticalAlignment = VerticalAlignment.Center;
    IFont fontTitle = workbook.CreateFont();
    fontTitle.FontName = "宋体";
    fontTitle.FontHeightInPoints = 18;
    styleTitle.SetFont(fontTitle);
    ICell cellTitle = rowTitle.CreateCell(0);
    cellTitle.SetCellValue("文件"+ stratTime.Substring(0, 7));
    cellTitle.CellStyle = styleTitle;
    sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 9)); //合并单元格
    
    //创建表格样式
    IFont font = workbook.CreateFont();
    font.FontName = "宋体";
    font.FontHeightInPoints = 10;
    ICellStyle style = workbook.CreateCellStyle();
    style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
    style.BottomBorderColor = HSSFColor.Black.Index;
    style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
    style.LeftBorderColor = HSSFColor.Black.Index;
    style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
    style.RightBorderColor = HSSFColor.Black.Index;
    style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
    style.TopBorderColor = HSSFColor.Black.Index;
    style.Alignment = HorizontalAlignment.Center;
    style.SetFont(font);
    
    //这是动销记录为0的样式
    ICellStyle style1 = workbook.CreateCellStyle();
    style1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
    style1.BottomBorderColor = HSSFColor.Black.Index;
    style1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
    style1.LeftBorderColor = HSSFColor.Black.Index;
    style1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
    style1.RightBorderColor = HSSFColor.Black.Index;
    style1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
    style1.TopBorderColor = HSSFColor.Black.Index;
    style1.Alignment = HorizontalAlignment.Center;
    style1.VerticalAlignment = VerticalAlignment.Center;
    style1.SetFont(font);
    style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;
    style1.FillPattern = FillPattern.SolidForeground;
    
    //公司和品牌的样式动销不为0
    ICellStyle style2 = workbook.CreateCellStyle();
    style2.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
    style2.BottomBorderColor = HSSFColor.Black.Index;
    style2.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
    style2.LeftBorderColor = HSSFColor.Black.Index;
    style2.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
    style2.RightBorderColor = HSSFColor.Black.Index;
    style2.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
    style2.TopBorderColor = HSSFColor.Black.Index;
    style2.Alignment = HorizontalAlignment.Left;
    style2.VerticalAlignment = VerticalAlignment.Center;
    style2.SetFont(font);
    //公司和品牌样式动销记录为0
    ICellStyle style3 = workbook.CreateCellStyle();
    style3.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
    style3.BottomBorderColor = HSSFColor.Black.Index;
    style3.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
    style3.LeftBorderColor = HSSFColor.Black.Index;
    style3.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
    style3.RightBorderColor = HSSFColor.Black.Index;
    style3.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
    style3.TopBorderColor = HSSFColor.Black.Index;
    style3.Alignment = HorizontalAlignment.Left;
    style3.VerticalAlignment = VerticalAlignment.Center;
    style3.SetFont(font);
    style3.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;
    style3.FillPattern = FillPattern.SolidForeground;
    
    //创建表头
    IRow rowHead = sheet.CreateRow(1);
    rowHead.CreateCell(0).SetCellValue("街道");
    rowHead.GetCell(0).CellStyle = style;//指定样式
    sheet.SetColumnWidth(0, 256 * 25);
    
    rowHead.CreateCell(1).SetCellValue("公司");
    rowHead.GetCell(1).CellStyle = style;
    sheet.SetColumnWidth(1, 256 * 30);
    
    rowHead.CreateCell(2).SetCellValue("品牌");
    rowHead.GetCell(2).CellStyle = style;
    sheet.SetColumnWidth(2, 256 * 30);
    
    rowHead.CreateCell(3).SetCellValue("当月最后动销时间");
    rowHead.GetCell(3).CellStyle = style;
    sheet.SetColumnWidth(3, 256 * 15);
    
    rowHead.CreateCell(4).SetCellValue("供应商数量");
    rowHead.GetCell(4).CellStyle = style;
    sheet.SetColumnWidth(4, 256 * 15);
    rowHead.CreateCell(5).SetCellValue("商品总量");
    rowHead.GetCell(5).CellStyle = style;
    sheet.SetColumnWidth(5, 256 * 15);
    rowHead.CreateCell(6).SetCellValue("当月入库总量");
    rowHead.GetCell(6).CellStyle = style;
    sheet.SetColumnWidth(6, 256 * 15);
    rowHead.CreateCell(7).SetCellValue("当月出库总量");
    rowHead.GetCell(7).CellStyle = style;
    sheet.SetColumnWidth(7, 256 * 15);
    rowHead.CreateCell(8).SetCellValue("当月动销记录");
    rowHead.GetCell(8).CellStyle = style;
    sheet.SetColumnWidth(8, 256 * 15);
    rowHead.CreateCell(9).SetCellValue("是否录入零度");
    rowHead.GetCell(9).CellStyle = style;
    sheet.SetColumnWidth(9, 256 * 15);
    
    int rowindex = 2;
    foreach (var item in query)
    {
    IRow rowContent = sheet.CreateRow(rowindex);//创建新行
    if (item.CountDX == 0)
    {
    rowContent.CreateCell(0).SetCellValue(item.Street.ToString());
    rowContent.GetCell(0).CellStyle = style1;
    rowContent.CreateCell(1).SetCellValue(Convert.ToString(item.Compnay));
    rowContent.GetCell(1).CellStyle = style3;
    rowContent.CreateCell(2).SetCellValue(Convert.ToString(item.Brand));
    rowContent.GetCell(2).CellStyle = style3;
    rowContent.CreateCell(3).SetCellValue(Convert.ToString(item.DateDX));
    rowContent.GetCell(3).CellStyle = style1;
    rowContent.CreateCell(4).SetCellValue(Convert.ToString(item.Supplier));
    rowContent.GetCell(4).CellStyle = style1;
    rowContent.CreateCell(5).SetCellValue(Convert.ToString(item.ProductCount));
    rowContent.GetCell(5).CellStyle = style1;
    rowContent.CreateCell(6).SetCellValue(Convert.ToString(item.WarehousingCount));
    rowContent.GetCell(6).CellStyle = style1;
    rowContent.CreateCell(7).SetCellValue(Convert.ToString(item.checkout));
    rowContent.GetCell(7).CellStyle = style1;
    rowContent.CreateCell(8).SetCellValue(Convert.ToString(item.CountDX));
    rowContent.GetCell(8).CellStyle = style1;
    rowContent.CreateCell(9).SetCellValue(Convert.ToString(item.Reading));
    rowContent.GetCell(9).CellStyle = style1;
    }
    else
    {
    rowContent.CreateCell(0).SetCellValue(item.Street.ToString());
    rowContent.GetCell(0).CellStyle = style;
    rowContent.CreateCell(1).SetCellValue(Convert.ToString(item.Compnay));
    rowContent.GetCell(0).CellStyle = style2;
    rowContent.CreateCell(2).SetCellValue(Convert.ToString(item.Brand));
    rowContent.GetCell(0).CellStyle = style2;
    rowContent.CreateCell(3).SetCellValue(Convert.ToString(item.DateDX));
    rowContent.GetCell(3).CellStyle = style;
    rowContent.CreateCell(4).SetCellValue(Convert.ToString(item.Supplier));
    rowContent.GetCell(4).CellStyle = style;
    rowContent.CreateCell(5).SetCellValue(Convert.ToString(item.ProductCount));
    rowContent.GetCell(5).CellStyle = style;
    rowContent.CreateCell(6).SetCellValue(Convert.ToString(item.WarehousingCount));
    rowContent.GetCell(6).CellStyle = style;
    rowContent.CreateCell(7).SetCellValue(Convert.ToString(item.checkout));
    rowContent.GetCell(7).CellStyle = style;
    rowContent.CreateCell(8).SetCellValue(Convert.ToString(item.CountDX));
    rowContent.GetCell(8).CellStyle = style;
    rowContent.CreateCell(9).SetCellValue(Convert.ToString(item.Reading));
    rowContent.GetCell(9).CellStyle = style;
    }
    rowindex++;
    }
    IRow rowContent2 = sheet.CreateRow(rowindex + 2);//创建新行
    rowContent2.CreateCell(0).SetCellValue("代表动销记录为0");
    rowContent2.CreateCell(1).SetCellValue("");
    rowContent2.CreateCell(2).SetCellValue("制表日期" + System.DateTime.Now.ToString("yyyyMMdd"));
    rowContent2.CreateCell(3).SetCellValue("");
    
    sheet.AddMergedRegion(new CellRangeAddress(rowindex + 2, rowindex + 2, 2, 3)); //合并单元格
    rowContent2.GetCell(0).CellStyle = style1;
    rowContent2.GetCell(2).CellStyle = style1;
    rowContent2.GetCell(3).CellStyle = style1;
    
    //合并单元格样式
    ICellStyle cellstyle = workbook.CreateCellStyle();
    cellstyle.VerticalAlignment = VerticalAlignment.Center;
    cellstyle.Alignment = HorizontalAlignment.Center;
    //合并行
    int count = query.Count() + 2;//总计单元行数
    for (int i = 0; i < count; i++)
    {
    //获取当前行第0列
    string value = sheet.GetRow(i).GetCell(0).StringCellValue;
    
    int end = i;
    //找到结束为止
    for (int j = i + 1; j < count; j++)
    {
    //获取下一行行第0列
    string value1 = sheet.GetRow(j).GetCell(0).StringCellValue;
    if (value != value1)
    {
    end = j - 1;
    break;
    }
    else if (value == value1 || j == query.Count())
    {
    end = j;
    
    }
    }
    
    sheet.AddMergedRegion(new CellRangeAddress(i, end, 0, 0));//和并单元格
    var cell = sheet.GetRow(i).GetCell(0);
    cell.CellStyle = cellstyle;
    i = end;
    }
    // 写入到客户端 
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    workbook.Write(ms);
    ms.Seek(0, SeekOrigin.Begin);
  • 相关阅读:
    通过pip工具安装selenium(初次安装、升级、降级)
    一次悲催的nginx转发白屏经历
    oracle-----视图/物化视图
    python初级实战-----主机在线情况监控web
    python模块------os
    python模块------pymysql
    python初级实战(1)-----关于邮件发送问题
    python基础(9)-----文件操作
    sql总结-----数据表操作
    neutron-----常用命令
  • 原文地址:https://www.cnblogs.com/geekX/p/11445427.html
Copyright © 2011-2022 走看看