博客
关于我
NPOI之Excel——合并单元格、设置样式、输入公式
阅读量:793 次
发布时间:2023-02-17

本文共 1506 字,大约阅读时间需要 5 分钟。

NPOI Excel操作实例:常见操作演示

1. 创建空白工作簿及工作表

IWorkbook workbook = new HSSFWorkbook();ISheet sheet = workbook.CreateSheet();

2. 设置单元格样式

ICellStyle style = workbook.CreateCellStyle();style.Alignment = HorizontalAlignment.CENTER;IFont font = workbook.CreateFont();font.Boldweight = short.MaxValue;style.SetFont(font);

3. 设置单元格宽高

sheet.SetColumnWidth(0, 256 * 30); // 单元格宽度设置sheet.SetRowHeight(0, 20 * 30); // 单元格高度设置

4. 合并单元格

CellRangeAddress address = new CellRangeAddress(0, 0, 0, 10);sheet.AddMergedRegion(address);

5. 添加公式

ICell cell = sheet.CreateRow(1).CreateCell(0);cell.CellFormula = "HYPERLINK(\"路径\",\"显示文本\")";

6. 写入文件

using (FileStream fs = new FileStream("文件名.xlsx", FileMode.Create, FileAccess.Write)) {    workbook.Write(fs);}

7. 数字格式问题解决

sheet.CreateRow(1).CreateCell(2).SetCellValue(123);

8. 下拉列表实现

// 创建辅助表HSSFSheet sheet2 = workbook.CreateSheet();sheet2.CreateRow(0).CreateCell(0).SetCellValue("项A");sheet2.CreateRow(1).CreateCell(0).SetCellValue("项B");sheet2.CreateRow(2).CreateCell(0).SetCellValue("项C");// 创建名称引用HSSFName name = workbook.CreateName();name.Reference = "ShtDictionary!$A1:$A3";name.NameName = "dicRange";// 设置数据有效性CellRangeAddressList regions = new CellRangeAddressList(0, 65535, 0, 0);DVConstraint constraint = DVConstraint.CreateFormulaListConstraint("dicRange");HSSFDataValidation validation = new HSSFDataValidation(regions, constraint);sheet1.AddValidationData(validation);

9. 代码解析

  • 创建区域

    使用 CellRangeAddressList 定义单元格范围。

  • 创建约束

    通过 DVConstraint 实现下拉功能。

  • 添加数据有效性

    在目标单元格范围内添加数据有效性约束。

  • 转载地址:http://jejfk.baihongyu.com/

    你可能感兴趣的文章
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>