这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion apache-poi-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.web.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
Expand Down Expand Up @@ -108,6 +113,7 @@
<logback-core.version>1.5.6</logback-core.version>
<jmh.version>1.37</jmh.version>
<log4j.version>2.23.1</log4j.version>
<spring.web.version>3.5.7</spring.web.version>
</properties>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.hssfworkbook;

import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) throws IOException {
SpringApplication.run(DemoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.baeldung.hssfworkbook;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@RestController
public class ExcelController {

@GetMapping("/download-excel")
public ResponseEntity<byte[]> downloadExcel() {
try {
HSSFWorkbook workbook = ExcelCreator.createSampleWorkbook();

try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
workbook.write(baos);
byte[] bytes = baos.toByteArray();

return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=employee_data.xls")
.contentType(MediaType.parseMediaType("application/vnd.ms-excel")) // More specific MIME type
.body(bytes);
}
} catch (IOException e) {
System.err.println("Error generating or writing Excel workbook: " + e.getMessage());
return ResponseEntity.internalServerError()
.build();
}
}

@PostMapping("/upload-excel")
public ResponseEntity<String> uploadExcel(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return ResponseEntity.badRequest()
.body("File is empty. Please upload a file.");
}

try {
try (HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream())) {
Sheet sheet = workbook.getSheetAt(0);

return ResponseEntity.ok("Sheet '" + sheet.getSheetName() + "' uploaded successfully!");
}
} catch (IOException e) {
System.err.println("Error processing uploaded Excel file: " + e.getMessage());
return ResponseEntity.internalServerError()
.body("Failed to process the Excel file.");
} catch (Exception e) {
System.err.println("An unexpected error occurred during file upload: " + e.getMessage());
return ResponseEntity.internalServerError()
.body("An unexpected error occurred.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.hssfworkbook;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelConverter {

public static byte[] convertWorkbookToBytes(HSSFWorkbook workbook) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
workbook.write(baos);
return baos.toByteArray();
}
}

public static HSSFWorkbook convertBytesToWorkbook(byte[] excelBytes) throws IOException {
try (ByteArrayInputStream bais = new ByteArrayInputStream(excelBytes)) {
return new HSSFWorkbook(bais);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.baeldung.hssfworkbook;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

public class ExcelCreator {

public static HSSFWorkbook createSampleWorkbook() {
HSSFWorkbook workbook = new HSSFWorkbook();

final String SHEET_NAME = "Employees";
final String[] COLUMN_HEADERS = { "ID", "Name", "Department" };
Object[][] data = { { 101, "John Doe", "Finance" }, { 102, "Jane Smith", "HR" }, { 103, "Michael Clark", "IT" } };

Sheet sheet = workbook.createSheet(SHEET_NAME);

HSSFFont font = workbook.createFont();
font.setBold(true);
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFont(font);

Row header = sheet.createRow(0);
for (int i = 0; i < COLUMN_HEADERS.length; i++) {
Cell cell = header.createCell(i);
cell.setCellValue(COLUMN_HEADERS[i]);
cell.setCellStyle(headerStyle);
}

int rowNum = 1;
for (Object[] rowData : data) {
Row row = sheet.createRow(rowNum++);
for (int i = 0; i < rowData.length; i++) {
Cell cell = row.createCell(i);
Object value = rowData[i];

if (value instanceof Integer) {
cell.setCellValue(((Integer) value).doubleValue());
} else if (value instanceof Double) {
cell.setCellValue((Double) value);
} else if (value != null) {
cell.setCellValue(value.toString());
}
}
}

for (int i = 0; i < COLUMN_HEADERS.length; i++) {
sheet.autoSizeColumn(i);
}

return workbook;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.baeldung.hssfworkbook;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;

import com.baeldung.hssfworkbook.ExcelConverter;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

class ExcelConverterTest {

private HSSFWorkbook createMinimalWorkbook() {
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("TestSheet");
sheet.createRow(0)
.createCell(0)
.setCellValue("Test Data");
return workbook;
}

private byte[] convertWorkbookToBytesSafely(HSSFWorkbook workbook) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
workbook.write(baos);
return baos.toByteArray();
} catch (IOException e) {
throw new UncheckedIOException("Failed to create test bytes.", e);
}
}

@Test
@DisplayName("GivenAValidHSSFWorkbook_whenConvertingToBytes_thenByteArrayIsNonEmptyAndValid")
void givenAValidHSSFWorkbook_whenConvertingToBytes_thenByteArrayIsNonEmptyAndValid() throws IOException {
HSSFWorkbook workbook = createMinimalWorkbook();

byte[] resultBytes = ExcelConverter.convertWorkbookToBytes(workbook);

assertNotNull(resultBytes, "The byte array should not be null.");
assertTrue(resultBytes.length > 0, "The byte array should not be empty.");

HSSFWorkbook convertedWorkbook = ExcelConverter.convertBytesToWorkbook(resultBytes);
assertEquals("TestSheet", convertedWorkbook.getSheetName(0));
}

@Test
@DisplayName("GivenAValidExcelByteArray_whenConvertingToWorkbook_thenHSSFWorkbookIsReturned")
void givenAValidExcelByteArray_whenConvertingToWorkbook_thenHSSFWorkbookIsReturned() throws IOException {
HSSFWorkbook originalWorkbook = createMinimalWorkbook();
byte[] validExcelBytes = convertWorkbookToBytesSafely(originalWorkbook);

HSSFWorkbook resultWorkbook = ExcelConverter.convertBytesToWorkbook(validExcelBytes);

assertNotNull(resultWorkbook, "The resulting workbook should not be null.");
assertEquals(1, resultWorkbook.getNumberOfSheets(), "The resulting workbook should have 1 sheet.");
assertEquals("TestSheet", resultWorkbook.getSheetName(0), "Sheet name should match the original.");
assertEquals("Test Data", resultWorkbook.getSheetAt(0)
.getRow(0)
.getCell(0)
.getStringCellValue());
}

@Test
@DisplayName("GivenAnEmptyByteArray_whenConvertingToWorkbook_thenIOExceptionIsThrown")
void givenAnEmptyByteArray_whenConvertingToWorkbook_thenIOExceptionIsThrown() {
byte[] emptyBytes = new byte[0];

assertThrows(IOException.class, () -> ExcelConverter.convertBytesToWorkbook(emptyBytes), "Expected IOException for empty byte array.");
}

@Test
@DisplayName("GivenAnInvalidByteArray_whenConvertingToWorkbook_thenIOExceptionIsThrown")
void givenAnInvalidByteArray_whenConvertingToWorkbook_thenIOExceptionIsThrown() {
byte[] invalidBytes = { 0x01, 0x02, 0x03, 0x04, 0x05 };

assertThrows(IOException.class, () -> ExcelConverter.convertBytesToWorkbook(invalidBytes), "Expected IOException for invalid byte array format.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.baeldung.hssfworkbook;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import com.baeldung.hssfworkbook.ExcelCreator;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ExcelCreatorTest {

private static final String SHEET_NAME = "Employees";
private static final String[] COLUMN_HEADERS = { "ID", "Name", "Department" };
private static final Object[][] DATA = { { 101, "John Doe", "Finance" }, { 102, "Jane Smith", "HR" }, { 103, "Michael Clark", "IT" } };

@Test
@DisplayName("Workbook should be created and contain one sheet named 'Employees'")
void givenExcelCreator_whenCreateSampleWorkbookCalled_thenWorkbookContainsOneSheetNamedEmployees() {
HSSFWorkbook workbook = ExcelCreator.createSampleWorkbook();

assertNotNull(workbook, "The workbook should not be null.");
assertEquals(1, workbook.getNumberOfSheets(), "The workbook should contain exactly one sheet.");

Sheet sheet = workbook.getSheet(SHEET_NAME);
assertNotNull(sheet, "The sheet named '" + SHEET_NAME + "' must exist.");
}

@Test
@DisplayName("Header row should have correct content and bold style")
void givenExcelCreator_whenCreateSampleWorkbookCalled_thenHeaderRowContainsCorrectTextAndBoldStyle() {
HSSFWorkbook workbook = ExcelCreator.createSampleWorkbook();
Sheet sheet = workbook.getSheet(SHEET_NAME);

Row headerRow = sheet.getRow(0);
assertNotNull(headerRow, "Header row (row 0) should exist.");

Font expectedFont = workbook.getFontAt(headerRow.getCell(0)
.getCellStyle()
.getFontIndex());
assertTrue(expectedFont.getBold(), "Header font should be bold.");

for (int i = 0; i < COLUMN_HEADERS.length; i++) {
Cell cell = headerRow.getCell(i);
assertNotNull(cell, "Header cell at index " + i + " should exist.");
assertEquals(COLUMN_HEADERS[i], cell.getStringCellValue(), "Header text mismatch at column " + i + ".");

CellStyle cellStyle = cell.getCellStyle();
assertNotNull(cellStyle, "Header cell should have a style applied.");

Font actualFont = workbook.getFontAt(cellStyle.getFontIndex());
assertTrue(actualFont.getBold(), "Header cell at index " + i + " style must be bold.");
}
}

@Test
@DisplayName("Data rows should have correct content and data type handling")
void givenExcelCreator_whenCreateSampleWorkbookCalled_thenDataRowsContainCorrectContentAndTypes() {
HSSFWorkbook workbook = ExcelCreator.createSampleWorkbook();
Sheet sheet = workbook.getSheet(SHEET_NAME);

assertEquals(DATA.length + 1, sheet.getLastRowNum() + 1, "The sheet should contain header row + data rows.");

for (int i = 0; i < DATA.length; i++) {
Row dataRow = sheet.getRow(i + 1);
Object[] expectedRowData = DATA[i];

assertNotNull(dataRow, "Data row at index " + (i + 1) + " should exist.");

for (int j = 0; j < expectedRowData.length; j++) {
Cell cell = dataRow.getCell(j);
Object expectedValue = expectedRowData[j];

assertNotNull(cell, "Cell [" + (i + 1) + ", " + j + "] should exist.");

if (expectedValue instanceof Integer) {
assertEquals(CellType.NUMERIC, cell.getCellType(), "Cell " + j + " type should be NUMERIC for Integer.");
assertEquals(((Integer) expectedValue).doubleValue(), cell.getNumericCellValue(), 0.001, "Numeric value mismatch.");
} else {
assertEquals(CellType.STRING, cell.getCellType(), "Cell " + j + " type should be STRING.");
assertEquals(expectedValue.toString(), cell.getStringCellValue(), "String value mismatch.");
}
}
}
}
}