当我下载Excel时,我得到格式为Numbers的单元格我需要格式文本和所有单元格。
try {
XSSFSheet sheet = workbook.createSheet(eyelash);
XSSFFont font = workbook.createFont();
font.setColor(HSSFColor.WHITE.index);
XSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.RED.index);
String[] header = { "name","surname"};
XSSFRow rowhead = sheet.createRow((short) 0);
XSSFCell cell;
int cellnum = 0;
for (int i = 0; i < header.length; i++) {
cell = rowhead.createCell(cellnum);
cell.setCellValue(header[i]);
cell.setCellStyle(style);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cellnum++;
}
if (data) {
int myRowData = 1;
XSSFRow row = sheet.createRow((short) myRowData);
ArrayList<GasNomination> list = this.select();
for (int i = 0; i < list.size(); i++) {
row.createCell(0).setCellValue(list.get(i).name());
row.createCell(1).setCellValue(list.get(i).surname());
myRowData++;
row = sheet.createRow((short) myRowData);
}
}
} catch (Exception ex) {
}
我试过cell.setCellType(XSSFCell.CELL_TYPE_STRING);但是当我打开我的Excel时,我看到formatcell类型数字...
你可以使用DataFormatter,
System.out.println("started");
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue(3.14159);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
XSSFDataFormat format = workbook.createDataFormat();
XSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(format.getFormat("Text"));
cell.setCellStyle(style);
workbook.write(new FileOutputStream("Test.xlsx"));
System.out.println("finished");