26
2018
09

NPOI设置单元格格式



using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NPOILessonConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
            NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
            var TitleCellStyle = book.CreateCellStyle();
            TitleCellStyle.FillPattern = FillPattern.SolidForeground; //重要必须设置,不设置不启作用
            TitleCellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index; //背景黄色
            TitleCellStyle.Alignment = HorizontalAlignment.Center; //居中
            TitleCellStyle.BorderBottom = BorderStyle.Thin;
            TitleCellStyle.BorderTop = BorderStyle.Thin;
            TitleCellStyle.BorderLeft = BorderStyle.Thin;
            TitleCellStyle.BorderRight = BorderStyle.Thin;
            
            var TitleCellFont = book.CreateFont();
            TitleCellFont.FontHeightInPoints = 10;
            TitleCellFont.FontName = "微软雅黑";
            TitleCellFont.Color = NPOI.HSSF.Util.HSSFColor.Black.Index;
            TitleCellFont.Boldweight = (short)FontBoldWeight.Bold;
            TitleCellStyle.SetFont(TitleCellFont);


            row1.CreateCell(0).SetCellValue("类型");
            row1.GetCell(0).CellStyle = TitleCellStyle;
            
            row1.CreateCell(1).SetCellValue("渠道");
            row1.GetCell(1).CellStyle = TitleCellStyle;

            row1.CreateCell(2).SetCellValue("取还方式");
            row1.GetCell(2).CellStyle = TitleCellStyle;

            row1.CreateCell(3).SetCellValue("邮费支付方式");
            row1.GetCell(3).CellStyle = TitleCellStyle;

            row1.CreateCell(4).SetCellValue("物流费用");
            row1.GetCell(4).CellStyle = TitleCellStyle;

            row1.CreateCell(5).SetCellValue("起租日期");
            row1.GetCell(5).CellStyle = TitleCellStyle;

            row1.CreateCell(6).SetCellValue("归还日期");
            row1.GetCell(6).CellStyle = TitleCellStyle;

            row1.CreateCell(7).SetCellValue("租凭天数");
            row1.GetCell(7).CellStyle = TitleCellStyle;

            row1.CreateCell(8).SetCellValue("分渠道佣金");
            row1.GetCell(8).CellStyle = TitleCellStyle;


            sheet1.SetColumnWidth(0, 13 * 256);
            sheet1.SetColumnWidth(1, 13 * 256);
            sheet1.SetColumnWidth(2, 13 * 256);
            sheet1.SetColumnWidth(3, 15 * 256);
            sheet1.SetColumnWidth(4, 13 * 256);
            sheet1.SetColumnWidth(5, 13 * 256);
            sheet1.SetColumnWidth(6, 13 * 256);
            sheet1.SetColumnWidth(7, 13 * 256);
            sheet1.SetColumnWidth(8, 13 * 256);

            // 写入到客户端 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);

            using (FileStream fs = File.Create("订单导出列表.xls"))
            {
                byte[] buffer = ms.ToArray();//转化为byte格式存储
                fs.Write(buffer, 0, buffer.Length);
                fs.Flush();
                buffer = null; 
            }
        }
    }
}













« 上一篇

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。