用NPOI、C#操作Excel表格生成班级成绩单
2021-03-31 08:26
                         标签:gen   sharp   case   pattern   normal   字体设置   ntb   生成   读取    转:https://blog.csdn.net/dcrmg/article/details/52374194#commentBox   在C#中利用NPOI操作Excel表格非常方便,几乎上支持所有的Excel表格本身所有的功能,如字体设置、颜色设置、单元格合并、数值计算、页眉页脚等等。 这里准备使用NPOI生成一个班级成绩单Excel表格,表格中包含的信息包括学号、姓名、各科成绩、平均成绩、排名等。   实现原理很简单,主要是NPOI的一些操作,具体实现的功能包括下边几个:                执行后,在E盘指定目录下生成了名字是“Score Excel”的表格:     “名次”列的排名实现: 先声明了一个大小为8的Int数组,默认值设为1,依次拿当前的平均成绩和其他7个的平均成绩对比,有几个大于当前平均成绩的元素,就在当前数组值上加上几,最后得到的就是每个人的排名,实现如下:                   用NPOI、C#操作Excel表格生成班级成绩单 标签:gen   sharp   case   pattern   normal   字体设置   ntb   生成   读取    原文地址:https://www.cnblogs.com/dare/p/9262226.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Data;
 
namespace Score_Excel
{
    class Program
    {
        static void Main(string[] args)
        {
            IWorkbook workbook = new HSSFWorkbook();//声明工作簿对象,可以创建xls或xlsx Excel文件
            ISheet sheet1 = workbook.CreateSheet("Score Record"); //创建工作表
            ICell sheet1Title = sheet1.CreateRow(0).CreateCell(0); //创建第一行第一个单元格
            sheet1Title.SetCellValue("国家体育总局附属二小幼儿园小一班体育成绩表"); //表头
            sheet1Title.CellStyle = GetTitleCellStyle(workbook);
            sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 9));  //合并单元格
            DataTable dt = GetData();
            IRow row;
            ICell cell;
            ICellStyle cellStyle1 = GetCellStyle(workbook, 2);
            ICellStyle cellStyle2 = GetCellStyle(workbook, 0);
            double[] aveScore = new double[8]; //平均成绩数组
            int[] rankNum = new int[8];  //名次数组
 
            //表头数据
            row = sheet1.CreateRow(1);
            cell = row.CreateCell(0);
            cell.SetCellValue("学号");
            cell.CellStyle = cellStyle1;
 
            cell = row.CreateCell(1);
            cell.SetCellValue("姓名");
            cell.CellStyle = cellStyle1;
 
            cell = row.CreateCell(2);
            cell.SetCellValue("排球");
            cell.CellStyle = cellStyle1;
 
            cell = row.CreateCell(3);
            cell.SetCellValue("乒乓球");
            cell.CellStyle = cellStyle1;
 
            cell = row.CreateCell(4);
            cell.SetCellValue("跳水");
            cell.CellStyle = cellStyle1;
 
            cell = row.CreateCell(5);
            cell.SetCellValue("举重");
            cell.CellStyle = cellStyle1;
 
            cell = row.CreateCell(6);
            cell.SetCellValue("自由泳");
            cell.CellStyle = cellStyle1;
 
            cell = row.CreateCell(7);
            cell.SetCellValue("体操");
            cell.CellStyle = cellStyle1;
 
            cell = row.CreateCell(8);
            cell.SetCellValue("平均成绩");
            cell.CellStyle = cellStyle1;
 
            cell = row.CreateCell(9);
            cell.SetCellValue("名次");
            cell.CellStyle = cellStyle1;
 
            // 写入数据
            for (int i = 0; i 

//以下for循环实现对每个人的成绩进行排名
            for (int i = 0; i 
文章标题:用NPOI、C#操作Excel表格生成班级成绩单
文章链接:http://soscw.com/index.php/essay/70377.html