当我下载 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 类型为 numeric ...
1 回答
神不在的星期二
TA贡献1963条经验 获得超6个赞
您可以使用 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");
添加回答
举报
0/150
提交
取消