Skip to content

Commit b9965d2

Browse files
author
Rajeev Kumar Singh
committed
ExcelReader, ExcelWriter Examples
1 parent 1c9f69d commit b9965d2

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

Diff for: poi-generated-file.xlsx

1 Byte
Binary file not shown.

Diff for: src/main/java/ExcelReader.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class ExcelReader {
1313
public static final String SAMPLE_XLS_FILE_PATH = "./sample-xls-file.xls";
14-
public static final String SAMPLE_XLSX_FILE_PATH = "sample-xlsx-file.xlsx";
14+
public static final String SAMPLE_XLSX_FILE_PATH = "./sample-xlsx-file.xlsx";
1515

1616
public static void main(String[] args) throws IOException, InvalidFormatException {
1717

@@ -47,8 +47,6 @@ Iterating over all the sheets in the workbook (Multiple ways)
4747
System.out.println("=> " + sheet.getSheetName());
4848
});
4949

50-
51-
5250
/*
5351
==================================================================
5452
Iterating over all the rows and columns in a Sheet (Multiple ways)

Diff for: src/main/java/ExcelWriter.java

+38-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import org.apache.poi.ss.usermodel.*;
33
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
44

5+
import java.io.File;
56
import java.io.FileOutputStream;
67
import java.io.IOException;
78
import java.util.ArrayList;
@@ -37,7 +38,10 @@ public class ExcelWriter {
3738
public static void main(String[] args) throws IOException, InvalidFormatException {
3839

3940
// Create a Workbook
40-
Workbook workbook = new XSSFWorkbook();
41+
Workbook workbook = new XSSFWorkbook(); // new HSSFWorkbook() for generating `.xls` file
42+
43+
/* CreationHelper helps us create instances for various things like
44+
DataFormat, Hyperlink, RichTextString etc in a format (HSSF, XSSF) independent way */
4145
CreationHelper createHelper = workbook.getCreationHelper();
4246

4347
// Create a Sheet
@@ -78,14 +82,15 @@ public static void main(String[] args) throws IOException, InvalidFormatExceptio
7882
row.createCell(1)
7983
.setCellValue(employee.getEmail());
8084

81-
Cell dobCell = row.createCell(2);
82-
dobCell.setCellValue(employee.getDateOfBirth());
83-
dobCell.setCellStyle(dateCellStyle);
85+
Cell dateOfBirthCell = row.createCell(2);
86+
dateOfBirthCell.setCellValue(employee.getDateOfBirth());
87+
dateOfBirthCell.setCellStyle(dateCellStyle);
8488

8589
row.createCell(3)
8690
.setCellValue(employee.getSalary());
8791
}
8892

93+
// Resize all columns to fit the content size
8994
for(int i = 0; i < columns.length; i++) {
9095
sheet.autoSizeColumn(i);
9196
}
@@ -95,6 +100,35 @@ public static void main(String[] args) throws IOException, InvalidFormatExceptio
95100
workbook.write(fileOut);
96101
fileOut.close();
97102
}
103+
104+
105+
// Example to modify an existing excel file
106+
private static void modifyExistingWorkbook() throws InvalidFormatException, IOException {
107+
// Obtain a workbook from the excel file
108+
Workbook workbook = WorkbookFactory.create(new File("existing-spreadsheet.xlsx"));
109+
110+
// Get Sheet at index 0
111+
Sheet sheet = workbook.getSheetAt(0);
112+
113+
// Get Row at index 1
114+
Row row = sheet.getRow(1);
115+
116+
// Get the Cell at index 2 from the above row
117+
Cell cell = row.getCell(2);
118+
119+
// Create the cell if it doesn't exist
120+
if (cell == null)
121+
cell = row.createCell(2);
122+
123+
// Update the cell's value
124+
cell.setCellType(CellType.STRING);
125+
cell.setCellValue("Updated Value");
126+
127+
// Write the output to a file
128+
FileOutputStream fileOut = new FileOutputStream("existing-spreadsheet.xlsx");
129+
workbook.write(fileOut);
130+
fileOut.close();
131+
}
98132
}
99133

100134

0 commit comments

Comments
 (0)