Android中使用Apache POI时HSSFCellStyle的BORDER_MEDIUM未识别问题
HSSFCellStyle.BORDER_MEDIUM in Android Studio Hey there, I’ve run into this exact headache with Apache POI before—let’s break down what’s going wrong and fix it!
The Root Cause
Starting with Apache POI 3.17, the old border style constants like HSSFCellStyle.BORDER_MEDIUM were deprecated and moved to a dedicated BorderStyle enum class. If you’re using a newer POI version in your Android project, Android Studio can’t find the constant in HSSFCellStyle anymore because it no longer lives there.
Fix Steps
Update your code to use the
BorderStyleenum
Replace your problematic line with this:borderBottom.setBorderBottom(BorderStyle.MEDIUM);Don’t forget to add the import statement at the top of your file:
import org.apache.poi.ss.usermodel.BorderStyle;Verify your POI dependency version
If you’re stuck using the old constant (not recommended, since it’s deprecated), double-check your build.gradle file to ensure you’re using POI 3.16 or earlier. But I’d strongly suggest sticking with the newer enum approach—it’s the standard now and avoids future compatibility issues.Double-check your core imports
Make sure you’re pulling in the correctHSSFCellStyleclass too:import org.apache.poi.hssf.usermodel.HSSFCellStyle;
Quick Modified Code Snippet
Here’s how your updated code should look:
public boolean exportToExcel(final Context context, final String fileName){ if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { Log.e("External Storage", "Storage not available or read only"); return false; } resultExcelArrayList = new ArrayList<>(); success = false; String sheetName = "Sheet1"; wb = new HSSFWorkbook(); sheet = wb.createSheet(sheetName) ; HSSFCellStyle borderBottom = wb.createCellStyle(); // Use BorderStyle enum instead of old HSSFCellStyle constant borderBottom.setBorderBottom(BorderStyle.MEDIUM); // 其余代码省略... }
That should squash the compilation error and get your Excel export back on track!
内容的提问来源于stack exchange,提问作者Cees Graafland

