当C#遇上NPOI导出Excel

用NPOI可以实现Excel的导入与导出,下面我先介绍将c# 的datagrideview中数据导出到Excel功能(将Excel导入datagrideview后续补充。)

第一步:在你的项目文件夹中可以建立一个名为lib的文件夹,并将插件文件放入里面

第二步:将上图所有.dll格式的文件在项目中所需此功能的位置进行添加引用(我在U层引用的)

第三步:有了插件的支持然后就可以写代码了。我把它封装到类里边了,可以将表格列首内容参数化,通过传参灵活使用此方法。

class Excel
    {
        
        public void ExcelOut(List<ChargeWorkInfo>list)
        {
            
            DataGridView dgv = new DataGridView();

            #region 创建Excel
            //进行Excel生成创建操作
            //1.创建workbook
            HSSFWorkbook workbook = new HSSFWorkbook();
            //2.创建sheet
            HSSFSheet sheet = workbook.CreateSheet("工作簿");//文件名
            //3.创建row
            HSSFRow row = sheet.CreateRow(0);
            //4.创建cell
            HSSFCell cell0 = row.CreateCell(0);
            cell0.SetCellValue("工作记录");//表头
            //5.设置合并单元格
            sheet.AddMergedRegion(new NPOI.HSSF.Util.Region(0, 0, 0, 3));
            //6.设置单元格居中
            HSSFCellStyle styleTitle = workbook.CreateCellStyle();
            styleTitle.Alignment = 2;
            cell0.CellStyle = styleTitle;
            //6.1设置字体
            HSSFFont fontTitle = workbook.CreateFont();
            fontTitle.FontHeightInPoints = 15;//样式设置在单元格上
            styleTitle.SetFont(fontTitle);//字体设置在样式上
            //7.创建标题行
            //7.1创建行
            HSSFRow rowTitle = sheet.CreateRow(1);
            //7.2创建单元格
            HSSFCell cellTitle0 = rowTitle.CreateCell(0);
            cellTitle0.SetCellValue("ID");//填入内容
            cellTitle0.CellStyle = styleTitle;//内容居中
            HSSFCell cellTitle1 = rowTitle.CreateCell(1);
            cellTitle1.SetCellValue("姓名");
            cellTitle1.CellStyle = styleTitle;
            HSSFCell cellTitle2 = rowTitle.CreateCell(2);
            cellTitle2.SetCellValue("用户类型");
            cellTitle2.CellStyle = styleTitle;
            HSSFCell cellTitle3 = rowTitle.CreateCell(3);
            cellTitle3.SetCellValue("上线时间");
            cellTitle3.CellStyle = styleTitle;
            HSSFCell cellTitle4 = rowTitle.CreateCell(4);
            cellTitle4.SetCellValue("下线时间");
            cellTitle4.CellStyle = styleTitle;
            HSSFCell cellTitle5 = rowTitle.CreateCell(5);
            cellTitle5.SetCellValue("工作时长(min)");
            cellTitle5.CellStyle = styleTitle;
            #endregion

            #region 向Excel中导入数据
            //8.1遍历集合,创建正文数据
            int rowIndex = 2;
            foreach (var mi in list)
            {
                //8.2创建行
                HSSFRow rowData = sheet.CreateRow(rowIndex++);
                //8.3创建数据单元格,绑定数据
                HSSFCell cellData0 = rowData.CreateCell(0);
                cellData0.SetCellValue(mi.WID);
                HSSFCell cellData1 = rowData.CreateCell(1);
                cellData1.SetCellValue(mi.WName);
                HSSFCell cellData2 = rowData.CreateCell(2);
                cellData2.SetCellValue(mi.WType);
                HSSFCell cellData3 = rowData.CreateCell(3);
                cellData3.SetCellValue(mi.WOnTime);
                HSSFCell cellData4 = rowData.CreateCell(4);
                cellData4.SetCellValue(mi.WOffTime);
                HSSFCell cellData5 = rowData.CreateCell(5);
                cellData5.SetCellValue(mi.WorkTime);

            }
            #endregion

            //保存工作本
            FileStream stream = new FileStream(@"C:\Users\卡哇牛\Desktop\工作簿1.xls", FileMode.Create);
            workbook.Write(stream);
            stream.Close();
            stream.Dispose();
        }
    }
发布了233 篇原创文章 · 获赞 22 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41026669/article/details/94330915