天天看点

java 导出excel设置数字格式

                    // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成

                    if (textValue != null) {

                        Pattern p = Pattern.compile("^\\d+(\\.\\d+)?$");

                        Matcher matcher = p.matcher(textValue);

                        if (matcher.matches()) {

                            cell.setCellValue(Double.parseDouble(textValue));// 是数字当作double处理

                            style2.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));//excel 自定义的格式

                            cell.setCellStyle(style2);

                        } else {

                            HSSFRichTextString richString = new HSSFRichTextString(textValue);

                            HSSFFont font3 = workbook.createFont();

                            font3.setColor(HSSFColor.BLUE.index);

                            richString.applyFont(font3);

                            cell.setCellValue(richString);

                        }

                    }

http://blog.sina.com.cn/s/blog_70bcd7c10101cvl4.html