簡體   English   中英

如何在 Apache POI 中制作帶有“逗號樣式”單元格的 excel 工作表?

[英]How to make excel sheet with "comma style" cell in Apache POI?

我的任務是將 excel 文件格式化為“逗號樣式”。 例如,我的單元格中的值為 0 或 0.00,然后我按 excel 中的“,”按鈕

在此處輸入圖像描述

結果我的價值變成了“-”。

在此處輸入圖像描述

如果我有價值“你好”

在此處輸入圖像描述

按下此按鈕后 - 值向右移動一點點:

在此處輸入圖像描述

我的任務是模擬在 Apache poi 中按下這個按鈕,我該怎么做?

我不知道該怎么做,我只能找到以下格式:但它不起作用:

public class Test {
public static void main(String s[]) {
    try{
        FileOutputStream out = new FileOutputStream
                ("dateFormat.xls");
        HSSFWorkbook hssfworkbook = new HSSFWorkbook();
        HSSFSheet sheet = hssfworkbook.createSheet
                ("new sheet");
        HSSFCellStyle cs = hssfworkbook.createCellStyle();
        HSSFDataFormat df = hssfworkbook.
                createDataFormat();
        cs.setDataFormat(df.getFormat("#,##0.0"));
        HSSFRow row = sheet.createRow((short)0);
        HSSFCell cell = row.createCell((short)0);
        cell.setCellValue(11111.0);
        cell.setCellStyle(cs);

        HSSFCell cell2 = row.createCell((short)1);
        cell2.setCellValue(0);
        cell2.setCellStyle(cs);

        HSSFCell cell3 = row.createCell((short)2);
        cell3.setCellValue("hello");
        cell3.setCellStyle(cs);

        hssfworkbook.write(out);
        out.close();
    }catch(Exception e){}
}
}

我還有單獨的任務——如果值是 1.0,它應該變成 1,如果你能幫我處理這個格式就太好了!

我不確定該代碼,我有一個類似的任務但我自己解析了數字......這是代碼,但不確定它是否會幫助你:


private String getNumericValue(final String raw) {
    final double value = Double.parseDouble(raw);
    final String strValue = String.valueOf(new BigDecimal(value).setScale(1, RoundingMode.CEILING).doubleValue()).replace('.', ',');
    if (strValue.endsWith(",0"))
        return strValue.substring(0, (strValue.length() - 2));
    return strValue;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM