C# NPOI操作Excel

时光总嘲笑我的痴心妄想 提交于 2019-11-29 04:47:30
      using System.IO;
      using NPOI.POIFS.FileSystem;
      using NPOI.HSSF.Util;
      using NPOI.HSSF.UserModel;
      using NPOI.XSSF.UserModel            
       //新建Excel文件                 IWorkbook workbook = new HSSFWorkbook();       //新建Sheet表
            ISheet sheet = workbook.CreateSheet("Test Result");       //创建单元格样式和字体样式
            IFont font = workbook.CreateFont();
            ICellStyle style = workbook.CreateCellStyle();           //设置居中       
            style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection;        //设置内容垂直居中
            style.VerticalAlignment = VerticalAlignment.Justify;
       //字体加粗                   font.Boldweight = short.MaxValue;
      //设置字体大小
          font.FontHeightInPoints = 12;      style.SetFont(font);
          IFont font1 = workbook.CreateFont(); ICellStyle style1 = workbook.CreateCellStyle(); style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection; style1.VerticalAlignment = VerticalAlignment.Justify;       //修改列宽       sheet.SetColumnWidth(1, 18 * 400);       sheet.SetColumnWidth(2, 18 * 256);     
            string[] value1 = { "NO", "UUT SN", "Condition"};        //新建行
            IRow row = sheet.CreateRow(0);        
            for (int a = 0; a < value1.Length; a++)
            {          //将数组中的值逐一添加到单元格中
               row.CreateCell(a).SetCellValue(Convert.ToString(value1[a]));         //给每个单元格写入样式
               row.GetCell(a).CellStyle = style;
            }        for (int i = 0; i < 32; i++)
            {          //追加行
                IRow row1 = sheet.CreateRow((sheet.LastRowNum + 1));
                row1.CreateCell(0).SetCellValue(i + 1);
                row1.CreateCell(1).SetCellValue("5473567I008D9193541A1000041");
                row1.CreateCell(2).SetCellValue("Temperature ");
                row1.GetCell(0).CellStyle = style1;
                row1.GetCell(1).CellStyle = style1;
                row1.GetCell(2).CellStyle = style1;
                font1.Boldweight = short.MaxValue;
                style1.SetFont(font1);

                row1 = sheet.CreateRow((sheet.LastRowNum + 1));
                row1.CreateCell(2).SetCellValue("AC input");
                row1.GetCell(2).CellStyle = style1;


                row1 = sheet.CreateRow((sheet.LastRowNum + 1));
                row1.CreateCell(2).SetCellValue("DC  output");
                row1.GetCell(2).CellStyle = style1;


                row1 = sheet.CreateRow((sheet.LastRowNum + 1));
                row1.CreateCell(2).SetCellValue("Current");
                row1.GetCell(2).CellStyle = style1;
          //新建path
                FileStream fs = new FileStream(@"C:\Users\lenovo\Desktop\Test Result.xls", FileMode.Create, FileAccess.Write);          //写入
                workbook.Write(fs);          //关闭
                fs.Close();
            }
        //追加内容函数         FileStream fs = new FileStream(@"C:\Users\lenovo\Desktop\Test Result.xls", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);//读取流
            POIFSFileSystem ps = new POIFSFileSystem(fs);
            IWorkbook workbook = new HSSFWorkbook(ps);
            ISheet sheet = workbook.GetSheetAt(0);//获取工作表
            IRow row = sheet.GetRow(0); //得到表头
            ColumnStyle column1 = new ColumnStyle();


            FileStream fout = new FileStream(@"C:\Users\lenovo\Desktop\Test Result.xls", FileMode.Open, FileAccess.Write, FileShare.ReadWrite);//写入流         
            ICellStyle style = workbook.CreateCellStyle();
            style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection;
            style.VerticalAlignment = VerticalAlignment.Justify;
     

            IFont font = workbook.CreateFont();
            ICellStyle style1 = workbook.CreateCellStyle();
            style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection;
            style1.VerticalAlignment = VerticalAlignment.Justify;
            font.Boldweight = short.MaxValue;
            style1.SetFont(font);

            int cellNum = 0;
            cellNum = row.LastCellNum;
            row.CreateCell(cellNum).SetCellValue(DateTime.Now.ToString("HH:mm:ss")); 
            row.GetCell(cellNum).CellStyle = style1;

            for (int i = 0; i < 32; i++)
            {
                row = sheet.GetRow(1 + i*4);
                row.CreateCell(cellNum).SetCellValue("40℃");
                row.GetCell(cellNum).CellStyle = style;

                row = sheet.GetRow(2 + i * 4);
                row.CreateCell(cellNum).SetCellValue("223.7V");
                row.GetCell(cellNum).CellStyle = style;

                row = sheet.GetRow(3 + i * 4);
                row.CreateCell(cellNum).SetCellValue("6.07V");
                row.GetCell(cellNum).CellStyle = style;

                row = sheet.GetRow(4 + i * 4);
                row.CreateCell(cellNum).SetCellValue("3.79A");
                row.GetCell(cellNum).CellStyle = style;
            }

            fout.Flush();
            workbook.Write(fout);//写入文件
            workbook = null;
            fout.Close();

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!