NPOI 生成Excel (單元格合並、設置單元格樣式:字段,顏色、設置單元格為下拉框並限制輸入值、設置單元格只能輸入數字等)


NPIO源碼地址:https://github.com/tonyqus/npoi

NPIO使用參考:源碼中的 NPOITest項目

下面代碼包括:

1、包含多個Sheet的Excel

2、單元格合並

3、設置單元格樣式:字段,顏色

4、設置單元格為下拉框並限制輸入值

5、設置單元格只能輸入數字

 //
        // GET: /Excel/
        public ActionResult Write()
        {
            var workbook = new HSSFWorkbook();//從流內容創建Workbook對象
            ISheet sheet = ((HSSFWorkbook)workbook).CreateSheet("sheetOne");//創建工作表

            IRow row = sheet.CreateRow(0);//在工作表中添加一行
            ICell cell = row.CreateCell(1);//在行中添加一列
            cell.SetCellValue("test");//設置列的內容
            setCellStyle(workbook, cell);
            mergeCell(sheet, 0, 0, 1, 4);

            sheet = ((HSSFWorkbook)workbook).CreateSheet("sheet2");//創建工作表
            setCellDropdownlist(sheet);
            setCellInputNumber(sheet);

            string filePath = Server.MapPath("~/ExportFiles/test.xls");
            FileStream fs = new FileStream(filePath, FileMode.Create);
            workbook.Write(fs);
            fs.Close();

            return null;
        }

        /// <summary>
        /// 設置單元格為下拉框並限制輸入值
        /// </summary>
        /// <param name="sheet"></param>
        private void setCellDropdownlist(ISheet sheet)
        {
            //設置生成下拉框的行和列
            var cellRegions = new CellRangeAddressList(0, 65535, 0, 0);

            //設置 下拉框內容
            DVConstraint constraint = DVConstraint.CreateExplicitListConstraint(
                new string[] { "itemA", "itemB", "itemC" });

            //綁定下拉框和作用區域,並設置錯誤提示信息
            HSSFDataValidation dataValidate = new HSSFDataValidation(cellRegions, constraint);
            dataValidate.CreateErrorBox("輸入不合法", "請輸入下拉列表中的值。");
            dataValidate.ShowPromptBox = true;

            sheet.AddValidationData(dataValidate);
        }

        /// <summary>
        /// 設置單元格只能輸入數字
        /// </summary>
        /// <param name="sheet"></param>
        private void setCellInputNumber(ISheet sheet)
        {
            //設置生成下拉框的行和列
            var cellRegions = new CellRangeAddressList(0, 65535, 1, 1);

            //第二個參數int comparisonOperator  參考源碼獲取
            //https://github.com/tonyqus/npoi
            //NPOITest項目
            DVConstraint constraint = DVConstraint.CreateNumericConstraint(
                ValidationType.INTEGER, OperatorType.BETWEEN, "0", "100");
            
            HSSFDataValidation dataValidate = new HSSFDataValidation(cellRegions, constraint);
            dataValidate.CreateErrorBox("輸入不合法", "請輸入1~100的數字。");
            //dataValidate.PromptBoxTitle = "ErrorInput";

            sheet.AddValidationData(dataValidate);
        }

        /// <summary>
        /// 合並單元格
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="firstRow"></param>
        /// <param name="lastRow"></param>
        /// <param name="firstCell"></param>
        /// <param name="lastCell"></param>
        private void mergeCell(ISheet sheet, int firstRow, int lastRow, int firstCell, int lastCell)
        {
            sheet.AddMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCell, lastCell));//2.0使用 2.0以下為Region
        }

        /// <summary>
        /// 設置單元格樣式
        /// </summary>
        /// <param name="workbook"></param>
        /// <param name="cell"></param>
        private void setCellStyle(HSSFWorkbook workbook, ICell cell)
        {
            HSSFCellStyle fCellStyle = (HSSFCellStyle)workbook.CreateCellStyle();
            HSSFFont ffont = (HSSFFont)workbook.CreateFont();
            ffont.FontHeight = 20 * 20;
            ffont.FontName = "宋體";
            ffont.Color = HSSFColor.Red.Index;
            fCellStyle.SetFont(ffont);

            fCellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//垂直對齊
            fCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//水平對齊
            cell.CellStyle = fCellStyle;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM