zoukankan      html  css  js  c++  java
  • 通过 NPOI 生成 Excel

            HSSFWorkbook hssfworkbook;
            ISheet sheet1;
    
            public void BuildExcel()
            {
                hssfworkbook = new HSSFWorkbook();
                // 新建一个Excel页签
                sheet1 = hssfworkbook.CreateSheet("Sheet1");
    
                // 创建新增行
                for (var i = 0; i < 10;i++ )
                {
                    IRow row1 = sheet1.CreateRow(i);
                    for (var j = 0; j < 10; j++)
                    {
                        //新建单元格
                        ICell cell = row1.CreateCell(j);
    
    
    
                        // 单元格赋值
                        cell.SetCellValue("单元格"+j.ToString());
                    }
                }
                
                // 设置行宽度
                sheet1.SetColumnWidth(2, 10 * 256);
    
    
                // 获取单元格 并设置样式
                ICellStyle styleCell = hssfworkbook.CreateCellStyle();
                //居中
                styleCell.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                //垂直居中 
                styleCell.VerticalAlignment = VerticalAlignment.Top;
                ICellStyle cellStyle = hssfworkbook.CreateCellStyle();
    
                //设置字体
                IFont fontColorRed = hssfworkbook.CreateFont();
                fontColorRed.Color = HSSFColor.OliveGreen.Red.Index;
    
                styleCell.SetFont(fontColorRed);
    
                
                sheet1.GetRow(2).GetCell(2).CellStyle = styleCell;
    
                // 合并单元格
                sheet1.AddMergedRegion(new CellRangeAddress(2, 4, 2, 5));
    
    
                // 输出Excel
                string filename = "cnblogs.rhythmk.com.导出.xls";
                var context = HttpContext.Current;
                context.Response.ContentType = "application/vnd.ms-excel";
                context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", context.Server.UrlEncode(filename)));
                context.Response.Clear();
    
               
                MemoryStream file = new MemoryStream();
                hssfworkbook.Write(file);
                context.Response.BinaryWrite(file.GetBuffer());
                context.Response.End();
    
              
    
            }
    

      

  • 相关阅读:
    log4j的使用
    转:http与https
    转:归一化与正则化
    转:python 的开源库
    转:openTSDB 2.0 安装
    hadoop 2.2.0编译安装及打包
    查看磁盘信息
    HBase Region的flush过程
    HBase96的RPC
    阐述二维码的原理以及使用google api和PHP QR Code来生成二维码
  • 原文地址:https://www.cnblogs.com/rhythmK/p/3833393.html
Copyright © 2011-2022 走看看