如何设置Excel中的行的数据格式?
我试图在Excel中设置数据types为date格式('yyyy-MM-dd'),为此我写了以下一段代码:
.... Row r = null; CellStyle cellStyle = workbook.createCellStyle(); //CreationHelper createHelper = workbook.getCreationHelper(); cellStyle.setDataFormat( workbook.createDataFormat().getFormat("yyyy-MM-dd")); for(int i =0;i < 2;i++){ switch(i){ case 0: r = sheet.getRow(0); r.setRowStyle(cellStyle); break; case 1: r = sheet.getRow(1); r.setRowStyle(cellStyle); break; default: break; } } ....
我只发布了前两行的格式以供参考。 但是,当我运行我的代码,并打开Excel表格,并检查前两行的单元格的格式,那么它只是一般的。 更改没有反映在Excel工作表中。 我没有得到我在这里做错了什么。
如果你是从头开始创build这个Excel表格,那么它还没有行。 我得到一个新的工作表中的getRow(0)
NullPointerException
。 尝试使用createRow(0)
来代替:
public static void main(String[] args) throws JDOMException, IOException { File excelFile = new File("C:/temp/test.xls"); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("sheetname"); Row r = null; CellStyle cellStyle = workbook.createCellStyle(); // CreationHelper createHelper = workbook.getCreationHelper(); cellStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd")); for (int i = 0; i < 2; i++) { switch (i) { case 0: r = sheet.createRow(0); r.setRowStyle(cellStyle); break; case 1: r = sheet.createRow(1); r.setRowStyle(cellStyle); break; default: break; } } try (FileOutputStream out = new FileOutputStream(excelFile)) { workbook.write(out); } Desktop.getDesktop().open(excelFile); }
- 在Java中使用Apoche POI读取excel(xlsx)文件(获取奇怪的数字)
- 如何从Excel文件单元格使用POI获取数值和string值,我正在获取数值错误,string值很容易获取
- Apache POI,创build新的单元格覆盖了行的样式
- Apache-poi限制用户只select下拉列表的值
- 而将Excel转换为.csv文件,我不能够转换date,并将其保存在CSV文件?
- 生成新的Excel,同时保留模板Excel文件中的旧公式
- 使用apache poi 3.7多次写入xlsx文档时出现exception
- 用于打印的手动分页符在Apache POI excel中不起作用
- apache poi jar缺less类文件