Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache poi十进制格式仅在选择后应用_Java_Apache Poi - Fatal编程技术网

Java Apache poi十进制格式仅在选择后应用

Java Apache poi十进制格式仅在选择后应用,java,apache-poi,Java,Apache Poi,你能建议下一个案子吗 我已经设置了数据格式和单元格类型(“#,#,##.00”,数字) (我想要thounsand分隔符加上两个十进制数) 它的工作原理和我预期的一样,但要格式化单元格,我需要首先选择它们 在选择之前,数据看起来没有格式化 换言之,我必须选择单元格,以便对其进行格式化,否则它将保持不进行任何格式化 CellStyle style = workbook.createCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT);

你能建议下一个案子吗

我已经设置了数据格式和单元格类型(“#,#,##.00”,数字) (我想要thounsand分隔符加上两个十进制数)

它的工作原理和我预期的一样,但要格式化单元格,我需要首先选择它们 在选择之前,数据看起来没有格式化

换言之,我必须选择单元格,以便对其进行格式化,否则它将保持不进行任何格式化

CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.RIGHT);
style.setLocked(locked);
style.setDataFormat(workbook.createDataFormat().getFormat("#,###.00"));

cell.setCellStyle(style);
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(<big decimal value>.toString());
CellStyle style=workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.RIGHT);
style.setLocked(已锁定);
style.setDataFormat(workbook.createDataFormat().getFormat(“#,#,##.00”);
cell.setCellStyle(style);
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(.toString());

如果需要数值单元格值,请不要将单元格值设置为字符串。如果将单元格值设置为
String
,则单元格类型也将为String。这与在设置单元格值之前设置单元格类型无关。设置
字符串
单元格值时,类型始终更改为字符串

查看哪些显示不推荐使用
Cell.setCellType
,哪些
Cell.setCellValue
方法是可行的

如果单元格应有数字内容,则需要设置一个
单元格值

例如:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.math.BigDecimal;
import java.util.Random;

class CreateExcelCellNumberFormat {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   CellStyle style = workbook.createCellStyle();
   style.setDataFormat(workbook.createDataFormat().getFormat("#,###.00"));

   Sheet sheet = workbook.createSheet();

   for (int r = 0; r < 10; r++) {
    Cell cell = sheet.createRow(r).createCell(0);
    cell.setCellStyle(style);

    BigDecimal bigDecimal = new BigDecimal(new Random().nextDouble() * 10000000000000d);
    cell.setCellValue(bigDecimal.doubleValue());

   }

   sheet.setColumnWidth(0, 25 * 256);

   workbook.write(fileout);
  }
 }
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入java.math.BigDecimal;
导入java.util.Random;
类CreateExcelCellNumberFormat{
公共静态void main(字符串[]args)引发异常{
尝试(工作簿=新XSSFWORKWORK();
FileOutputStream fileout=新的FileOutputStream(“Excel.xlsx”)){
CellStyle style=workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat(“#,#,##.00”);
工作表=工作簿.createSheet();
对于(int r=0;r<10;r++){
Cell Cell=sheet.createRow(r).createCell(0);
cell.setCellStyle(style);
BigDecimal BigDecimal=新的BigDecimal(新的Random().nextDouble()*100000000000d);
cell.setCellValue(bigDecimal.doubleValue());
}
表.设置柱宽(0,25*256);
工作簿。写入(归档);
}
}
}

请显示用于创建和设置单元格样式以及设置单元格值的代码。如果没有代码,就不可能有任何帮助,因为数以千计的
apache poi
用户正在做您正在描述的事情,而没有您正在描述的问题。您好@Axel Ritcher,added代码仅显示如何创建和设置单元格样式。您仍然没有显示如何设置单元格值。请出示。@AxelRitcher补充道