新之助meow

Kratos
专注于用户阅读体验的响应式博客主题
  1. 首页
  2. 未分类
  3. 正文

java poi XSSF 模式 添加行和单元格

2019年8月5日 887点热度 20人点赞 0条评论

一般操作excel 有两种方式,
一种是画好表格了,填固定的空,这种情况直接找到格子,填入就行了
另外一种是只画好表头了,但实际表内容的行数是不固定的,需要程序根据数据的实际行数来添加行。
这种加行在 XSSF模式下,直接写入单元格会报错。要先加行,再加单元格,再写入,才可以。

public class ExcelUtil {
    private XSSFWorkbook wb = null;

    public void OpenXLSXFile(String FilePath) throws IOException {
        InputStream ExcelFileToRead = new FileInputStream(FilePath);
        wb = new XSSFWorkbook(ExcelFileToRead);
    }

    public void CreateRow(String SheetName, int rownum) {
        Sheet ws = wb.getSheet(SheetName);
        ws.createRow(rownum);
    }

    public void SetCellValue(String SheetName, String CellName, double value) {
        Sheet ws = wb.getSheet(SheetName);
        CellReference cr = new CellReference(CellName);
        Row row = ws.getRow(cr.getRow());
        Cell cell = row.getCell(cr.getCol());
        if (cell == null) {
            cell = row.createCell(cr.getCol());
        }
        cell.setCellValue(value);
    }

    public void SetCellValue(String SheetName, String CellName, String Value) {
        Sheet ws = wb.getSheet(SheetName);
        CellReference cr = new CellReference(CellName);
        Row row = ws.getRow(cr.getRow());
        Cell cell = row.getCell(cr.getCol());
        if (cell == null) {
            cell = row.createCell(cr.getCol());
        }
        cell.setCellValue(Value);

    public void SaveAs(String FilePath) {
        FileOutputStream fileOut = null;
        try {
            fileOut = new FileOutputStream(FilePath);
            // 保存之前刷新公式
            XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
            wb.write(fileOut);
            fileOut.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

            try {
                wb.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                fileOut.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    }

参考
writing a new cell to a sheet apache poi
https://stackoverflow.com/questions/12754641/writing-a-new-cell-to-a-sheet-apache-poi
Java POI(第一讲):POI中HSSF用法大全
https://blog.51cto.com/zangyanan/1837229
How to insert a row between two rows in an existing excel with HSSF (Apache POI)
https://stackoverflow.com/questions/5785724/how-to-insert-a-row-between-two-rows-in-an-existing-excel-with-hssf-apache-poi
POI Excel 插入新的行,下面的行动态移动
https://www.cnblogs.com/zwdx/p/7878523.html
POI shiftRows方法
https://www.cnblogs.com/staticxy/p/6122336.html

标签: 暂无
最后更新:2019年8月5日

tjccn_wx@hotmail.com

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

归档

  • 2026 年 10 月
  • 2024 年 2 月
  • 2023 年 7 月
  • 2023 年 1 月
  • 2022 年 10 月
  • 2022 年 9 月
  • 2022 年 6 月
  • 2022 年 4 月
  • 2022 年 3 月
  • 2022 年 2 月
  • 2021 年 11 月
  • 2021 年 10 月
  • 2021 年 9 月
  • 2021 年 1 月
  • 2020 年 12 月
  • 2020 年 11 月
  • 2020 年 10 月
  • 2020 年 9 月
  • 2020 年 7 月
  • 2020 年 6 月
  • 2020 年 5 月
  • 2020 年 4 月
  • 2020 年 3 月
  • 2020 年 2 月
  • 2019 年 10 月
  • 2019 年 8 月
  • 2019 年 7 月
  • 2019 年 4 月
  • 2019 年 3 月
  • 2018 年 12 月
  • 2018 年 11 月
  • 2018 年 10 月
  • 2018 年 6 月
  • 2018 年 5 月
  • 2018 年 4 月
  • 2018 年 3 月
  • 2018 年 2 月
  • 2018 年 1 月
  • 2017 年 12 月
  • 2017 年 11 月
  • 2017 年 10 月
  • 2017 年 9 月
  • 2017 年 8 月
  • 2017 年 7 月
  • 2017 年 6 月
  • 2017 年 5 月
  • 2017 年 4 月
  • 2017 年 3 月
  • 2017 年 2 月
  • 2017 年 1 月
  • 2016 年 12 月
  • 2016 年 11 月
  • 2016 年 10 月
  • 2016 年 9 月
  • 2016 年 8 月
  • 2016 年 7 月
  • 2016 年 6 月
  • 2016 年 5 月
  • 2016 年 4 月
  • 2016 年 3 月
  • 2015 年 12 月
  • 2015 年 8 月

分类

  • android
  • mysql
  • nethunter
  • proxmark
  • 未分类

COPYRIGHT © 2026 新之助meow. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang