我哪里错了?
我正在关注本教程:https://www.youtube.com/watch?v=PvrnbhGeSKE
但是,当我粘贴以下代码时,eclipse给了我这个错误:“ ALIGN_CENTER无法解析或不是字段”
package main;
import entities.*;
import dao.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
public class Main {
public static void main(String[] args) {
try {
ProductModel pmodel = new ProductModel();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("List Products");
Row rowheading = sheet.createRow(0);
rowheading.createCell(0).setCellValue("Id");
rowheading.createCell(1).setCellValue("Name");
rowheading.createCell(2).setCellValue("Creation Date");
rowheading.createCell(3).setCellValue("Price");
rowheading.createCell(4).setCellValue("Quantity");
rowheading.createCell(5).setCellValue("Sub Total");
for (int i = 0; i < 6; i++) {
CellStyle stylerowHeading = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short) 11);
stylerowHeading.setFont(font);
stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowheading.getCell(i).setCellStyle(stylerowHeading);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
在以下代码段的MainActivity中会发生这种情况:
stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowheading.getCell(i).setCellStyle(stylerowHeading);
具体错误在“ CellStyle.ALIGN_CENTER”中
最佳答案
这些常量已移至单独的类。正确的方法应该是:
stylerowHeading.setVerticalAlignment(VerticalAlignment.CENTER);