Apache POI实例

简介: Apache POI实例

首先引入apache poi的依赖

<dependency>  
    <groupId>org.apache.poi</groupId>  
    <artifactId>poi</artifactId>  
    <version>3.8</version>  
</dependency>

示例一:在桌面上生成一个Excel文件

public static void createExcel() throws IOException{
    // 获取桌面路径
    FileSystemView fsv = FileSystemView.getFileSystemView();
    String desktop = fsv.getHomeDirectory().getPath();
    String filePath = desktop + "/template.xls";
    
    File file = new File(filePath);
    OutputStream outputStream = new FileOutputStream(file);
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Sheet1");
    HSSFRow row = sheet.createRow(0);
    row.createCell(0).setCellValue("id");
    row.createCell(1).setCellValue("订单号");
    row.createCell(2).setCellValue("下单时间");
    row.createCell(3).setCellValue("个数");
    row.createCell(4).setCellValue("单价");
    row.createCell(5).setCellValue("订单金额");
    row.setHeightInPoints(30); // 设置行的高度
    
    HSSFRow row1 = sheet.createRow(1);
    row1.createCell(0).setCellValue("1");
    row1.createCell(1).setCellValue("NO00001");
    
    // 日期格式化
    HSSFCellStyle cellStyle2 = workbook.createCellStyle();
    HSSFCreationHelper creationHelper = workbook.getCreationHelper();
    cellStyle2.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
    sheet.setColumnWidth(2, 20 * 256); // 设置列的宽度
    
    HSSFCell cell2 = row1.createCell(2);
    cell2.setCellStyle(cellStyle2);
    cell2.setCellValue(new Date());
    
    row1.createCell(3).setCellValue(2);
    
    
    // 保留两位小数
    HSSFCellStyle cellStyle3 = workbook.createCellStyle();
    cellStyle3.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
    HSSFCell cell4 = row1.createCell(4);
    cell4.setCellStyle(cellStyle3);
    cell4.setCellValue(29.5);
    
    
    // 货币格式化
    HSSFCellStyle cellStyle4 = workbook.createCellStyle();
    HSSFFont font = workbook.createFont();
    font.setFontName("华文行楷");
    font.setFontHeightInPoints((short)15);
    font.setColor(HSSFColor.RED.index);
    cellStyle4.setFont(font);
    
    HSSFCell cell5 = row1.createCell(5);
    cell5.setCellFormula("D2*E2");  // 设置计算公式
    
    // 获取计算公式的值
    HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(workbook);
    cell5 = e.evaluateInCell(cell5);
    System.out.println(cell5.getNumericCellValue());

    
    workbook.setActiveSheet(0);
    workbook.write(outputStream);
    outputStream.close();
}

示例2:读取Excel,解析数据

public static void readExcel() throws IOException{
    FileSystemView fsv = FileSystemView.getFileSystemView();
    String desktop = fsv.getHomeDirectory().getPath();
    String filePath = desktop + "/template.xls";
    
    FileInputStream fileInputStream = new FileInputStream(filePath);
    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
    POIFSFileSystem fileSystem = new POIFSFileSystem(bufferedInputStream);
    HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
    HSSFSheet sheet = workbook.getSheet("Sheet1");
    
    int lastRowIndex = sheet.getLastRowNum();
    System.out.println(lastRowIndex);
    for (int i = 0; i <= lastRowIndex; i++) {
        HSSFRow row = sheet.getRow(i);
        if (row == null) { break; }
        
        short lastCellNum = row.getLastCellNum();
        for (int j = 0; j < lastCellNum; j++) {
            String cellValue = row.getCell(j).getStringCellValue();
            System.out.println(cellValue);
        }
    }
    
    
    bufferedInputStream.close();
}
目录
相关文章
|
18天前
|
Java Apache
Apache POI java对excel表格进行操作(读、写) 有代码!!!
文章提供了使用Apache POI库在Java中创建和读取Excel文件的详细代码示例,包括写入数据到Excel和从Excel读取数据的方法。
25 0
|
2月前
|
easyexcel Java API
Apache POI、EasyPoi、EasyExcel 三种区别,如何选择
Apache POI、EasyPoi、EasyExcel 三种区别,如何选择
448 0
|
3月前
|
easyexcel Java Apache
EasyExcel导入的时候报错Caused by: java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/File
EasyExcel导入的时候报错Caused by: java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/File
285 0
|
4月前
|
easyexcel Java API
Apache POI与easyExcel:Excel文件导入导出的技术深度分析
Apache POI与easyExcel:Excel文件导入导出的技术深度分析
|
4月前
|
Java API Apache
Apache POI(Poor Obfuscation Implementation
Apache POI(Poor Obfuscation Implementation
74 0
|
存储 Java BI
探索Apache POI库:强大的Excel和Word文档处理工具
在企业应用和数据处理中,Excel和Word文档是常见的数据交换和存储格式。然而,处理和操作这些文档可能是一项繁琐的任务。Apache POI库作为一款强大的文档处理工具,可以帮助我们更轻松地进行Excel和Word文档的读写、编辑和生成。本文将深入探讨Apache POI库的基本概念、特点,以及如何在实际应用中使用它进行文档处理。
741 0
|
5月前
|
XML 存储 Java
Apache POI 实现用Java操作Excel完成读写操作
Apache POI 实现用Java操作Excel完成读写操作
|
11月前
|
Java Apache
java word转html 报错org/apache/poi/xwpf/usermodel/IRunBody
java word转html 报错org/apache/poi/xwpf/usermodel/IRunBody
232 0
|
XML Java API
Apache POI详解及Word文档读取示例
apache poi资料详解,包括内部jar包依赖关系,及与使用文档的对应关系
1357 0
|
Java API Apache
Apache POI 读写 Excel 教程
Apache POI 读写 Excel 教程
285 0

推荐镜像

更多