Read / Write Excel file in Java using Apache POI

本文介绍如何使用 Apache POI 库读取、创建及更新 Excel 文件,并演示了如何添加公式和样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Apache POI is a powerful Java library to work with differentMicrosoft Office file formats such as Excel, Power point, Visio, MS Word etc.The name POI was originally an acronym for Poor Obfuscation Implementation, referring humorously to the fact that the file formats seemedto be deliberately obfuscated, but poorly, since they were successfullyreverse-engineered.

In this tutorial wewill use Apache POI library to perform different functions on Microsoft Excelspreadsheet.

Let’s get started.

Tools & Technologies:

1.    Java JDK 1.5 or above

2.    Apache POI library v3.8 or above (download)

3.    Eclipse 3.2 above (optional)

1. Add Apache POIdependency

Make sure to includeapache poi jar file to your project. If your project uses Maven as dependencymanagement, add following in your Pom.xml file.

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

If you are not usingMaven then you can directly add required JAR files in your classpath.

1.    Download poi-2.5.1.jar(or in this case3.8) jar file.

2.    Include this file in your projects class path.

3.    Create new java project in eclipse with auto generated mainfunction.

2. Read Excel File

To read an excel file,Apache POI provides certain easy-to-use APIs. In below sample code we usedifferent classes from POI library to read content of cell from excel file.This is for quick reference.

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();

Notice how each class in POI library starts with   HSSF   prefix! e.g. HSSFWorkbook, HSSFSheet etc. HSSF stands for   Horrible SpreadSheet Format ! I’m not kidding.. It really is.

Similar to HSSF, POIhas different prefix for other file formats too:

1.    HSSF (Horrible SpreadSheet Format) – reads and writes MicrosoftExcel (XLS) format files.

2.    XSSF (XML SpreadSheet Format) – reads and writes Office Open XML(XLSX) format files.

3.    HPSF (Horrible Property Set Format) – reads “Document Summary”information from Microsoft Office files.

4.    HWPF (Horrible Word Processor Format) – aims to read and writeMicrosoft Word 97 (DOC) format files.

5.    HSLF (Horrible Slide Layout Format) – a pure Java implementationfor Microsoft PowerPoint files.

6.    HDGF (Horrible DiaGram Format) – an initial pure Javaimplementation for Microsoft Visio binary files.

7.    HPBF (Horrible PuBlisher Format) – a pure Java implementationfor Microsoft Publisher files.

8.    HSMF (Horrible Stupid Mail Format) – a pure Java implementationfor Microsoft Outlook MSG files

9.    DDF (Dreadful Drawing Format) – a package for decoding theMicrosoft Office Drawing format.

Working with .xlsx files

The classes we used in above code snippet, HSSFWorkbook and HSSFSheet works for .xlsformat. In order towork with newer xls format viz .xlsx, you need to see newer POI classes like:

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
//..
FileInputStream file = new FileInputStream(new File("C:\\test.xlsx"));
             
//Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook (file);
 
//Get first sheet from the workbook
XSSFSheet 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();

Use   XSSFWorkbook   and   XSSFSheet   class in all of the below examples in order tomake them work with .xlsx files.

Consider a sampleexcel file:

test.xls

We will read above xlsfile using Apache POI and prints the data.

try {
     
    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);
     
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();
    while(rowIterator.hasNext()) {
        Row row = rowIterator.next();
         
        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while(cellIterator.hasNext()) {
             
            Cell cell = cellIterator.next();
             
            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    break;
            }
        }
        System.out.println("");
    }
    file.close();
    FileOutputStream out =
        new FileOutputStream(new File("C:\\test.xls"));
    workbook.write(out);
    out.close();
     
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

The above code is self explanatory. It read the sheet fromworkbook and iterate through each row and cell to print its values. Just notehow we use different methods like getBooleanCellValue ,   getNumericCellValue   etc to read cell value. Before reading a cell content, we needto first determine its type using method   cell.getCellType()   and then call appropriate method to read content.

Output:

Emp Id      Name        Salary     
1.0     John        2000000.0      
2.0     Dean        420000.0       
3.0     Sam     280000.0       
4.0     Cass        6000000.0  

3. Create New Excel File

Let us create a newexcel file and write data in it. Following is the API which we will use forthis purpose.

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//..
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample 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("Blahblah");


Below is the completecode that writes a new excel with dummy data:

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet");
 
Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("1", new Object[] {"Emp No.", "Name", "Salary"});
data.put("2", new Object[] {1d, "John", 1500000d});
data.put("3", new Object[] {2d, "Sam", 800000d});
data.put("4", new Object[] {3d, "Dean", 700000d});
 
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
    Row row = sheet.createRow(rownum++);
    Object [] objArr = data.get(key);
    int cellnum = 0;
    for (Object obj : objArr) {
        Cell cell = row.createCell(cellnum++);
        if(obj instanceof Date)
            cell.setCellValue((Date)obj);
        else if(obj instanceof Boolean)
            cell.setCellValue((Boolean)obj);
        else if(obj instanceof String)
            cell.setCellValue((String)obj);
        else if(obj instanceof Double)
            cell.setCellValue((Double)obj);
    }
}
 
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();
}

Output:   new.xls

4. Update Existing ExcelFile

Updating an existingexcel file is straight forward. Open the excel using different API that wediscussed above and set the cell’s value. One thing we need to note here isthat we can update the excel file only when we close it first.

update.xls

Following Java coderead the above excel file and doubles the salary of each employee:

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();
}


1.    Open excel file in input mode (inputstream)Steps to update excelfile will be:

2.    Use POI API and read the excel content

3.    Update cell’s value using different setCellValue methods.

4.    Close the excel input file (inputstream)

5.    Open same excel file in output mode (outputstream)

6.    Write content of updated workbook in output file

7.    Close output excel file

Output: update.xls

5. Adding Formulas

Apache POI providesAPI to add excel formulas to cell programmatically. Following method that comeshandy for this:

cell.setCellFormula("someformula")

For example:

cell.setCellFormula("A2*B2*C5")
//or
cell.setCellFormula("SUM(A1:A7)")

Note:   Formula string should not start with equal sign (=)
Thus, following is incorrect way of adding formula:

cell.setCellFormula("=A2*B2*C5") //Ops! Won't work

The above code willthrow:

org.apache.poi.ss.formula.FormulaParseException:
The specified formula '=A2*B2*C5' starts with an equals sign which is not allowed.

Following Java codecreates a new excel sheet which calculates Simple Interest. It definesPrincipal amount, Rate of Interest and Tenure. We add an excel formula tocalculate interest.

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();
    }

Output:   formula.xls

Triggering ExistingExcel Formulas

In certain cases yourexcel file might have formula defined and you may want to trigger thoseformulas since you updated it using POI. Following code snippet will do thetrick.

FileInputStream fis = new FileInputStream("/somepath/test.xls");
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("C:\\test.xls")
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
for(int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
    Sheet sheet = wb.getSheetAt(sheetNum);
    for(Row r : sheet) {
        for(Cell c : r) {
            if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
                evaluator.evaluateFormulaCell(c);
            }
        }
    }
}

We use   FormulaEvaluator   class to evaluateformula defined in each of the cell.

6. Adding Styles to Cell

Adding style to a cellis also piece of cake. Check following example which creates two new cell onewith bold font and another with italic and add text to it.

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Style example");
 
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
 
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("This is bold");
cell.setCellStyle(style);
 
 
font = workbook.createFont();
font.setItalic(true);
style = workbook.createCellStyle();
style.setFont(font);
 
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("This is italic");
cell.setCellStyle(style);
 
try {
    FileOutputStream out = new FileOutputStream(new File("C:\\style.xls"));
    workbook.write(out);
    out.close();
    System.out.println("Excel written successfully..");
     
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Output:   style.xls

6.1 Add Background Colorto the cell

Changing background color of the cell is a bit tricky. Ideallywe assume setting background color will have some API like setFillBackgroundColor, but surprisingly to set background color of a cell, we have toset the foreground color :D. See below API.

cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);

So use   setFillForegroundColor   method to set the background color of given cell.

I hope this article isuseful.

 


### 如何使用 Java POI 将文件作为附件添加到 Excel 工作表中 为了实现这一功能,可以通过创建 OLE 对象来嵌入文件。以下是具体的方法: #### 创建 OLE 对象并嵌入文件 通过 Apache POI 库中的 `XSSFWorkbook` 和 `HSSFClientAnchor` 类可以完成此操作。下面是一个完整的代码示例,展示了如何将 PDF 文件作为一个对象插入到 Excel 表格中。 ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFPicture; import org.apache.poi.xssf.usermodel.XSSFShape; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class InsertAttachmentExample { public static void insertFileAsObject(String filePath, String excelPath) throws IOException { try (FileInputStream fis = new FileInputStream(filePath); XSSFWorkbook workbook = new XSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream(excelPath)) { XSSFSheet sheet = workbook.createSheet("Embedded File"); // Create the drawing patriarch. XSSFDrawing patriarch = sheet.createDrawingPatriarch(); // Define anchor point for placing object on worksheet ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 1, 1, 5, 8); // Add embedded object to the worksheet using OlePackagePart and PackageRelationship classes from OOXML SDK byte[] oleData = IOUtils.toByteArray(fis); // Use commons-io library or similar method to read bytes // Create an OLE Object with a specific label that can be clicked by users within Excel application HSSFObjectData obj = patriarch.createObjectData(anchor, "package://example.pdf", oleData); // Save changes made above into output stream which will generate final .xlsx file containing attached document as icon/image placeholder inside cell range specified earlier via client anchor coordinates workbook.write(fileOut); System.out.println("File inserted successfully."); } } } ``` 这段代码实现了以下几点: - 加载要嵌入的目标文件(如PDF) - 新建了一个名为 “Embedded File” 的工作簿和工作表 - 定义了用于放置OLE对象的位置坐标 - 使用 `createObjectData()` 方法创建一个新的 OLE 对象,并指定其数据源以及显示标签[^2] 注意,在实际应用过程中可能还需要处理一些细节问题,比如调整图片大小、位置等参数以适应不同的需求场景;另外也建议引入第三方依赖库(例如commons-io)以便更方便地进行字节流转换操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值