国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

使用NPOI做Excel簡單報表

這篇具有很好參考價值的文章主要介紹了使用NPOI做Excel簡單報表。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。


使用NPOI做Excel簡單報表
前言

前面介紹了NPOI單元格樣式的使用、單元格合并,以及NPOI提供的顏色?,F(xiàn)在用上前面的一些知識點,做一個測試結(jié)果表格。

1、 介紹NPOI 的顏色卡、名稱以及索引 https://editor.csdn.net/md/?articleId=130265415
2、 NPOI的CellStyle單元格樣式 https://editor.csdn.net/md/?articleId=130245869
3、 將DataTable中的數(shù)據(jù)保存到Excel (二) 使用NPOI https://editor.csdn.net/md/?articleId=130225379

初版表格,單元格的合并

先上一個簡單版本的,主要內(nèi)容在于跨行和跨列單元格的合并。涉及的主要函數(shù)是Sheet的AddMergedRegion 以及NPOI.SS.Util.CellRangeAddress這兩個函數(shù)。
這里使用的NPOI版本是 2.0.6.0, 框架 .NET Framework 4.0。

using NPOI.HSSF.Util;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
        /// <summary>
        /// 合并單元格
        /// </summary>
        void MergeCell()
        {
            //創(chuàng)建工作簿對象
            HSSFWorkbook workBook = new HSSFWorkbook();

            //創(chuàng)建一個sheet
            ISheet sheet = workBook.CreateSheet("MergeTable");

            IRow row;
            ICell cell;

            //行索引
            int rowIndex = 0;

            //標(biāo)題
            row = sheet.CreateRow(rowIndex);
            cell = row.CreateCell(0);
            cell.SetCellValue("測試報告");
            for (int i=1; i<8; i++)
            {
                cell = row.CreateCell(i);
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 7));
            rowIndex++;


            row = sheet.CreateRow(rowIndex);
            cell = row.CreateCell(0);
            cell.SetCellValue("機型:單相   功率:2.2kw   電流:1.8A  測試時間:2023/04/20  耗時:3分鐘");
            for (int i = 1; i < 8; i++)
            {
                cell = row.CreateCell(i);
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 7));
            rowIndex++;

            //記錄起始項
            int startIndex = rowIndex;
            //第一個小表格的標(biāo)題
            row = sheet.CreateRow(rowIndex);
            cell = row.CreateCell(0);
            cell.SetCellValue("品質(zhì)測試");
            List<string> valueList = new List<string>()
            {
                "序號","測試項", "產(chǎn)品", "合格數(shù)據(jù)", "測試結(jié)果"
            };
            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i+1);
                cell.SetCellValue (valueList[i]);
            }
            rowIndex++;


            //第一條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            valueList.Clear();
            valueList.AddRange(new string[]
            {
              "", "1", "通訊測試","成功", "", "/", "", "合格"
            });
            
            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
            rowIndex++;

            //第二條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            valueList.Clear();
            valueList.AddRange(new string[]
            {
               "", "2", "溫度測試","25℃", "", "23-30℃", "", "合格"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
            rowIndex++;


            //第三條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            valueList.Clear();
            valueList.AddRange(new string[]
            {
                "", "3", "故障測試","E001", "", "/", "", "不合格"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));

            //品質(zhì)測試 要跨行
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(startIndex, rowIndex, 0, 0));

            rowIndex++;



            startIndex = rowIndex;
            //第二個 小表格
            row = sheet.CreateRow(rowIndex);
            cell = row.CreateCell(0);
            cell.SetCellValue("性能測試");
            valueList.Clear();
            valueList.AddRange(new string[]
            {
                "序號","測試項", "產(chǎn)品", "對比項", "最小值" ,"最大值", "測試結(jié)果"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i+1);
                cell.SetCellValue(valueList[i]);
            }
            rowIndex++;

            //第一條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            valueList.Clear();
            valueList.AddRange(new string[]
            {
              "", "1", "版本檢測","112", "112", "", "", "合格"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
            }
            rowIndex++;


            //第二條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            valueList.Clear();
            valueList.AddRange(new string[]
            {
               "","2", "帶載檢測","10.4/10.2", "", "10.1", "10.6", "合格"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
            }
            //品質(zhì)測試 要跨行
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(startIndex, rowIndex, 0, 0));
            rowIndex++;

            row = sheet.CreateRow(rowIndex);
            cell = row.CreateCell(0);
            cell.SetCellValue("測試總結(jié)果:");
            for (int i = 1; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i+1);
                if( i == 3)
                {
                    cell.SetCellValue("不合格");
                }
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 2));
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 7));

        
            using (FileStream file = new FileStream(@"測試結(jié)果.xls", FileMode.Create))
            {
                workBook.Write(file);
                file.Close();
            }
        }

效果圖:
使用NPOI做Excel簡單報表

進階表格,單元格美化

設(shè)置行高列寬,對單元格的對齊方式、邊框、字體大小IFont、顏色進行設(shè)置美化, 使用的是ICellStyle.
先看一下最終的效果圖,由結(jié)果分析向上分析。
使用NPOI做Excel簡單報表

測試報告
行高30,居中顯示,字體: 黑體,16號,加粗, 設(shè)置單元格邊框。

            //大標(biāo)題 黑體 16號 加邊框  居中顯示 行高30
            string fontName = "黑體";
            ICellStyle totalHeader = workBook.CreateCellStyle();
            //垂直居中對齊
            totalHeader.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            totalHeader.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            totalHeader.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            totalHeader.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            totalHeader.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            totalHeader.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;

            //字體
            IFont totalFont = workBook.CreateFont();
            totalFont.FontName = fontName;
            totalFont.FontHeightInPoints = 16;
            totalFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
            totalHeader.SetFont(totalFont);

第二行 機型
行高20, 居中顯示, 字體 :微軟雅黑12號 設(shè)置單元格邊框。

           //中間行樣式 機型 行高20 居中顯示 字體 微軟雅黑12號 加邊框
            ICellStyle lineStyle = workBook.CreateCellStyle();
            //垂直居中對齊
            lineStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            lineStyle.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            lineStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            lineStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            lineStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            lineStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            //字體大小
            IFont midFont = workBook.CreateFont();
            midFont.FontHeightInPoints = 12;
            midFont.FontName = "微軟雅黑";
            lineStyle.SetFont(midFont);

左側(cè)跨單元格 品質(zhì)(性能)測試 總結(jié)果
居中顯示, 字體微軟雅黑12號加粗 ,設(shè)置單元格邊框。在單元格合并后再設(shè)置樣式。

          //左側(cè)跨行單元格的樣式 居中顯示 字體微軟雅黑12號加粗 設(shè)置單元格邊框
            ICellStyle leftCellStyle = workBook.CreateCellStyle();
            //對齊
            leftCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            leftCellStyle.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            leftCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            leftCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            leftCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            leftCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            //字體大小
            IFont leftFont = workBook.CreateFont();
            leftFont.FontHeightInPoints = 12;
            leftFont.FontName = "微軟雅黑";
            leftFont.Boldweight = (short)(short)NPOI.SS.UserModel.FontBoldWeight.Bold; ;
            leftCellStyle.SetFont(leftFont);

帶背景小標(biāo)題
行高20,居中顯示,字體字體微軟雅黑12號,設(shè)置單元格邊框,設(shè)置背景色。
要想設(shè)置的背景色起作用,必須要設(shè)置 midTitleStyle.FillPattern = FillPattern.SolidForeground;這句

          //小標(biāo)題的樣式 行高20,居中顯示,字體字體微軟雅黑12號,設(shè)置單元格邊框,設(shè)置背景色
            ICellStyle midTitleStyle = workBook.CreateCellStyle();
            //對齊
            midTitleStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            midTitleStyle.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            midTitleStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            midTitleStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            midTitleStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            midTitleStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            //需要設(shè)置圖例,顏色才會起作用
            midTitleStyle.FillPattern = FillPattern.SolidForeground;

            //背景顏色
            midTitleStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
            midTitleStyle.SetFont(midFont);

中間的測試內(nèi)容
行高15,左對齊,設(shè)置單元格邊框。

            //內(nèi)容的樣式  行高15 左對齊 設(shè)置單元格邊框
            ICellStyle contentStyle = workBook.CreateCellStyle();
            //對齊
            contentStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
            contentStyle.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            contentStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            contentStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            contentStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            contentStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;

全部代碼

        /// <summary>
        /// 合并單元格
        /// </summary>
        void MergeCell()
        {
            //創(chuàng)建工作簿對象
            HSSFWorkbook workBook = new HSSFWorkbook();

            //創(chuàng)建一個sheet
            ISheet sheet = workBook.CreateSheet("MergeTable");

            //設(shè)置列寬
            sheet.SetColumnWidth(0, 15 * 256);
            sheet.SetColumnWidth(1, 10 * 256);
            sheet.SetColumnWidth(2, 10 * 256);
            sheet.SetColumnWidth(3, 10 * 256);
            sheet.SetColumnWidth(4, 10 * 256);
            sheet.SetColumnWidth(5, 10 * 256);
            sheet.SetColumnWidth(7, 12 * 256);


            IRow row;
            ICell cell;

            //大標(biāo)題 黑體 16號 加邊框  居中顯示 行高30
            string fontName = "黑體";
            ICellStyle totalHeader = workBook.CreateCellStyle();
            //垂直居中對齊
            totalHeader.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            totalHeader.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            totalHeader.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            totalHeader.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            totalHeader.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            totalHeader.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;

            //字體
            IFont totalFont = workBook.CreateFont();
            totalFont.FontName = fontName;
            totalFont.FontHeightInPoints = 16;
            totalFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
            totalHeader.SetFont(totalFont);


            //行索引
            int rowIndex = 0;

            //標(biāo)題
            row = sheet.CreateRow(rowIndex);
            //設(shè)置行高
            row.HeightInPoints = 30;
            cell = row.CreateCell(0);
            cell.SetCellValue("測試報告");
            //設(shè)置單元格樣式
            cell.CellStyle = totalHeader;
            for (int i=1; i<8; i++)
            {
                cell = row.CreateCell(i);
                //設(shè)置單元格樣式
                cell.CellStyle = totalHeader;

            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 7));
            rowIndex++;


            //中間行樣式 機型 行高20 居中顯示 字體 微軟雅黑12號 加邊框
            ICellStyle lineStyle = workBook.CreateCellStyle();
            //垂直居中對齊
            lineStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            lineStyle.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            lineStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            lineStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            lineStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            lineStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            //字體大小
            IFont midFont = workBook.CreateFont();
            midFont.FontHeightInPoints = 12;
            midFont.FontName = "微軟雅黑";
            lineStyle.SetFont(midFont);

            row = sheet.CreateRow(rowIndex);
            //行高20
            row.HeightInPoints = 20;
            cell = row.CreateCell(0);
            cell.SetCellValue("機型:單相   功率:2.2kw   電流:1.8A  測試時間:2023/04/20  耗時:3分鐘");
            //設(shè)置樣式
            cell.CellStyle = lineStyle;
            for (int i = 1; i < 8; i++)
            {
                cell = row.CreateCell(i);
                cell.CellStyle = lineStyle;
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 7));
            rowIndex++;


            //小標(biāo)題的樣式 行高20,居中顯示,字體字體微軟雅黑12號,設(shè)置單元格邊框,設(shè)置背景色
            ICellStyle midTitleStyle = workBook.CreateCellStyle();
            //對齊
            midTitleStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            midTitleStyle.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            midTitleStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            midTitleStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            midTitleStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            midTitleStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            //需要設(shè)置圖例,顏色才會起作用
            midTitleStyle.FillPattern = FillPattern.SolidForeground;

            //背景顏色
            midTitleStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
            midTitleStyle.SetFont(midFont);
            


            //內(nèi)容的樣式  行高15 左對齊 設(shè)置單元格邊框
            ICellStyle contentStyle = workBook.CreateCellStyle();
            //對齊
            contentStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
            contentStyle.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            contentStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            contentStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            contentStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            contentStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;


            //左側(cè)跨行單元格的樣式 居中顯示 字體微軟雅黑12號加粗 設(shè)置單元格邊框
            ICellStyle leftCellStyle = workBook.CreateCellStyle();
            //對齊
            leftCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            leftCellStyle.VerticalAlignment = VerticalAlignment.Center;
            //邊框
            leftCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            leftCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            leftCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            leftCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            //字體大小
            IFont leftFont = workBook.CreateFont();
            leftFont.FontHeightInPoints = 12;
            leftFont.FontName = "微軟雅黑";
            leftFont.Boldweight = (short)(short)NPOI.SS.UserModel.FontBoldWeight.Bold; ;
            leftCellStyle.SetFont(leftFont);


            //記錄起始項
            int startIndex = rowIndex;
            //第一個小表格的標(biāo)題
            row = sheet.CreateRow(rowIndex);
            //行高
            row.HeightInPoints = 20;
            cell = row.CreateCell(0);
            cell.SetCellValue("品質(zhì)測試");
            List<string> valueList = new List<string>()
            {
                "序號","測試項", "產(chǎn)品", "", "合格數(shù)據(jù)","", "測試結(jié)果"
            };
            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i+1);
                cell.SetCellValue (valueList[i]);
                //樣式
                cell.CellStyle = midTitleStyle;
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
            rowIndex++;


            //第一條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            row.HeightInPoints = 15;
            valueList.Clear();
            valueList.AddRange(new string[]
            {
              "", "1", "通訊測試","成功", "", "/", "", "合格"
            });
            
            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
                //內(nèi)容樣式
                cell.CellStyle = contentStyle;
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
            rowIndex++;

            //第二條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            row.HeightInPoints = 15;
            valueList.Clear();
            valueList.AddRange(new string[]
            {
               "", "2", "溫度測試","25℃", "", "23-30℃", "", "合格"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
                //內(nèi)容樣式
                cell.CellStyle = contentStyle;
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));
            rowIndex++;


            //第三條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            row.HeightInPoints = 15;
            valueList.Clear();
            valueList.AddRange(new string[]
            {
                "", "3", "故障測試","E001", "", "/", "", "不合格"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
                //內(nèi)容樣式
                cell.CellStyle = contentStyle;
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 4));
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 5, 6));

            //品質(zhì)測試 要跨行
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(startIndex, rowIndex, 0, 0));
            sheet.GetRow(startIndex).GetCell(0).CellStyle = leftCellStyle;
            rowIndex++;


            startIndex = rowIndex;
            //第二個 小表格
            row = sheet.CreateRow(rowIndex);
            //行高
            row.HeightInPoints = 20;
            cell = row.CreateCell(0);
            cell.SetCellValue("性能測試");
            valueList.Clear();
            valueList.AddRange(new string[]
            {
                "序號","測試項", "產(chǎn)品", "對比項", "最小值" ,"最大值", "測試結(jié)果"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i+1);
                cell.SetCellValue(valueList[i]);
                cell.CellStyle = midTitleStyle;
            }
            rowIndex++;

            //第一條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            row.HeightInPoints = 15;
            valueList.Clear();
            valueList.AddRange(new string[]
            {
              "", "1", "版本檢測","112", "112", "", "", "合格"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
                //內(nèi)容樣式
                cell.CellStyle = contentStyle;
            }
            rowIndex++;


            //第二條測試結(jié)果
            row = sheet.CreateRow(rowIndex);
            row.HeightInPoints = 15;
            valueList.Clear();
            valueList.AddRange(new string[]
            {
               "","2", "帶載檢測","10.4/10.2", "", "10.1", "10.6", "合格"
            });

            for (int i = 0; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                cell.SetCellValue(valueList[i]);
                //內(nèi)容樣式
                cell.CellStyle = contentStyle;
            }
            //性能測試 要跨行
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(startIndex, rowIndex, 0, 0));
            sheet.GetRow(startIndex).GetCell(0).CellStyle = leftCellStyle;

            rowIndex++;

            row = sheet.CreateRow(rowIndex);
            row.HeightInPoints = 20;
            cell = row.CreateCell(0);
            cell.SetCellValue("測試總結(jié)果:");
            cell.CellStyle = leftCellStyle;
            for (int i = 1; i < valueList.Count; i++)
            {
                cell = row.CreateCell(i);
                if( i == 3)
                {
                    cell.SetCellValue("不合格");
                }
                cell.CellStyle = leftCellStyle;
            }
            //單元格合并 合并列
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 2));
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 3, 7));
            sheet.GetRow(rowIndex).GetCell(0).CellStyle = leftCellStyle;
            sheet.GetRow(rowIndex).GetCell(1).CellStyle = leftCellStyle;


            using (FileStream file = new FileStream(@"測試結(jié)果.xls", FileMode.Create))
            {
                workBook.Write(file);
                file.Close();
            }
        }

小結(jié)

1 單元格合并 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowIndex, rowIndex, 0, 2));
2 設(shè)置行高 row.HeightInPoints = 20;
3 設(shè)置列寬 sheet.SetColumnWidth(0, 15 * 256);
4 樣式 ICellStyle leftCellStyle = workBook.CreateCellStyle();
5 字體 IFont font = workBook.CreateFont();文章來源地址http://www.zghlxwxcb.cn/news/detail-421737.html

到了這里,關(guān)于使用NPOI做Excel簡單報表的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • C#【必備技能篇】使用NPOI實現(xiàn)對excel的讀取和寫入

    C#【必備技能篇】使用NPOI實現(xiàn)對excel的讀取和寫入

    依次執(zhí)行下圖中的1-6按鈕 , 可以通過查看程序文件夾中的excel文件來加深理解。 鏈接:https://pan.baidu.com/s/19PgQMhCKviw9aBAjmJHSww 提取碼:2omi 需要在源碼中增加如下引用。相應(yīng)的dll已更新到 5 的下載地址中。

    2023年04月13日
    瀏覽(25)
  • Unity 使用NPOI,模板替換Excel中的關(guān)鍵字(針對.xlsx)

    Unity 使用NPOI,模板替換Excel中的關(guān)鍵字(針對.xlsx)

    Unity 使用NPOI,模板替換Excel中的(針對.xlsx) 需求:項目中要用到生成Excel來打印文件,只需要替換其中的值,保留原模板,生成新的Excel 第一步:在unity中導(dǎo)入一下的dll 新建一個Plugin的文件夾,把dll全部放進去 以上選中的這些文件在unity的安裝目錄下 UnityEditorData

    2023年04月24日
    瀏覽(23)
  • 【工具插件類教學(xué)】NPOI插件使用Excel表格的導(dǎo)入和導(dǎo)出(包含圖片)

    目錄 一.導(dǎo)入Excel?解析讀取 1.選擇導(dǎo)入的目標(biāo)文件 2.解析讀取導(dǎo)入的文件

    2024年01月16日
    瀏覽(33)
  • 使用SpringBoot+React搭建一個Excel報表平臺

    使用SpringBoot+React搭建一個Excel報表平臺

    摘要:本文由葡萄城技術(shù)團隊于CSDN原創(chuàng)并首發(fā)。轉(zhuǎn)載請注明出處:葡萄城官網(wǎng),葡萄城為開發(fā)者提供專業(yè)的開發(fā)工具、解決方案和服務(wù),賦能開發(fā)者。 Excel報表平臺是一款功能強大、操作簡單的系統(tǒng)平臺,可以幫助用戶上傳、編輯和分析報表數(shù)據(jù),實現(xiàn)數(shù)據(jù)可視化。 本文所

    2024年02月16日
    瀏覽(18)
  • NPOI操作Excel

    名詞介紹: 整個Excel:工作薄 Sheet頁:頁簽,一個工作簿中可以包含多個Sheet頁 表格:對應(yīng)一個Sheet 行、列、單元格 C#中的常規(guī)操作: 導(dǎo)出一個Excel:其實就是要生成一個Excel文件,Excel文件對應(yīng)的文件流 導(dǎo)入一個Excel:讀取一個文件,讀取文件流,需要從文件流中讀取我們需要

    2024年02月12日
    瀏覽(18)
  • .Net NPOI Excel 導(dǎo)出

    .Net NPOI Excel 導(dǎo)出

    最終效果圖 ? ? ?環(huán)境:Revit,WPF,NPOI 2.5.6,.Net Framework 4.7.2 右擊項目引用,選擇 \\\"管理NuGet程序包\\\",在瀏覽搜索 NPOI ,選擇版本(我這里是2.5.6),安裝 ? ? ? 安裝成功后,引用里會出現(xiàn)這四個引用(NPOI,NPOI.OOXML,NPOI.OpenXml4Net,NPOI.OpenXmlFormats) 在NuGet程序包里會多出來這

    2024年02月07日
    瀏覽(26)
  • NPOI 讀取和寫入Excel

    在C#中使用NPOI庫讀取和寫入Excel文件,你需要先下載并安裝NPOI庫。你可以在NuGet管理器中搜索NPOI并進行安裝。 以下是一個使用NPOI庫進行Excel文件讀取和寫入的示例: 讀取Excel文件: 寫入Excel文件: 請注意,在使用NPOI時,記得引入必要的命名空間: NPOI.SS.UserModel 和 NPOI.XSSF.

    2024年02月12日
    瀏覽(39)
  • C# NPOI操作Excel匯總

    C# NPOI操作Excel匯總

    C#操作Excel有多種方法,如通過數(shù)據(jù)庫的方式來讀寫Excel的OleDb方式,但是OleDb方式需要安裝微軟office,還可以通過COM組件方式操作Excel,也需要安裝微軟Excel。如果不想安裝微軟辦公套餐可以使用ClosedXML、EPPlus、NPOI。本文主要是介紹NPOI的常用使用方法。 Apache POI是Apache軟件基金

    2024年02月12日
    瀏覽(19)
  • 【微信小程序】導(dǎo)出 Excel 報表并分享,使用xlsx庫生成 Excel,使用echars插入圖表、使用pdfmake導(dǎo)出為PDF文件

    要在微信小程序中導(dǎo)出 Excel 報表并分享,可以使用第三方庫 xlsx 來生成 Excel 文件,并使用 wx.saveFile 方法將文件保存到本地,然后使用 wx.shareFile 方法來分享文件。 以下是一個示例代碼,演示如何在微信小程序中導(dǎo)出 Excel 報表并分享: 首先,安裝依賴庫 xlsx ,可以使用 npm

    2024年02月13日
    瀏覽(25)
  • .NET Core NPOI導(dǎo)出復(fù)雜Excel

    .NET Core NPOI導(dǎo)出復(fù)雜Excel

    NPOI GitHub源碼地址: GitHub - tonyqus/npoi: a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop. 版本說明: NPOI 2.4.1 (注意不同版本可能使用的姿勢有點小差別) 程序包管理器控制臺輸入一下命令安裝: 1 Install-Package NPOI -Version 2.4.1 通過NuGet管理解決方

    2024年02月03日
    瀏覽(17)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包