使用 Apache POI 读取/写入不同的 Microsoft Office 文件格式

Read / Write different Microsoft Office file formats using Apache POI

如何使用 Apache POI 在 Microsoft Excel 电子表格上执行不同的功能?

我正在尝试从我的 Spring MVC 应用生成和更新 Excel 文件(来自数据库的数据)..

谢谢

Include apache poi jar file

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

To read an excel file

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//..
FileInputStream file = new FileInputStream(new File("C:\test.xls"));

//Get the workbook instance for XLS file 
HSSFWorkbook workbook = new HSSFWorkbook(file);

//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);

//Get iterator to all the rows in current sheet
Iterator<Row> rowIterator = sheet.iterator();

//Get iterator to all cells of current row
Iterator<Cell> cellIterator = row.cellIterator();

我们在上面的代码片段中使用的 classes,HSSFWorkbookHSSFSheet 适用于 .xls 格式。为了使用 .xlsx,请使用 XSSFWorkbookXSSFSheet class.

与 HSSF 类似,POI 对于其他文件格式也有不同的前缀:

HSSF(糟糕的电子表格格式) – 读取和写入 Microsoft Excel (XLS) 格式文件。

XSSF(XML SpreadSheet 格式) – 读写 Office Open XML (XLSX) 格式文件。

HPSF(可怕的 属性 设置格式) – 从 Microsoft Office 文件中读取“文档摘要”格式。

HWPF (Horrible Word Processor Format) – 旨在读写 Microsoft Word 97 (DOC) 格式文件。

HSLF(糟糕的幻灯片版式格式) – Microsoft PowerPoint 文件的纯 Java 实现。

HDGF(可怕的图表格式) – Microsoft Visio 二进制文件的初始纯 Java 实现。

HPBF(可怕的 PuBlisher 格式) – Microsoft Publisher 文件的纯 Java 实现。

HSMF(可怕的愚蠢邮件格式) – Microsoft Outlook MSG 文件的纯 Java 实现。

DDF(可怕的绘图格式) – 用于解码 Microsoft Office 绘图格式的包。

Create New Excel File

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//..
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FuSsA sheet");
//Create a new row in current sheet
Row row = sheet.createRow(0);
//Create a new cell in current row
Cell cell = row.createCell(0);
//Set value to new value
cell.setCellValue("Slim Shady");
    try {
        FileOutputStream out = 
                new FileOutputStream(new File("C:\new.xls"));
        workbook.write(out);
        out.close();
        System.out.println("Excel written successfully..");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Update Existing Excel File

try {
            FileInputStream file = new FileInputStream(new File("C:\update.xls"));

            HSSFWorkbook workbook = new HSSFWorkbook(file);
            HSSFSheet sheet = workbook.getSheetAt(0);
            Cell cell = null;

            //Update the value of cell
            cell = sheet.getRow(1).getCell(2);
            cell.setCellValue(cell.getNumericCellValue() * 2);
            cell = sheet.getRow(2).getCell(2);
            cell.setCellValue(cell.getNumericCellValue() * 2);
            cell = sheet.getRow(3).getCell(2);
            cell.setCellValue(cell.getNumericCellValue() * 2);

            file.close();

            FileOutputStream outFile =new FileOutputStream(new File("C:\update.xls"));
            workbook.write(outFile);
            outFile.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

有关向单元格添加公式和添加样式的更多详细信息,您可以查看此 link:Read / Write Excel file in Java using Apache POI

要操作任何单元格或添加公式,您可以使用以下方法:

HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Calculate Simple Interest");

        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("Pricipal Amount (P)");
        header.createCell(1).setCellValue("Rate of Interest (r)");
        header.createCell(2).setCellValue("Tenure (t)");
        header.createCell(3).setCellValue("Interest (P r t)");

        Row dataRow = sheet.createRow(1);
        dataRow.createCell(0).setCellValue(14500d);
        dataRow.createCell(1).setCellValue(9.25);
        dataRow.createCell(2).setCellValue(3d);
        dataRow.createCell(3).setCellFormula("A2*B2*C2");

        try {
            FileOutputStream out = 
                    new FileOutputStream(new File("C:\formula.xls"));
            workbook.write(out);
            out.close();
            System.out.println("Excel written successfully..");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

为单元格添加样式,

you can use: cell.setCellStyle(style);

要为单元格添加背景,您可以使用以下方法:

cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);